import { Component } from '@angular/core';
import { LicenseService } from '@services/license.service';
import { map } from 'rxjs/operators';
import { ChartDataset } from 'chart.js';
import { ChartBlue, ChartGreen, ChartRed } from '../../utils/constants';
import { getLabelsFromLicense, getLineConfig } from '../../utils/functions';
import { TranslateService } from '@ngx-translate/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { size } from '@iqser/common-ui/lib/utils';

@Component({
    selector: 'red-license-analysis-capacity-usage',
    templateUrl: './license-analysis-capacity-usage.component.html',
    styleUrls: ['./license-analysis-capacity-usage.component.scss'],
})
export class LicenseAnalysisCapacityUsageComponent {
    readonly data$ = this.licenseService.selectedLicense$.pipe(map(() => this.#getData()));

    constructor(
        readonly licenseService: LicenseService,
        private readonly _translateService: TranslateService,
    ) {}

    #getData() {
        const yAxisLabel = _('license-info-screen.analysis-capacity-usage.analyzed-per-month');
        const yAxisLabelRight = _('license-info-screen.analysis-capacity-usage.total-analyzed-data');

        return {
            datasets: this.#getCapacityDatasets(),
            labels: getLabelsFromLicense(this.licenseService.selectedLicenseReport),
            yAxisLabel: this._translateService.instant(yAxisLabel),
            yAxisLabelRight: this._translateService.instant(yAxisLabelRight),
            valueFormatter: (value: number) => size(value),
        };
    }

    #getCapacityDatasets(): ChartDataset[] {
        const monthlyData = this.licenseService.selectedLicenseReport.monthlyData;

        const datasets: ChartDataset[] = [
            {
                data: monthlyData.flatMap(d => d.analysedFilesBytes),
                label: this._translateService.instant('license-info-screen.analysis-capacity-usage.analyzed-per-month'),
                type: 'bar',
                backgroundColor: ChartBlue,
                borderColor: ChartBlue,
                order: 2,
            },

            {
                data: monthlyData.map(
                    (month, monthIndex) =>
                        month.analysedFilesBytes + monthlyData.slice(0, monthIndex).reduce((acc, curr) => acc + curr.analysedFilesBytes, 0),
                ),
                label: this._translateService.instant('license-info-screen.analysis-capacity-usage.analyzed-cumulative'),
                yAxisID: 'y1',
                order: 1,
                ...getLineConfig(ChartGreen, true, false),
            },
        ];

        if (this.licenseService.analysisCapacityBytes > 0) {
            datasets.push({
                data: monthlyData.flatMap(() => this.licenseService.analysisCapacityBytes),
                label: this._translateService.instant('license-info-screen.analysis-capacity-usage.licensed'),
                ...getLineConfig(ChartRed, true, false),
                yAxisID: 'y1',
                order: 1,
            });
        }

        return datasets;
    }
}
