const axios = require('axios');
const https = require('https');
const log = require('loglevel');

const axiosPosInstance = axios.create({
    baseURL: process.env.PROTOCOL + '://' + process.env.HOST + ':' + process.env.PORT_BACKEND + '/api',
    httpsAgent: new https.Agent({rejectUnauthorized: false})
});

axiosPosInstance.interceptors.request.use((request) => {
    // log.info('axios req', request);
    return request;
}, (error) => {
    return error.response;
});

axiosPosInstance.interceptors.response.use((response) => {
    // log.info('axios res', response.data);
    if (typeof response === 'undefined') {
        log.error('Response was empty, check if server you are trying to access is online.');
    }
    return response.data;
}, (error) => {
    // log.info('axios res error', error.request, error.response);
    log.error('Response error ('+((!!error.request.path)?error.request.path:error.request._currentRequest.path)+' '+((!!error.request.method)?error.request.method:error.request._currentRequest.method)+' '+((!!error.response && !!error.response.status)?error.response.status:error.response)+').');
    return error.response;
});

const afcApiKey = (process.env.POS_DEVELOPMENT_MODE == 'true') ? process.env.CLIENT_APIKEY : new Buffer.from(process.env.CLIENT_APIKEY, 'base64').toString('ascii');

const axiosAfcInstance = axios.create({
    baseURL: process.env.CLIENT_APIURL + '/rest/afc/integration',
    headers: {
        'Authorization': 'ApiKey ' + afcApiKey
    },
    httpsAgent: new https.Agent({rejectUnauthorized: false})
});

axiosAfcInstance.interceptors.request.use((request) => {
    // log.info('axios req', request);
    return request;
}, (error) => {
    return error.response;
});

axiosAfcInstance.interceptors.response.use((response) => {
    // log.info('axios res', response.data);
    if (typeof response === 'undefined') {
        log.error('Response was empty, check if server you are trying to access is online.');
    }
    return response.data;
}, (error) => {
    // log.info('axios res error', error.request, error.response);
    log.error('Response error ('+((!!error.request.path)?error.request.path:error.request._currentRequest.path)+' '+((!!error.request.method)?error.request.method:error.request._currentRequest.method)+' '+((!!error.response && !!error.response.status)?error.response.status:error.response)+').');
    return error.response;
});

const sclibApiKey = 'dd119931-ba0f-4aeb-83f6-0963167a7c55';
let axiosSclibInstance;
if (process.env.CLIENT_SECURE_ACCESS_MODULE == 'true') {
    axiosSclibInstance = axios.create({
        baseURL: process.env.PROTOCOL + '://' + process.env.HOST + ':' + process.env.PORT_SCLIB + '/resource/smartcard',
        headers: {
            'Authorization': 'Bearer ' + sclibApiKey
        },
        httpsAgent: new https.Agent({rejectUnauthorized: false})
    });

    axiosSclibInstance.interceptors.request.use((request) => {
        // log.info('axios req', request);
        return request;
    }, (error) => {
        return error.response;
    });

    axiosSclibInstance.interceptors.response.use((response) => {
        // log.info('axios res', response.data);
        if (typeof response === 'undefined') {
            log.error('Response was empty, check if server you are trying to access is online.');
        }
        return response;
    }, (error) => {
        // log.info('axios res error', error.request, error.response);
        log.error('Response error ('+((!!error.request.path)?error.request.path:error.request._currentRequest.path)+' '+((!!error.request.method)?error.request.method:error.request._currentRequest.method)+' '+((!!error.response && !!error.response.status)?error.response.status:error.response)+').');
        return error.response;
    });
}

const rmsApiKey = 'jpIlK8YWTllSgs4TQEmgeM2pT87ms5IlYabZNdqoqCT59gZXpPwEAXRT13AbLJKqxriWVx3wcp3hkPfgWqnBSupy7VjlJumf8TXiAVPETvzydONkbR047muJgvKdFP46';
let axiosRmsInstance;
if (process.env.CLIENT_RESERVATION_MODULE == 'true') {
    axiosRmsInstance = axios.create({
        baseURL: process.env.CLIENT_RMS_URL + '/api/v1',
        headers: {
            'Authorization': 'ApiKey ' + rmsApiKey
        },
        httpsAgent: new https.Agent({rejectUnauthorized: false})
    });

    axiosRmsInstance.interceptors.request.use((request) => {
        // log.info('axios req', request);
        return request;
    }, (error) => {
        return error.response;
    });

    axiosRmsInstance.interceptors.response.use((response) => {
        // log.info('axios res', response.data);
        if (typeof response === 'undefined') {
            log.error('Response was empty, check if server you are trying to access is online.');
        }
        return response.data;
    }, (error) => {
        // log.info('axios res error', error.request, error.response);
        log.error('Response error ('+((!!error.request.path)?error.request.path:error.request._currentRequest.path)+' '+((!!error.request.method)?error.request.method:error.request._currentRequest.method)+' '+((!!error.response && !!error.response.status)?error.response.status:error.response)+').');
        return error.response;
    });
}

module.exports = {
    axiosPosInstance,
    axiosAfcInstance,
    axiosSclibInstance,
    axiosRmsInstance
};
