let JV = require('../jpc_value_define');
let JpcFieldHelper = require('./jpc_helper_field');
const OFFSET_FLOAT = 0.0000000001;

let JpcCommonOutputHelper = {
    createCommonOutputWithoutDecorate: function (node, value, forceCombine) {
        let me = this, rst = {};
        //1. font/style/control
        rst[JV.PROP_FONT] = node[JV.PROP_FONT];
        rst[JV.PROP_CONTROL] = node[JV.PROP_CONTROL];
        rst[JV.PROP_STYLE] = node[JV.PROP_STYLE];
        //2. value
        rst[JV.PROP_VALUE] = value;
        me.formatCell(node[JV.PROP_FORMAT], rst);
        // innerFormat(node[JV.PROP_FORMAT], rst);
        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null) {
            rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
        } else if (node[JV.PROP_PREFIX] && forceCombine) {
            rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX];
        }
        if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null) {
            rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
        } else if (node[JV.PROP_SUFFIX] && forceCombine) {
            rst[JV.PROP_VALUE] = node[JV.PROP_SUFFIX];
        }
        return rst;
    },
    createCommonOutput: function (node, value, controls) {
        let me = this, rst = {};
        //1. font/style/control
        rst[JV.PROP_FONT] = node[JV.PROP_FONT];
        rst[JV.PROP_CONTROL] = node[JV.PROP_CONTROL];
        rst[JV.PROP_STYLE] = node[JV.PROP_STYLE];
        //2. value
        rst[JV.PROP_VALUE] = value;
        JpcFieldHelper.decorateValue(rst, controls);
        me.formatCell(node[JV.PROP_FORMAT], rst);
        // innerFormat(node[JV.PROP_FORMAT], rst);
        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== "") {
            rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
        }
        if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== "") {
            rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
        }
        return rst;
    },
    formatCell: function(formatStr, rstCell) {
        if (formatStr) {
            const rstCellFloatVal = parseFloat(rstCell[JV.PROP_VALUE]);
            if (!(isNaN(rstCellFloatVal))) {
                const dotIdx = formatStr.indexOf('.');
                if (dotIdx >= 0) {
                    let tmpStr = (rstCellFloatVal + OFFSET_FLOAT).toFixed(formatStr.length - dotIdx - 1);
                    const digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
                    for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
                        if (digStr[sIdx] === '#') {
                            if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
                                tmpStr = tmpStr.substr(0, tmpStr.length - 1);
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    }
                    if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
                    rstCell[JV.PROP_VALUE] = tmpStr;
                } else {
                    rstCell[JV.PROP_VALUE] = (rstCellFloatVal + OFFSET_FLOAT).toFixed(0);
                }
                const commaIdx = formatStr.indexOf(',');
                if (commaIdx >= 0) {
                    rstCell[JV.PROP_VALUE] = comdify(rstCell[JV.PROP_VALUE].toString());
                }
            }
        }
    }
};

function comdify(numStr){
    let re = /\d{1,3}(?=(\d{3})+$)/g;
    return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
}

module.exports = JpcCommonOutputHelper;