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 { BarHorizontalChartComponent } from '@components/shared/helpers-chart';
import { HistoryTableComponent } from '@components/shared/helpers-jump';
import { UtilitiesService, AeronefsService } from '@services';
import { AeronefByImat, AeronefByYear } from '@models';

@Component({
    selector: 'app-aeronefs-bar',
    standalone: true,
    imports: [
        MatButtonModule, MatCardModule, MatDividerModule,
        MatExpansionModule, MatIconModule, MatMenuModule,
        BarHorizontalChartComponent, HistoryTableComponent
    ],
    templateUrl: './aeronefs-bar.component.html'
})
export class AeronefsBarComponent 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: 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 displayCharts = false;
    public destroyRef = inject(DestroyRef);

    constructor(
        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().pipe(takeUntilDestroyed(this.destroyRef));
        this._aeronefByImat = aeronefs$.subscribe((aggregate: Array<AeronefByImat>) => {
            this._aeronefsByImat = aggregate.map((row: AeronefByImat) => {
                //this.chartConfig.barChartData.labels!.push(`${row.aeronef} ${row.imat}`);
                this.seriesName.push(`${row.aeronef} ${row.imat}`);
                this.seriesValue.push(row.count);
                return row;
            });
            this._loadAeronefByYear();
        });
    }

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