import { Component, OnInit } from "@angular/core";
import { RouterLink } from "@angular/router";
import { TranslateModule, TranslateService } from "@ngx-translate/core";
import { Observable, forkJoin } from "rxjs";
import { MatIconModule } from "@angular/material/icon";

import { AircraftService } from "../../services/aircraft.service";
import { AuthenticationService } from "../../services/authentication.service";
import { DropzoneService } from "../../services/dropzone.service";
import { GearService } from "../../services/gear.service";
import { JumpTypeService } from "../../services/jump-type.service";
import { ServiceComm } from "../../services/service-comm.service";

@Component({
    selector: "app-default",
    templateUrl: "./default.component.html",
    styleUrls: ["./default.component.css"],
    imports: [TranslateModule, MatIconModule, RouterLink],
})
export class DefaultComponent implements OnInit {
    constructor(
        private serviceComm: ServiceComm,
        private translateService: TranslateService,
        private authenticationService: AuthenticationService,
        private serviceApiAircraft: AircraftService,
        private serviceApiJumpType: JumpTypeService,
        private serviceApiDropzone: DropzoneService,
        private serviceApiGear: GearService,
    ) {}

    ngOnInit() {
        this.authenticationService.alwaysLogin();

        this.putToCacheRefDatas().subscribe(() => {
            console.log("Push to cache the referentiel datas");
        });
        this.updateTitle();
        this.serviceComm.forceTranslateTitle.subscribe((data) => {
            if (data === true) {
                this.updateTitle();
            }
        });
    }

    private putToCacheRefDatas(): Observable<any[]> {
        var aircraftResp = this.serviceApiAircraft.getListOfAircrafts(false);
        var jumpTypeResp = this.serviceApiJumpType.getListOfJumpTypes();
        var dzResp = this.serviceApiDropzone.getListOfDropZones(false);
        var gearResp = this.serviceApiGear.getListOfGears();

        return forkJoin([aircraftResp, jumpTypeResp, dzResp, gearResp]);
    }

    private updateTitle() {
        this.translateService.get("Default_Title").subscribe((data) => {
            this.serviceComm.updatedComponentTitle(data);
        });
    }
}
