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

@Component({
    selector: 'red-license-retention-capacity',
    templateUrl: './license-retention-capacity.component.html',
    styleUrls: ['./license-retention-capacity.component.scss'],
})
export class LicenseRetentionCapacityComponent {
    readonly formatSize = size;
    readonly donutChartConfig$ = this.licenseService.selectedLicense$.pipe(
        map(() => this.licenseService.selectedLicenseReport),
        map(license => this.#getDonutChartConfig(license)),
    );
    readonly data$ = this.licenseService.selectedLicense$.pipe(
        map(() => this.licenseService.selectedLicenseReport),
        map(license => ({
            datasets: this.#getDatasets(license),
            labels: getLabelsFromLicense(license),
        })),
    );
    readonly size = size;

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

    #getDonutChartConfig(license: ILicenseReport): DonutChartConfig[] {
        return [
            {
                value: license.activeFilesUploadedBytes,
                color: ChartGreen,
                label: this._translateService.instant(_('license-info-screen.retention-capacity-usage.active-documents')),
            },
            {
                value: license.archivedFilesUploadedBytes,
                color: ChartBlue,
                label: this._translateService.instant(_('license-info-screen.retention-capacity-usage.archived-documents')),
            },
            {
                value: license.trashFilesUploadedBytes,
                color: ChartRed,
                label: this._translateService.instant(_('license-info-screen.retention-capacity-usage.trash-documents')),
            },
            {
                value: Math.max(this.licenseService.retentionCapacityBytes - license.totalFilesUploadedBytes, 0),
                color: ChartGrey,
                label: this._translateService.instant(_('license-info-screen.retention-capacity-usage.unused')),
            },
        ];
    }

    #getDatasets(license: ILicenseReport): ChartDataset[] {
        const monthlyData = license.monthlyData;

        return [
            {
                data: monthlyData.flatMap(d => d.activeFilesUploadedBytes),
                label: this._translateService.instant('license-info-screen.retention-capacity-usage.active-documents'),
                ...getLineConfig(ChartGreen, false, 'origin'),
                stack: 'storage',
            },
            {
                data: monthlyData.flatMap(d => d.archivedFilesUploadedBytes),
                label: this._translateService.instant('license-info-screen.retention-capacity-usage.archived-documents'),
                ...getLineConfig(ChartBlue, false, '-1'),
                stack: 'storage',
            },
            {
                data: monthlyData.flatMap(d => d.trashFilesUploadedBytes),
                label: this._translateService.instant('license-info-screen.retention-capacity-usage.trash-documents'),
                ...getLineConfig(ChartRed, false, '-1'),
                stack: 'storage',
            },
            {
                data: monthlyData.flatMap(d => d.activeFilesUploadedBytes + d.archivedFilesUploadedBytes + d.trashFilesUploadedBytes),
                label: this._translateService.instant('license-info-screen.retention-capacity-usage.used-capacity'),
                ...getLineConfig(ChartBlack, true, false),
                borderWidth: 2,
            },
        ];
    }
}
