var JV = require('../jpc_value_define');
var JpcFieldHelper = require('./jpc_helper_field');

var JpcCommonOutputHelper = {
    createCommonOutputWithoutDecorate: function (node, value, controls) {
        var 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;
        if (node[JV.PROP_FORMAT]) {
            if (!(isNaN(parseFloat(rst[JV.PROP_VALUE])))) {
                var dotIdx = node[JV.PROP_FORMAT].indexOf(".");
                if (dotIdx >= 0) {
                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(node[JV.PROP_FORMAT].length - dotIdx - 1);
                } else {
                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(0);
                }
            }
        }
        if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] != null) {
            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] + node[JV.PROP_SUFFIX];
        }
        return rst;
    },
    createCommonOutput: function (node, value, controls) {
        var 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);
        if (node[JV.PROP_FORMAT]) {
            if (!(isNaN(parseFloat(rst[JV.PROP_VALUE])))) {
                var dotIdx = node[JV.PROP_FORMAT].indexOf(".");
                if (dotIdx >= 0) {
                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(node[JV.PROP_FORMAT].length - dotIdx - 1);
                } else {
                    rst[JV.PROP_VALUE] = parseFloat(rst[JV.PROP_VALUE]).toFixed(0);
                }
            }
        }
        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;
    }
}

module.exports = JpcCommonOutputHelper;