/**
 * Created by Tony on 2016/12/28.
 */
let strUtil = require('../../../public/stringUtil');
const scMathUtil = require('../../../public/scMathUtil').getUtil();
let JV = require('./jpc_value_define');
let 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;
    },
    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;
            }
        }
    }
};

module.exports = JE;