import { Component, OnInit, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { NgChartsModule } from 'ng2-charts';

import { Observable, Subscription } from 'rxjs';

import { MenuItems, HistoryTableComponent } from 'src/app/components/shared';
import { UtilitiesService, CanopiesService } from 'src/app/core/services';
import { CanopyBySize, CanopyByYear, DoughnutConfig } from 'src/app/core/models';

@Component({
    standalone: true,
    imports: [
        CommonModule,
        MatButtonModule, MatCardModule, MatExpansionModule, MatIconModule, MatMenuModule,
        NgChartsModule,
        HistoryTableComponent
    ],
    selector: 'huapp-canopy-sizes',
    templateUrl: './canopy-sizes.component.html'
})
export class CanopySizesComponent implements OnInit, OnDestroy {
    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: any[] = [];
    public seriesColor: string[] = this._utilitiesService.getChartColors();
    public displayCharts = false;
    public chartConfig: DoughnutConfig = this._utilitiesService.getDoughnutChartConfig();

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

    ngOnInit() {
        this._loadCanopyBySize();
    }

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

    private _loadCanopyBySize(): void {
        const canopies$: Observable<Array<CanopyBySize>> = this._canopiesService.getAllBySize();
        this._canopyBySize = canopies$.subscribe((aggregate: Array<CanopyBySize>) => {
            this._canopiesBySize = aggregate.map((row: CanopyBySize) => {
                this.chartConfig.doughnutChartLabels.push(row._id.taille.toString());
                this.chartConfig.doughnutChartDatasets[0].data.push(row.count);
                return row;
            });
            this.seriesName = [...this.chartConfig.doughnutChartLabels];
            this.seriesValue = [...this.chartConfig.doughnutChartDatasets[0].data];
            this.displayCharts = true;
            this._loadCanopyByYear();
        });
    }

    private _loadCanopyByYear(): void {
        this.seriesHeader = ["2018", "2019", "2020", "2021", "2022", "2023"];
        let values: Array<number> = this.seriesHeader.map(() => 0);
        this.seriesName.forEach(() => {
            this.seriesRow.push([...values]);
        });
        const dropzones$: Observable<Array<CanopyByYear>> = this._canopiesService.getAllByYear();
        this._canopyByYear = dropzones$.subscribe((aggregate: Array<CanopyByYear>) => {
            this._canopiesByYear = aggregate.map((row: CanopyByYear) => {
                let taille: string = row._id.taille.toString();
                let year: string = row._id.year.toString();
                this.seriesRow[this.seriesName.indexOf(taille)][this.seriesHeader.indexOf(year)] = row.count;
                return row;
            });
        });
    }
}
