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

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

    invoiceUploadByFareItemIssuedNumber = async (fareItemIssuedNumber, invoiceData) => {
        try {
            const res = await this.axios.post('invoice/upload/fare-item-issued-number/'+fareItemIssuedNumber, invoiceData, {headers: {'Content-Type': 'multipart/form-data'}});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    invoiceUploadByOrderId = async (orderId, invoiceData) => {
        try {
            const res = await this.axios.post('invoice/upload/order-id/'+orderId, invoiceData, {headers: {'Content-Type': 'multipart/form-data'}});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    invoiceDownloadByFareItemIssuedNumber = async (fareItemIssuedNumber) => {
        try {
            const res = await this.axios.post('invoice/download/fare-item-issued-number/'+fareItemIssuedNumber, {}, {responseType: 'arraybuffer'});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return Buffer.from(res, 'binary');
        } catch (err) {
            log.error(err);
        }
    }

    invoiceDownloadByOrderId = async (orderId) => {
        try {
            const res = await this.axios.post('invoice/download/order-id/'+orderId, {}, {responseType: 'arraybuffer'});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return Buffer.from(res, 'binary');
        } catch (err) {
            log.error(err);
        }
    }

}

module.exports = InvoiceServ;
