import { Component, DestroyRef, inject, OnInit, OnDestroy } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatBadgeModule } from '@angular/material/badge';
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 { HistoryTableComponent } from '@components/shared/helpers-jump';
import { PieChartComponent } from '@components/shared/helpers-chart';
import { UtilitiesService, AeronefsService } from '@services';
import { AeronefByImat, AeronefByYear } from '@models';

@Component({
    selector: 'app-aeronefs-pie',
    imports: [
        MatBadgeModule,
        MatButtonModule,
        MatCardModule,
        MatDividerModule,
        MatExpansionModule,
        MatIconModule,
        MatMenuModule,
        HistoryTableComponent,
        PieChartComponent,
    ],
    templateUrl: './aeronefs-pie.component.html',
})
export class AeronefsPieComponent implements OnInit, OnDestroy {
    private _aeronefsService = inject(AeronefsService);
    private _utilitiesService = inject(UtilitiesService);
    menuItems = inject(MenuItems);

    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 destroyRef = inject(DestroyRef);

    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.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;
            });
        });
    }
}
