import { Component, OnInit, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router, RouterLink, RouterModule } from '@angular/router';
import { MatBadgeModule } from '@angular/material/badge';
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, AeronefsService } from 'src/app/core/services';
import { AeronefByImat, AeronefByYear, DoughnutConfig } from 'src/app/core/models';

@Component({
    standalone: true,
    imports: [
        CommonModule, RouterModule,
        MatBadgeModule, MatButtonModule, MatCardModule, MatExpansionModule, MatIconModule, MatMenuModule,
        NgChartsModule,
        HistoryTableComponent
    ],
    selector: 'huapp-aeronefs',
    templateUrl: './aeronefs.component.html'
})
export class AeronefsComponent implements OnInit, OnDestroy {
    private _aeronefByImat: Subscription = new Subscription();
    private _aeronefByYear: Subscription = new Subscription();
    private _aeronefsByImat!: Array<AeronefByImat>;
    private _aeronefsByYear!: Array<AeronefByYear>;
    public title: string = 'Aéronefs';
    public subtitle: string = 'Nombre total de sauts par aéronef';
    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 router: Router,
        private _aeronefsService: AeronefsService,
        private _utilitiesService: UtilitiesService,
        public menuItems: MenuItems
    ) { }

    ngOnInit() {
        this._loadAeronefByImat();
    }

    ngOnDestroy() {
        this._aeronefByImat.unsubscribe();
        this._aeronefByYear.unsubscribe();
    }

    private _loadAeronefByImat(): void {
        const aeronefs$: Observable<Array<AeronefByImat>> = this._aeronefsService.getAllByImat();
        this._aeronefByImat = aeronefs$.subscribe((aggregate: Array<AeronefByImat>) => {
            this._aeronefsByImat = aggregate.map((row: AeronefByImat) => {
                this.chartConfig.doughnutChartLabels.push(`${row._id.aeronef} ${row._id.imat}`);
                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._loadAeronefByYear();
        });
    }

    private _loadAeronefByYear(): 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<AeronefByYear>> = this._aeronefsService.getAllByDate();
        this._aeronefByYear = dropzones$.subscribe((aggregate: Array<AeronefByYear>) => {
            this._aeronefsByYear = aggregate.map((row: AeronefByYear) => {
                let aeronef: string = row._id.aeronef;
                let imat: string = row._id.imat;
                let year: string = row._id.year.toString();
                this.seriesRow[this.seriesName.indexOf(`${aeronef} ${imat}`)][this.seriesHeader.indexOf(year)] = row.count;
                return row;
            });
        });
    }
}
