import { Component, Input, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import {
    ScanningBaseResult,
    VulnerabilitySeverity
} from '../service/index';
import { SCANNING_STYLES } from './scanning.css';
import { TIP_COMPONENT_HTML } from './scanning.html';

export const MIN_TIP_WIDTH = 5;
export const MAX_TIP_WIDTH = 100;

@Component({
    selector: 'hbr-scan-result-tip',
    template: TIP_COMPONENT_HTML,
    styles: [SCANNING_STYLES]
})
export class ResultTipComponent implements OnInit {
    _percent: number = 5;
    _tipTitle: string = '';

    @Input() severity: VulnerabilitySeverity = VulnerabilitySeverity.UNKNOWN;
    @Input() completeDateTime: Date = new Date(); //Temp
    @Input() data: ScanningBaseResult[] = [];
    @Input() noneNumber: number = 0;
    @Input()
    public get percent(): number {
        return this._percent;
    }

    public set percent(percent: number) {
        this._percent = percent;
        if (this._percent < MIN_TIP_WIDTH) {
            this._percent = MIN_TIP_WIDTH;
        }

        if (this._percent > MAX_TIP_WIDTH) {
            this._percent = MAX_TIP_WIDTH;
        }
    }

    _getSeverityKey(): string {
        switch (this.severity) {
            case VulnerabilitySeverity.HIGH:
                return 'VULNERABILITY.CHART.SEVERITY_HIGH';
            case VulnerabilitySeverity.MEDIUM:
                return 'VULNERABILITY.CHART.SEVERITY_MEDIUM';
            case VulnerabilitySeverity.LOW:
                return 'VULNERABILITY.CHART.SEVERITY_LOW';
            case VulnerabilitySeverity.NONE:
                return 'VULNERABILITY.CHART.SEVERITY_NONE';
            default:
                return 'VULNERABILITY.CHART.SEVERITY_UNKNOWN';
        }
    }

    constructor(private translateService: TranslateService) { }

    ngOnInit(): void {
        this.translateService.get(this._getSeverityKey())
            .subscribe((res: string) => this._tipTitle = res);
    }

    public get tipTitle(): string {
        if (!this.data) {
            return '';
        }

        let dataSize: number = this.data.length;
        return this._tipTitle + ' (' + dataSize + ')';
    }

    public get hasResultsToList(): boolean {
        return this.data &&
            this.data.length > 0 && (
                this.severity !== VulnerabilitySeverity.NONE &&
                this.severity !== VulnerabilitySeverity.UNKNOWN
            );
    }

    public get tipWidth(): string {
        return this.percent + 'px';
    }

    public get tipClass(): string {
        let baseClass: string = "tip-wrapper tip-block";

        switch (this.severity) {
            case VulnerabilitySeverity.HIGH:
                return baseClass + " bar-block-high";
            case VulnerabilitySeverity.MEDIUM:
                return baseClass + " bar-block-medium";
            case VulnerabilitySeverity.LOW:
                return baseClass + " bar-block-low";
            case VulnerabilitySeverity.NONE:
                return baseClass + " bar-block-none";
            default:
                return baseClass + " bar-block-unknown"
        }

    }

    public get isHigh(): boolean {
        return this.severity === VulnerabilitySeverity.HIGH;
    }

    public get isMedium(): boolean {
        return this.severity === VulnerabilitySeverity.MEDIUM;
    }

    public get isLow(): boolean {
        return this.severity === VulnerabilitySeverity.LOW;
    }

    public get isNone(): boolean {
        return this.severity === VulnerabilitySeverity.NONE;
    }

    public get isUnknown(): boolean {
        return this.severity === VulnerabilitySeverity.UNKNOWN;
    }

    public get tipIconClass(): string {
        switch (this.severity) {
            case VulnerabilitySeverity.HIGH:
                return "is-error";
            case VulnerabilitySeverity.MEDIUM:
                return "is-warning";
            case VulnerabilitySeverity.LOW:
                return "is-info";
            case VulnerabilitySeverity.NONE:
                return "is-success";
            default:
                return "is-highlight"
        }
    }
}
