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 { CanopyBySize, CanopyByYear } from '@models';

@Component({
    selector: 'app-canopies-sizes',
    imports: [
        MatButtonModule,
        MatCardModule,
        MatDividerModule,
        MatExpansionModule,
        MatIconModule,
        MatMenuModule,
        PieChartComponent,
        HistoryTableComponent,
    ],
    templateUrl: './canopies-sizes.component.html',
})
export class CanopiesSizesComponent implements OnInit, OnDestroy {
    private _canopiesService = inject(CanopiesService);
    private _utilitiesService = inject(UtilitiesService);
    menuItems = inject(MenuItems);

    private _canopyBySize: Subscription = new Subscription();
    private _canopyByYear: Subscription = new Subscription();
    private _canopiesBySize!: Array<CanopyBySize>;
    private _canopiesByYear!: Array<CanopyByYear>;
    public title: string = 'Tailles de voile';
    public subtitle: string = 'Nombre total de sauts par taille';
    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);

    ngOnInit() {
        this._loadCanopyBySize();
    }

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

    private _loadCanopyBySize(): void {
        const canopies$: Observable<Array<CanopyBySize>> = this._canopiesService
            .getAllBySize()
            .pipe(takeUntilDestroyed(this.destroyRef));
        this._canopyBySize = canopies$.subscribe((aggregate: Array<CanopyBySize>) => {
            this._canopiesBySize = aggregate.map((row: CanopyBySize) => {
                this.seriesName.push(row.taille.toString());
                this.seriesValue.push(row.count);
                return row;
            });
            this._loadCanopyByYear();
        });
    }

    private _loadCanopyByYear(): 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<CanopyByYear>> = this._canopiesService
            .getAllBySizeByYear()
            .pipe(takeUntilDestroyed(this.destroyRef));
        this._canopyByYear = dropzones$.subscribe((aggregate: Array<CanopyByYear>) => {
            this._canopiesByYear = aggregate.map((row: CanopyByYear) => {
                const taille: string = row.taille.toString();
                const year: string = row.year.toString();
                this.seriesRow[this.seriesName.indexOf(taille)][this.seriesHeader.indexOf(year)] = row.count;
                return row;
            });
        });
    }
}
