import { Component, DestroyRef, inject, OnInit, OnDestroy} from '@angular/core';
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { Observable, Subscription } from 'rxjs';

import { MenuItems } from '@components/shared';
import { PieChartComponent } from '@components/shared/helpers-chart';
import { HistoryTableComponent } from '@components/shared/helpers-jump';
import { UtilitiesService, CanopiesService } from '@services';
import { CanopyModelBySize, CanopyModelByYear } from '@models';

@Component({
    selector: 'app-canopies-models',
    standalone: true,
    imports: [
        MatButtonModule, MatCardModule, MatDividerModule,
        MatExpansionModule, MatIconModule, MatMenuModule,
        PieChartComponent, HistoryTableComponent
    ],
    templateUrl: './canopies-models.component.html'
})
export class CanopiesModelsComponent implements OnInit, OnDestroy {
    private _canopyBySize: Subscription = new Subscription();
    private _canopyByYear: Subscription = new Subscription();
    private _canopiesBySize!: Array<CanopyModelBySize>;
    private _canopiesByYear!: Array<CanopyModelByYear>;
    public title: string = 'Modèles de voile';
    public subtitle: string = 'Nombre total de sauts par modèle';
    public seriesHeader: string[] = [];
    public seriesName: string[] = [];
    public seriesValue: number[] = [];
    public seriesRow: Array<Array<number>> = [];
    public seriesColor: {
        backgroundColor: string[],
        borderColor: string[]
    } = {
        backgroundColor: this._utilitiesService.getSeriesColors(0.8, 'all'),
        borderColor: this._utilitiesService.getSeriesColors(1, 'all')
    };
    public seriesColorClass: string[] = this._utilitiesService.getChartColors();
    public destroyRef = inject(DestroyRef);

    constructor(
        private _canopiesService: CanopiesService,
        private _utilitiesService: UtilitiesService,
        public menuItems: MenuItems
    ) { }

    ngOnInit() {
        this._loadCanopyModelBySize();
    }

    ngOnDestroy() {
        this._canopyBySize.unsubscribe();
        this._canopyByYear.unsubscribe();
    }

    private _loadCanopyModelBySize(): void {
        const canopies$: Observable<Array<CanopyModelBySize>> = this._canopiesService.getAllBySizeByModel().pipe(takeUntilDestroyed(this.destroyRef));
        this._canopyBySize = canopies$.subscribe((aggregate: Array<CanopyModelBySize>) => {
            this._canopiesBySize = aggregate.map((row: CanopyModelBySize) => {
                this.seriesName.push(`${row.taille.toString()} - ${row.voile}`);
                this.seriesValue.push(row.count);
                return row;
            });
            this._loadCanopyModelByYear();
        });
    }

    private _loadCanopyModelByYear(): void {
        this.seriesHeader = ['2018', '2019', '2020', '2021', '2022', '2023', '2024'];
        const values: Array<number> = this.seriesHeader.map(() => 0);
        this.seriesName.forEach(() => {
            this.seriesRow.push([...values]);
        });
        const dropzones$: Observable<Array<CanopyModelByYear>> = this._canopiesService.getAllBySizeByModelByYear().pipe(takeUntilDestroyed(this.destroyRef));
        this._canopyByYear = dropzones$.subscribe((aggregate: Array<CanopyModelByYear>) => {
            this._canopiesByYear = aggregate.map((row: CanopyModelByYear) => {
                const voile: string = row.voile;
                const taille: string = row.taille.toString();
                const year: string = row.year.toString();
                this.seriesRow[this.seriesName.indexOf(`${taille} - ${voile}`)][this.seriesHeader.indexOf(year)] = row.count;
                return row;
            });
        });
    }
}
