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

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

import { Aeronef, AeronefByImat, Canopy, CanopyModelBySize, DropZone, DropZoneByYear, Errors, Jump, User } from '@models';
//import { AeronefsService, CanopiesService, DropZonesService, UserService } from '@services';
import { JumpsService, UserService } from '@services';
import { ShowAuthedDirective } from '@components/shared/show-authed.directive';
import {
    AeronefsPieComponent, AeronefsBarComponent,
    CanopiesModelsComponent, CanopiesSizesComponent,
    DropzonesPieComponent, DropzonesBarComponent,
    JumpsByMonthComponent
} from '@components/shared/dashboard-components';

@Component({
    standalone: true,
    imports: [
        DatePipe, ShowAuthedDirective, MatDividerModule,
        AeronefsPieComponent, AeronefsBarComponent,
        CanopiesModelsComponent, CanopiesSizesComponent,
        DropzonesPieComponent, DropzonesBarComponent,
        JumpsByMonthComponent
    ],
    selector: 'app-dashboard',
    templateUrl: './dashboard.component.html',
    styleUrl: './dashboard.component.scss'
})
export class DashboardComponent implements OnInit, OnDestroy {
    private _currentUser: Subscription = new Subscription();
    private _data: Subscription = new Subscription();
    private _lastjump: Subscription = new Subscription();
    private _lastjump$: Observable<Jump> = new Observable();
    /*
    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;
    public lastJump: Jump = {} as Jump;

    constructor(
        private route: ActivatedRoute,
        private router: Router,
        private titleService: Title,
        private _userService: UserService,
        private _jumpsService: JumpsService
        /*
        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._lastjump$ = this._jumpsService.getLastJump();
            this._lastjump = this._lastjump$.subscribe((jump) => {
                this.lastJump = jump;
            });
        });
        /*
        this._loadAeronefs();
        this._loadCanopies();
        this._loadDropzones();
        */

    }

    ngOnDestroy() {
        this._currentUser.unsubscribe();
        this._data.unsubscribe();
        this._lastjump.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);
        });
    }
    */
}
