import { Inject, Injectable } from '@angular/core';
import { FlightStatusLegacy } from '@app/shared/enumerators';
import { AppSettings, FlightModel, getFirstLeg, getLastLeg } from '@app/shared/models-legacy';
import { APP_SETTINGS, WINDOW } from '@app/shared/services';
import * as moment from 'moment';

@Injectable({ providedIn: 'root' })
export class BuyTicketLogic {
    constructor(@Inject(APP_SETTINGS) public settings: AppSettings, @Inject(WINDOW) private window$: Window) {}
    isAnyAvailable(...flights: (FlightModel | undefined)[]) {
        flights = (flights || []).filter((f) => !!f);

        if (!flights.length) return false;

        for (const flight of flights) if (this.isAvailable(flight)) return true;

        return false;
    }

    isAvailable(flight?: FlightModel) {
        const { min, max } = this.settings.buyPeriod;

        if (!flight) return false;
        if (flight.status === FlightStatusLegacy.cancelled) return false;
        if (flight.status == FlightStatusLegacy.inFlight) return false;

        const date = getFirstLeg(flight).departure.times.scheduled.utc;

        const now = moment();

        const from = moment(date).add(max, 'hour'); // max before flight
        const to = moment(date).add(min, 'hour'); // min before flight

        if (from < now && now < to) return true;

        // console.log(`Buy ticket button is not available. departure utc: ${date}, from: ${from}, now: ${now}, to: ${to}`);
        return false;
    }

    private open(url: string) {
        this.window$.open(url, '_blank').focus();
    }

    public buyAll(direct: FlightModel, returning?: FlightModel) {
        if (!direct) return;

        const url = returning ? this.getLinkWithReturn(direct, returning) : this.getLink(direct);

        this.open(url);
    }
    private getLink(flight: FlightModel) {
        const firstLeg = getFirstLeg(flight);
        const lastLeg = getLastLeg(flight);

        const depAirport = firstLeg.departure.scheduled.airportCode;
        const arrAirport = lastLeg.arrival.scheduled.airportCode;
        const date = moment(firstLeg.departure.times.scheduled.local).format('YYYYMMDD');
        const params = `${depAirport}.${date}.${arrAirport}`;

        return `https://www.aeroflot.ru/sb/app/ru-ru#/search?adults=1&cabin=economy&children=0&infants=0&routes=${params}&autosearch=Y`;
    }

    private getLinkWithReturn(flight1: FlightModel, flight2: FlightModel) {
        const firstLeg1 = getFirstLeg(flight1);
        const lastLeg1 = getLastLeg(flight1);

        const depAirport1 = firstLeg1.departure.scheduled.airportCode;
        const arrAirport1 = lastLeg1.arrival.scheduled.airportCode;
        const date1 = moment(firstLeg1.departure.times.scheduled.local).format('YYYYMMDD');

        const firstLeg2 = getFirstLeg(flight2);
        const lastLeg2 = getLastLeg(flight2);

        const depAirport2 = firstLeg2.departure.scheduled.airportCode;
        const arrAirport2 = lastLeg2.arrival.scheduled.airportCode;
        const date2 = moment(firstLeg2.departure.times.scheduled.local).format('YYYYMMDD');

        const params = `${depAirport1}.${date1}.${arrAirport1}-${depAirport2}.${date2}.${arrAirport2}`;

        return `https://www.aeroflot.ru/sb/app/ru-ru#/search?adults=1&cabin=economy&children=0&infants=0&routes=${params}&autosearch=Y`;
    }
}
