import { Component, OnInit, OnDestroy } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ActivatedRoute, Data, Router } from '@angular/router';

import { Observable, Subscription } from 'rxjs';
import { take } from 'rxjs/operators';

import { User, Errors, Aeronef, AeronefByImat, Canopy, CanopyModelBySize, DropZone, DropZoneByYear } from 'src/app/core/models';
//import { AeronefsService, CanopiesService, DropZonesService, UserService } from 'src/app/core/services';
import { UserService } from 'src/app/core/services';
import { ShowAuthedDirective } from 'src/app/components/shared/show-authed.directive';
import {
    AeronefsComponent, AeronefsBarComponent,
    CanopyModelsComponent, CanopySizesComponent,
    DropZonesComponent, DropZonesBarComponent,
    JumpsByMonthComponent
} from 'src/app/components/shared/dashboard-components';

@Component({
    standalone: true,
    imports: [
        ShowAuthedDirective,
        AeronefsComponent, AeronefsBarComponent,
        CanopyModelsComponent, CanopySizesComponent,
        DropZonesComponent, DropZonesBarComponent,
        JumpsByMonthComponent
    ],
    selector: 'huapp-dashboard',
    templateUrl: './dashboard.component.html',
    styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit, OnDestroy {
    private _currentUser: Subscription = new Subscription();
    private _data: Subscription = new Subscription();
    /*
    private _aeronefs: Subscription = new Subscription();
    private _canopies: Subscription = new Subscription();
    private _dropzones: Subscription = new Subscription();
    */
    public title = 'Dashboard';
    public errors!: Errors;
    public isAuthenticated = false;
    public canModify = false;
    public aeronefAggregate!: Array<AeronefByImat>;
    public aeronefs!: Array<Aeronef>;
    public aeronefsCount = 0;
    public canopyAggregate!: Array<CanopyModelBySize>;
    public canopies!: Array<Canopy>;
    public canopiesCount = 0;
    public dropzoneAggregate!: Array<DropZoneByYear>;
    public dropzones!: Array<DropZone>;
    public dropzonesCount = 0;

    constructor(
        private route: ActivatedRoute,
        private router: Router,
        private titleService: Title,
        private userService: UserService
        /*
        private aeronefsService: AeronefsService,
        private canopiesService: CanopiesService,
        private dropzonesService: DropZonesService
        */
    ) {
        this._resetErrors();
    }

    ngOnInit() {
        this.titleService.setTitle(this.title);
        const data$: Observable<Data> = this.route.data.pipe(take(1));
        this._data = data$.subscribe((data: Data) => {
            this.isAuthenticated = data['isAuthenticated'];
            if (!this.isAuthenticated) {
                this.router.navigateByUrl('/login');
                return;
            }
            const currentUser$: Observable<User> = this.userService.currentUser;
            this._currentUser = currentUser$.subscribe((userData: User) => {
                this.canModify = userData.role === 'Admin';
            });
        });
        /*
        this._loadAeronefs();
        this._loadCanopies();
        this._loadDropzones();
        */

    }

    ngOnDestroy() {
        this._currentUser.unsubscribe();
        this._data.unsubscribe();
        /*
        this._aeronefs.unsubscribe();
        this._canopies.unsubscribe();
        this._dropzones.unsubscribe();
        */
    }

    private _resetErrors(): void {
        this.errors = { errors: {} };
    }

    /*
    private _loadAeronefs(): void {
        const aeronefs$: Observable<Array<AeronefByImat>> = this.aeronefsService.getAllByImat();
        this._aeronefs = aeronefs$.subscribe((aggregate: Array<AeronefByImat>) => {
            this.aeronefAggregate = aggregate;
            this.aeronefsCount = aggregate.length;
            console.log(this.aeronefAggregate);
            console.log(this.aeronefsCount);
        });
    }

    private _loadCanopies(): void {
        const canopies$: Observable<Array<CanopyModelBySize>> = this.canopiesService.getAllModelBySize();
        this._canopies = canopies$.subscribe((aggregate: Array<CanopyModelBySize>) => {
            this.canopyAggregate = aggregate;
            this.canopiesCount = aggregate.length;
            console.log(this.canopyAggregate);
            console.log(this.canopiesCount);
        });
    }

    private _loadDropzones(): void {
        const dropzones$: Observable<Array<DropZoneByYear>> = this.dropzonesService.getAllByDate();
        this._dropzones = dropzones$.subscribe((aggregate: Array<DropZoneByYear>) => {
            this.dropzoneAggregate = aggregate;
            this.dropzonesCount = aggregate.length;
            console.log(this.dropzoneAggregate);
            console.log(this.dropzonesCount);
        });
    }
    */
}
