const fs = require('fs');
const path = require('path');
const log = require('loglevel');

class BookingServ {
    constructor () {
        this.axios = global.axiosInstances.axiosAfcInstance;

        this.apiTimeout = 1000;
    }

    createBooking = async (booking) => {
        try {
            const res = await this.axios.post('booking/create', booking, {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    getBookingByReference = async (reference) => {
        try {
            const res = await this.axios.get('booking/by-reference/'+reference, {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getBookingByFareItemIssuedNumber = async (fareItemIssuedNumber) => {
        try {
            const res = await this.axios.get('booking/by-fareItemIssuedNumber/'+fareItemIssuedNumber, {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getBookingByDocumentReference = async (documentReference) => {
        try {
            const res = await this.axios.get('booking/by-documentReference/'+documentReference, {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getBookingByETicketReference = async (eTicketReference) => {
        try {
            const res = await this.axios.get('booking/by-eTicketReference/'+eTicketReference, {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getBookingByPassengerFareItemIssuedNumber = async (fareItemIssuedNumber) => {
        try {
            const res = await this.axios.get('booking/passenger/'+fareItemIssuedNumber+'/by-fareItemIssuedNumber', {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    cancelBooking = async (reference, reasonId) => {
        try {
            const res = await this.axios.delete('booking/'+reference+'/cancel/'+reasonId, {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    getTicketTransactionLog = async (fareItemIssuedNumber, timestampFilter) => {
        try {
            const res = await this.axios.get('webportal/ticket/'+fareItemIssuedNumber+'/transaction-log?filter=timestamp:GTE:\''+timestampFilter+'\'', {timeout: this.apiTimeout});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

}

module.exports = BookingServ;
