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

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

    search = async (value) => {
        try {
            const res = await this.axios.get('account-holder/search/'+value);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    searchBase = async (value) => {
        try {
            const res = await this.axios.get('account-holder/search-base/'+value);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccount = async (accountId) => {
        try {
            const res = await this.axios.get('account-holder/registered/'+accountId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountDetails = async (accountHolderUid) => {
        try {
            const res = await this.axios.get('account-holder/'+accountHolderUid);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountDetailsShort = async (accountShortId) => {
        try {
            const res = await this.axios.get('account-holder/account/'+accountShortId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountDetailsSecond = async (accountId) => {
        try {
            const res = await this.axios.get('account-holder/registered/'+accountId+'/other');
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    resetAccess = async (uniqueId, identity) => {
        try {
            const res = await this.axios.put('account-holder/identity/'+uniqueId, identity);
            if (typeof res === 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountDetailsPhone = async (phone) => {
        try {
            const res = await this.axios.get('account-holder/phone-number/'+phone);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountDetailsEmail = async (email) => {
        try {
            const res = await this.axios.get('account-holder/email-address/'+email);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    create = async (account) => {
        try {
            const res = await this.axios.post('account-holder/account', account);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    edit = async (accountId, account) => {
        try {
            const res = await this.axios.put('account-holder/'+accountId, account);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountPhoto = async (accountId) => {
        try {
            const res = await this.axios.get('account-holder/'+accountId+'/photo', {responseType: 'arraybuffer'});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return Buffer.from(res, 'binary').toString('base64');
        } catch (err) {
            log.error(err);
        }
    }

    uploadPhoto = async (accountId, imageType, imageData) => {
        try {
            const res = await this.axios.post('account-holder/'+accountId+'/photo/'+imageType, imageData, {headers: {'Content-Type': 'multipart/form-data'}});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    addIdentity = async (identity) => {
        try {
            const res = await this.axios.post('account-holder/identity', identity);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    removeIdentity = async (identityId, version, reason) => {
        try {
            const res = await this.axios.delete('account-holder/identity/'+identityId+'/'+version+'/'+reason);
            if (typeof res === 'undefined' || res.status >= 400) { return false; }
            return true;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountContracts = async (accountHolderUid) => {
        try {
            const res = await this.axios.get('account-holder/account/contracts/'+accountHolderUid);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountContractsShort = async (accountShortId) => {
        try {
            const res = await this.axios.get('account-holder/account/contracts-by-short-id/'+accountShortId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccountPurchases = async (accountId, days) => {
        try {
            const res = await this.axios.get('account-holder/account/purchases/'+accountId+'/days/'+days);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getVerificationRequests = async (accountId) => {
        try {
            const res = await this.axios.get('account-holder/account/'+accountId+'/verification-request/all');
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getVerificationRequest = async (uniqueId) => {
        try {
            const res = await this.axios.get('account-holder/account/verification-request/'+uniqueId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    addVerificationRequest = async (uniqueId, request) => {
        try {
            const res = await this.axios.post('account-holder/account/'+uniqueId+'/verification-request', request);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    updateVerificationRequest = async (uniqueId, request) => {
        try {
            const res = await this.axios.put('account-holder/account/verification-request/'+uniqueId, request);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    updateVerificationRequestProofs = async (uniqueId, proofs) => {
        try {
            const res = await this.axios.put('account-holder/account/verification-request/'+uniqueId, proofs);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    deleteVerificationRequestProof = async (uniqueId, version, reason) => {
        try {
            const res = await this.axios.delete('account-holder/account/verification-request/identity-proof/'+uniqueId+'/'+version+'/'+reason);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    addVerificationRequestProof = async (uniqueId, version, proof) => {
        try {
            const res = await this.axios.post('account-holder/account/verification-request/'+uniqueId+'/'+version+'/identity-proof', proof);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAllAccountPurchases = async (accountId) => {
        try {
            const res = await this.axios.get('account-holder/account/purchases/'+accountId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    isIdentityAvailable = async (value) => {
        try {
            const res = await this.axios.get('account-holder/identity/isAvailable/'+value);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getAccessDevice = async (uniqueId) => {
        try {
            const res = await this.axios.get('account-holder/account/access-device/'+uniqueId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    updateDevice = async (uniqueId, account) => {
        try {
            const res = await this.axios.put('account-holder/account/access-device/'+uniqueId, account);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    updateIdentity = async (uniqueId, identity) => {
        try {
            const res = await this.axios.put('account-holder/identity/'+uniqueId, identity);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getIdentity = async (uniqueId) => {
        try {
            const res = await this.axios.get('account-holder/identity/'+uniqueId);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    linkAccount = async (uniqueId, version, account) => {
        try {
            const res = await this.axios.put('account-holder/'+uniqueId+'/link/'+version, account);
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    unlinkAccount = async (uniqueId, version, account) => {
        try {
            const res = await this.axios.put('account-holder/'+uniqueId+'/unlink/'+version+'/'+account, {});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    registerCard = async (uniqueId, cardId) => {
        try {
            const res = await this.axios.post('account-holder/'+uniqueId+'/register/smartcard/'+cardId, {});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    deregisterCard = async (uniqueId, cardId) => {
        try {
            const res = await this.axios.post('account-holder/'+uniqueId+'/deregister/smartcard/'+cardId, {});
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

    getTicketTransactionLog = async (fareItemIssuedNumber) => {
        try {
            const res = await this.axios.get('webportal/ticket/'+fareItemIssuedNumber+'/transaction-log');
            if (typeof res === 'undefined' || res.status >= 400) { return null; }
            return res;
        } catch (err) {
            log.error(err);
        }
    }

}

module.exports = AccountHolderServ;
