const fs = require('fs');
const path = require('path');
const moment = require('moment');
const _ = require('lodash'); // TODO
const log = require('loglevel');

const constants = require('../models/constants');
const constantsXml = require('../models/constants-xml');
const enums = require('../models/enums');
const enumsXml = require('../models/enums-xml');

const ReservationServ = require('../services/reservation');
const FareEnvironmentConfiguration = require('../models/fare-environment-configuration');

class ReservationCtrl {
    constructor () {
        this.service = new ReservationServ();
        this.fareEnvironmentModel = FareEnvironmentConfiguration;

        this.rmsUiUrl = process.env.CLIENT_RMS_URL;
        this.rmsUser = process.env.CLIENT_RMS_USER;
        this.rmsPass = process.env.CLIENT_RMS_PASS;
    }

    login = async (req, res) => {
        try {
            const username = this.rmsUser;
            const userpass = this.rmsPass;

            const credentials = {
                userName: username,
                password: userpass
            };

            const heartbeatResponse = await this.service.version();

            let loginResponse;

            if (!heartbeatResponse.hasOwnProperty('head') && global.currentPosData.distributionVersion == heartbeatResponse.distributionVersion) {
                let reservationLogin = await this.service.login(credentials);
                if (reservationLogin) {
                    loginResponse = {rmsUiUrl: this.rmsUiUrl, sessionId: reservationLogin.sessionId};
                }
            }

            res.status(200).json(loginResponse);
        } catch (err) {
            log.error(err);
        }
    }

    getIncludedLuggage = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;

            const luggage = await this.service.getIncludedLuggage(cartId, sessionId);

            res.status(200).json(luggage);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    getCart = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;

            const cart = await this.service.getCart(cartId, sessionId);

            res.status(200).json(cart);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    deleteCart = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;

            const cart = await this.service.deleteCart(cartId, sessionId);

            res.status(200).json(cart);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    confirmCart = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;

            const cart = await this.service.confirmCart(cartId, sessionId);

            res.status(200).json(cart);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    confirmCartPayment = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;

            const payment = await this.service.confirmCartPayment(cartId, sessionId);

            res.status(200).json(payment);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    resetCart = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;

            const cart = await this.service.resetCart(cartId, sessionId);

            res.status(200).json(cart);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    passengerCancelation = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;
            const passengerId = req.body.passengerId;

            const cart = await this.service.passengerCancelation(cartId, sessionId, passengerId);

            res.status(200).json(cart);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

    removeLuggage = async (req, res) => {
        try {
            const cartId = req.body.cartId;
            const sessionId = req.body.sessionId;
            const journeyId = req.body.journeyId;
            const luggageId = req.body.luggageId;

            const cart = await this.service.removeLuggage(cartId, sessionId, journeyId, luggageId);

            res.status(200).json(cart);
        } catch (err) {
            log.error(err);
            res.status(400).json({ error: err.message });
        }
    }

}

module.exports = ReservationCtrl;
