import { MessageService } from "primeng/api";
import { Injectable } from "@angular/core";
import { Title } from "@angular/platform-browser";
import * as moment from "moment";
import { FileUpload } from "primeng/fileupload";
import jsPDF from "jspdf";
import * as FileSaver from "file-saver";
import autoTable from 'jspdf-autotable';
import { API } from "../api.service";
import { ValidationService } from "../../common/validation.service";

@Injectable()
export class FlightContractService {
    constructor(
        public ApiServ: API,
        public validationService: ValidationService,
        public titleService: Title
    ) {
    }


    ddLoader = {
        AirLine: false,
        Currency: false,
        origin: false,
        destination: false,
        ToAirport: false,
        FormAirport: false,
        AirCraft: false,
    }
    timer;
    AirlineList = [];
    AirportList = [];
    AllAirCraftList = [];
    getAirLineBySearch(val) {
        if (val.length >= 2) {
            this.ddLoader.AirLine = true;
            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                let request = {
                    "langcode": "en",
                    "query": val
                }
                this.ApiServ.AdminGoPost(`/MasterSearch/GetAllAirLine`, request).subscribe(res => {
                    if (res) {
                        this.AirlineList = res?.Result;
                        this.ddLoader.AirLine = false;
                    }
                });
            }, 500);
        };
        if (!val) {
            this.AirlineList = [];
        }
    };

    GetAirportList(val, name) {
        if (val.length >= 2) {
            this.ddLoader[name] = true;
            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                this.ApiServ.AdminGOGet(`/Airport/GetList/en/` + val).subscribe(res => {
                    if (res) {
                        this.AirportList = res;
                        this.ddLoader[name] = false;
                    }
                });
            }, 500);
        };
        if (!val) {
            this.AirlineList = [];
        }
    };
    getAircraftList(val) {
        let repObj = {
            aircraft_name: val
        };
        if (val.length >= 3) {
            this.ddLoader.AirCraft = true;
            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                this.ApiServ.Post(`/MasterSearch/GetAllAircraftType`, repObj).subscribe(res => {
                    if (res) {
                        this.AllAirCraftList = res.data;
                        this.ddLoader.AirCraft = false;
                    }
                });
            }, 500);
        }
    };
    GetAirportsByAirportCode(data) {
        let obj = {
            airport_code: data
        }
        this.ApiServ.AdminGoPost('/Airport/GetAirportsByAirportCode', obj).subscribe(res => {
            if (res) {
                if (res.Result && res.Result.length > 0) {
                    this.AirportList = res.Result;
                    this.AirportList = this.AirportList.map(e => ({ 'ac': e.airport_code, 'ct': e.city_name }));
                }
                setTimeout(() => {
                    this.AirportList = [];
                }, 500);
            }
        });
    };
    GetAirlineByAirlineCode(data) {
        let obj = {
            airline_code: data
        }
        this.ApiServ.AdminGoPost('/MasterSearch/GetAirlineByAirlineCode', obj).subscribe(res => {
            if (res) {
                this.AirlineList = res.Result;
                setTimeout(() => {
                    this.AirlineList = [];
                }, 500);
            }
        });
    };
    GetAircraftTypeById(data) {
        let obj = {
            aircraft_type_id: data
        }
        this.ApiServ.Post('/MasterSearch/GetAircraftTypeById', obj).subscribe(res => {
            if (res) {
                this.AllAirCraftList = res.data;
                setTimeout(() => {
                    this.AllAirCraftList = [];
                }, 500);
            }
        });
    };
}