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

class ReservationServ {
    constructor () {
        this.axios = global.axiosInstances.axiosAfcInstance;
        this.axiosRms = global.axiosInstances.axiosRmsInstance

        this.apiTimeout = 1000;
    }

    version = async () => {
        try {
            const xmlBuilder = new xml2js.Builder({xmldec: {version: '1.0', encoding: 'UTF-8'} });
            const xml = await xmlBuilder.buildObject({heartbeat: {deviceId: 'MST'}});
            const res = await this.axios.post('device_comm/v1/MST/heartbeat', xml, {timeout: this.apiTimeout, headers: {'Content-Type': 'application/xml'}});
            if (typeof res === 'undefined' || res.status >= 400) { return {head: null}; }
            return await xml2js.parseStringPromise(res, { explicitRoot: false, explicitArray: false });
        } catch (err) {
            log.error(err);
        }
    }

    login = async (credentials) => {
        try {
            const res = await this.axiosRms.post('login', credentials, {timeout: this.apiTimeout});
            if (typeof res == 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getIncludedLuggage = async (cartId, sessionId) => {
        try {
            const res = await this.axiosRms.get('luggage/included-luggage', {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getCart = async (cartId, sessionId) => {
        try {
            const res = await this.axiosRms.get('carts/'+cartId, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    deleteCart = async (cartId, sessionId) => {
        try {
            const res = await this.axiosRms.delete('carts/'+cartId, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    confirmCart = async (cartId, sessionId) => {
        try {
            const res = await this.axiosRms.put('carts/'+cartId+'/confirm', {}, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    confirmCartPayment = async (cartId, sessionId) => {
        try {
            const res = await this.axiosRms.put('carts/'+cartId+'/confirm-payment', {}, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    resetCart = async (cartId, sessionId) => {
        try {
            const res = await this.axiosRms.put('carts/'+cartId+'/reset', {}, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    passengerCancelation = async (cartId, sessionId, passengerId) => {
        try {
            const res = await this.axiosRms.delete('carts/'+cartId+'/passenger-cancelation/'+passengerId, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    removeLuggage = async (cartId, sessionId, journeyId, luggageId) => {
        try {
            const res = await this.axiosRms.put('carts/'+cartId+'/remove-luggage/'+journeyId+'/'+luggageId, {}, {timeout: this.apiTimeout, headers: {'Authorization': sessionId}});
            if (typeof res == 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

}

module.exports = ReservationServ;
