/**
 * Created by Tony on 2016/12/28.
 */
const strUtil = require('../../../public/stringUtil');
const scMathUtil = require('../../../public/scMathUtil').getUtil();
const JV = require('./jpc_value_define');
const JE = {
    $STR_UTIL: strUtil,
    $SC_MATH_UTIL: scMathUtil,
    F: function(fID, $CURRENT_RPT) {
        let rst = null;
        if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_DETAIL_FIELDS][JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields[JV.NODE_DETAIL_FIELDS][JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_MASTER_FIELDS][JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields[JV.NODE_MASTER_FIELDS][JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_MASTER_FIELDS_EX][JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields[JV.NODE_MASTER_FIELDS_EX][JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_DETAIL_FIELDS_EX][JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields[JV.NODE_DETAIL_FIELDS_EX][JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_DISCRETE_FIELDS][JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields[JV.NODE_DISCRETE_FIELDS][JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields[JV.NODE_NO_MAPPING_FIELDS][JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields[JV.NODE_NO_MAPPING_FIELDS][JV.PROP_ID + "_" + fID];
        } else {
            rst = {msg: "the Field-ID is not valid, no result could be found!"};
        }
        return rst;
    },
    P: function(pID, $CURRENT_RPT) {
        let rst = null;
        if ($CURRENT_RPT && ($CURRENT_RPT.params[JV.PROP_ID + "_" + pID])) {
            rst = $CURRENT_RPT.params[JV.PROP_ID + "_" + pID];
        } else {
            rst = {msg: "the Param-ID is not valid, no result was found!"};
        }
        return rst;
    },
    getCurrentPage: function ($CURRENT_RPT) {
        let rst = 0;
        if ($CURRENT_RPT) {
            rst = $CURRENT_RPT.runTimePageData.currentPage;
        }
        return rst;
    },
    getTotalPage: function ($CURRENT_RPT) {
        let rst = 0;
        if ($CURRENT_RPT) {
            rst = $CURRENT_RPT.totalPages;
        }
        return rst;
    },
    getFieldDataLen: function(field, dataObj) {
        let rst = 0;
        if (field.DataNodeName === 'NA') {
            if (!field[JV.PROP_AD_HOC_DATA]) {
                field[JV.PROP_AD_HOC_DATA] = [];
            }
            rst = field[JV.PROP_AD_HOC_DATA].length;
        } else {
            if (!field.DataNodeName) {
                // that means this is a self-defined discrete field!
                field.DataNodeName = JV.DATA_DISCRETE_DATA;
                field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA];
                dataObj[JV.DATA_DISCRETE_DATA].push([]);
            }
            rst = dataObj[field.DataNodeName][field.DataSeq].length;
        }
        return rst;
    },
    setFieldValue: function (field, dataObj, valIdx, newValue) {
        if (field.DataNodeName === "NA") {
            if (!field[JV.PROP_AD_HOC_DATA]) {
                field[JV.PROP_AD_HOC_DATA] = [];
            }
            field[JV.PROP_AD_HOC_DATA][valIdx] = newValue;
        } else if (!field.DataNodeName) {
            //that means this is a self-defined discrete field!
            field.DataNodeName = JV.DATA_DISCRETE_DATA;
            field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA].length;
            dataObj[JV.DATA_DISCRETE_DATA].push([]);
            dataObj[field.DataNodeName][field.DataSeq][valIdx] = newValue;
        } else {
            dataObj[field.DataNodeName][field.DataSeq][valIdx] = newValue;
        }
    },
    getFieldValue: function (field, dataObj, valIdx, newVal) {
        let rst = null;
        if (field.DataNodeName === "NA") {
            if (!field[JV.PROP_AD_HOC_DATA]) {
                field[JV.PROP_AD_HOC_DATA] = [];
            }
            if (field[JV.PROP_AD_HOC_DATA].length > valIdx) {
                rst = field[JV.PROP_AD_HOC_DATA][valIdx];
            } else {
                if (newVal === null && field[JV.PROP_AD_HOC_DATA].length > 0) {
                    rst = field[JV.PROP_AD_HOC_DATA][field[JV.PROP_AD_HOC_DATA].length - 1];
                }
            }
        } else {
            if (!field.DataNodeName) {
                //that means this is a self-defined discrete field!
                field.DataNodeName = JV.DATA_DISCRETE_DATA;
                field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA];
                dataObj[JV.DATA_DISCRETE_DATA].push([]);
            }
            if (dataObj[field.DataNodeName][field.DataSeq].length > valIdx) {
                rst = dataObj[field.DataNodeName][field.DataSeq][valIdx];
            } else {
                if (newVal === null && dataObj[field.DataNodeName][field.DataSeq].length > 0) {
                    rst = dataObj[field.DataNodeName][field.DataSeq][dataObj[field.DataNodeName][field.DataSeq].length - 1];
                }
            }
        }
        if (rst === null || rst === undefined) rst = newVal;
        return rst;
    },
    getFieldValueArray: function(field, dataObj) {
        let rst = null;
        if (field.DataNodeName === 'NA') {
            if (!field[JV.PROP_AD_HOC_DATA]) {
                field[JV.PROP_AD_HOC_DATA] = [];
            }
            rst = field[JV.PROP_AD_HOC_DATA];
        } else {
            if (!field.DataNodeName) {
                // that means this is a self-defined discrete field!
                field.DataNodeName = JV.DATA_DISCRETE_DATA;
                field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA];
                dataObj[JV.DATA_DISCRETE_DATA].push([]);
            }
            rst = dataObj[field.DataNodeName][field.DataSeq];
        }
        if (rst === null || rst === undefined) rst = [];
        return rst;
    },
    setFieldValueArray: function(field, dataObj, newArr) {
        if (newArr instanceof Array) {
            if (field.DataNodeName === 'NA') {
                field[JV.PROP_AD_HOC_DATA] = newArr;
            } else {
                if (!field.DataNodeName) {
                    // that means this is a self-defined discrete field!
                    field.DataNodeName = JV.DATA_DISCRETE_DATA;
                    field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA].length;
                    dataObj[JV.DATA_DISCRETE_DATA].push([]);
                }
                dataObj[field.DataNodeName][field.DataSeq] = newArr;
            }
        }
    },
    removeFieldValue: function (field, dataObj, valIdx) {
        if (field.DataNodeName === "NA") {
            if (field[JV.PROP_AD_HOC_DATA].length > valIdx && valIdx >= 0) {
                field[JV.PROP_AD_HOC_DATA].splice(valIdx, 1);
            }
        } else {
            if (dataObj[field.DataNodeName][field.DataSeq].length > valIdx && valIdx >= 0) {
                dataObj[field.DataNodeName][field.DataSeq].splice(valIdx, 1);
            }
        }
    },
    insertFieldValue: function (field, dataObj, valIdx, newValue) {
        if (field.DataNodeName === "NA") {
            if (field[JV.PROP_AD_HOC_DATA].length > valIdx && valIdx >= 0) {
                field[JV.PROP_AD_HOC_DATA].splice(valIdx, 0, newValue);
            } else if (field[JV.PROP_AD_HOC_DATA].length <= valIdx) {
                field[JV.PROP_AD_HOC_DATA][field[JV.PROP_AD_HOC_DATA].length] = newValue;
            }
        } else {
            if (dataObj[field.DataNodeName][field.DataSeq].length > valIdx && valIdx >= 0) {
                dataObj[field.DataNodeName][field.DataSeq].splice(valIdx, 0, newValue);
            } else if (dataObj[field.DataNodeName][field.DataSeq].length <= valIdx) {
                dataObj[field.DataNodeName][field.DataSeq][dataObj[field.DataNodeName][field.DataSeq].length] = newValue;
            }
        }
    },
    alignFieldDecimal: function(sourceID, destID, $CURRENT_RPT) {
        // 把source指标的精度align到dest
        const me = JE;
        let sourceField = null;
        if (isNaN(parseInt(sourceID))) {
            sourceField = sourceID;
        } else {
            sourceField = me.F(sourceID,$CURRENT_RPT);
        }
        let destField = null;
        if (isNaN(parseInt(destID))) {
            destField = destID;
        } else {
            destField = me.F(destID,$CURRENT_RPT);
        }
        if (sourceField[JV.PROP_PRECISION]) {
            destField[JV.PROP_PRECISION] = sourceField[JV.PROP_PRECISION];
            destField[JV.PROP_FIXED_PRECISION_AMT] = sourceField[JV.PROP_FIXED_PRECISION_AMT];
        }
    },
    batchIniFields: function(IDArr, $CURRENT_RPT) {
        // 批处理查找指标
        const me = JE;
        for (let idxF = 0; idxF < IDArr.length; idxF++) {
            IDArr[idxF] = me.F(IDArr[idxF],$CURRENT_RPT);
        }
    }
};

module.exports = JE;