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, tap } from 'rxjs/operators';
import type { DonutChartConfig, ILicenseReport } from '@red/domain';
import { ChartDataset } from 'chart.js';
import { 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-storage',
    templateUrl: './license-storage.component.html',
    styleUrls: ['./license-storage.component.scss'],
})
export class LicenseStorageComponent {
    readonly formatSize = size;
    uploadedBytesCapacityPercentage = -1;
    donutChartConfig: DonutChartConfig[] = [];
    readonly data$ = this.licenseService.licenseData$.pipe(
        map(() => this.licenseService.currentLicenseReport),
        tap(license => {
            this.uploadedBytesCapacityPercentage = this.#getUploadedBytesCapacityPercentage(license);
            this.donutChartConfig = this.#getDonutChartConfig(license);
        }),
        map(license => ({
            datasets: this.#getDatasets(license),
            labels: getLabelsFromLicense(license),
        })),
    );
    readonly size = size;

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

    #getUploadedBytesCapacityPercentage(license: ILicenseReport): number {
        return this.licenseService.uploadedBytesCapacity
            ? (license.totalFilesUploadedBytes / this.licenseService.uploadedBytesCapacity) * 100
            : -1;
    }

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

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

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