const pdf = require('pdf-extraction');
const fs = require('fs-extra');
module.exports = {
    num2Text: (alaberNumber) => {
        var chineseNumber = '零一二三四五六七八九'.split('');
        var amountSmallUnit = ['', '十', '百', '千'];
        var amountBigUnit = ['', '萬', '億', '兆', '京', '垓', '秭', '穰', '溝', '澗', '正', '載'];
        var alaberNumberSplit = new Array();
        var alaberNumberSplitCount = parseInt(alaberNumber.length / 4);

        for (var i = 0; i <= alaberNumberSplitCount; i++) {
            if (i == 0 && alaberNumber.length % 4 != 0) {
                alaberNumberSplit[i] = alaberNumber.substr(0, alaberNumber.length % 4);
                alaberNumber = alaberNumber.slice(alaberNumber.length % 4);
            } else if (alaberNumber != '') {
                alaberNumberSplit[i] = alaberNumber.substr(0, 4);
                alaberNumber = alaberNumber.slice(4);
            }
        }

        var chineseBigNumber = '';

        for (var i = 0; i < alaberNumberSplit.length; i++) {
            for (var j = 0; j < alaberNumberSplit[i].length; j++) {
                if (
                    alaberNumberSplit[i][0] != 0 &&
                    j == 1 &&
                    alaberNumberSplit[i][j] == 0 &&
                    alaberNumberSplit[i].length == 4 &&
                    alaberNumberSplit[i][2] != 0
                ) {
                    chineseBigNumber += chineseNumber[alaberNumberSplit[i][j]];
                } else if (alaberNumberSplit[i][j] != 0) {
                    chineseBigNumber += chineseNumber[alaberNumberSplit[i][j]];
                    chineseBigNumber += amountSmallUnit[alaberNumberSplit[i].length - 1 - j];
                } else if (alaberNumberSplit[i][j] == 0 && alaberNumberSplit[i][j - 1] != 0) {
                    if (alaberNumberSplit[i][alaberNumberSplit[i].length - 1] != 0) {
                        chineseBigNumber += chineseNumber[alaberNumberSplit[i][j]];
                    }
                }
            }
            if (parseInt(alaberNumberSplit[i]) != 0) {
                chineseBigNumber += amountBigUnit[alaberNumberSplit.length - 1 - i];
            }
        }

        if (chineseBigNumber != '') {
            /* chineseBigNumber += '元整'; */
            chineseBigNumber = '' + chineseBigNumber + '';
        }
        return chineseBigNumber;
    },
    text2Num: (digit) => {
        const zh = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        const unit = ['千', '百', '十'];
        const quot = [
            '萬',
            '億',
            '兆',
            '京',
            '垓',
            '秭',
            '穰',
            '溝',
            '澗',
            '正',
            '載',
            '極',
            '恆河沙',
            '阿僧祗',
            '那由他',
            '不可思議',
            '無量',
            '大數',
        ];
        let result = 0,
            quotFlag;
        for (let i = digit.length - 1; i >= 0; i--) {
            if (zh.indexOf(digit[i]) > -1) {
                // 數字
                if (quotFlag) {
                    result += quotFlag * getNumber(digit[i]);
                } else {
                    result += getNumber(digit[i]);
                }
            } else if (unit.indexOf(digit[i]) > -1) {
                // 十分位
                if (quotFlag) {
                    result += quotFlag * getUnit(digit[i]) * getNumber(digit[i - 1]);
                } else {
                    result += getUnit(digit[i]) * getNumber(digit[i - 1]);
                }
                --i;
            } else if (quot.indexOf(digit[i]) > -1) {
                // 萬分位
                if (unit.indexOf(digit[i - 1]) > -1) {
                    if (getNumber(digit[i - 1])) {
                        result += getQuot(digit[i]) * getNumber(digit[i - 1]);
                    } else {
                        result += getQuot(digit[i]) * getUnit(digit[i - 1]) * getNumber(digit[i - 2]);
                        quotFlag = getQuot(digit[i]);
                        --i;
                    }
                } else {
                    result += getQuot(digit[i]) * getNumber(digit[i - 1]);
                    quotFlag = getQuot(digit[i]);
                }
                --i;
            }
        }
        return result;
        // 返回中文大寫數字對應的阿拉伯數字
        function getNumber(num) {
            for (let i = 0; i < zh.length; i++) {
                if (zh[i] == num) {
                    return i;
                }
            }
        } // 取單位
        function getUnit(num) {
            for (let i = unit.length; i > 0; i--) {
                if (num == unit[i - 1]) {
                    return Math.pow(10, 4 - i);
                }
            }
        } // 取分段
        function getQuot(q) {
            for (var i = 0; i < quot.length; i++) {
                if (q == quot[i]) {
                    return Math.pow(10, (i + 1) * 4);
                }
            }
        }
    },
    //pdf轉text
    pdf2text: async (filePath) => {
        console.log('PDF2Text轉換中....');
        try {
            let dataBuffer = fs.readFileSync(filePath);
            const data = await pdf(dataBuffer);
            console.log('PDF轉換成功');
            return data.text.replace(/["']/g, "");
            // Fix Specail Character Cause SQL Syntax Error
            // This issue was first found in imeetu_aacd & fix by Johnny @ 20220125
        } catch (err) {
            console.log(err);
        }
    },
};
