let JV = require('../jpc_value_define');

let JpcFieldHelper = {
    getValue: function(dataField, valueIdx) {
        let rst = "";
        if (dataField && (dataField.length > valueIdx) && (valueIdx >= 0)) {
            rst = dataField[valueIdx];
        }
        return rst;
    },
    setValue: function(dataField, valueIdx, newValue) {
        if (dataField && (dataField.length > valueIdx) && (valueIdx >= 0)) {
            dataField[valueIdx] = newValue;
        }
    },
    resetSumFormat: function(ref_tab_fields, sum_tab_field) {
        let rst = false;
        if (ref_tab_fields && ref_tab_fields.length > 0) {
            for (let tab_field of ref_tab_fields) {
                if (tab_field[JV.PROP_FIELD_ID] === sum_tab_field[JV.PROP_FIELD_ID]) {
                    rst = true;
                    if (tab_field[JV.PROP_FORMAT]) {
                        sum_tab_field[JV.PROP_FORMAT] = tab_field[JV.PROP_FORMAT];
                    }
                    break;
                }
            }
        }
        return rst;
    },
    resetFormat: function (tab_field, map_field, customizeCfg) {
        let rst = false;
        if (map_field && map_field[JV.PROP_PRECISION] && map_field[JV.PROP_PRECISION].type === "fixed") {
            let formatStrs = ["#"], ffStr = (customizeCfg && customizeCfg.fillZero)?"0":"#";
            for (let idx = 0; idx < parseInt(map_field[JV.PROP_FIXED_PRECISION_AMT]); idx++) {
                if (idx === 0) {
                    formatStrs.push(".");
                }
                formatStrs.push(ffStr);
            }
            if (tab_field[JV.PROP_FORMAT] && tab_field[JV.PROP_FORMAT].indexOf(",") >= 0) {
                tab_field[JV.PROP_FORMAT] = "," + formatStrs.join("");
            } else {
                tab_field[JV.PROP_FORMAT] = formatStrs.join("");
            }
            map_field[JV.PROP_FORMAT] = tab_field[JV.PROP_FORMAT]; //这里顺手把format也赋给map_field,后期统计用得着
            rst = true;
        }
        return rst;
    },
    resetFlexibleFormat: function (tab_field, ref_field_data, flexiblePrecisionRefObj, valueIdx, customizeCfg) {
        let precisionAmt = 2;
        if (ref_field_data && flexiblePrecisionRefObj && valueIdx >= 0) {
            precisionAmt = flexiblePrecisionRefObj["refUnit_" + ref_field_data[valueIdx]];
            if (precisionAmt === null || precisionAmt === undefined) precisionAmt = flexiblePrecisionRefObj["refUnit_其他未列单位"];
        }
        let formatStrs = ["#."], ffStr = (customizeCfg && customizeCfg.fillZero)?"0":"#";
        for (let idx = 0; idx < precisionAmt; idx++) {
            formatStrs.push(ffStr);
        }
        if (tab_field[JV.PROP_FORMAT] && tab_field[JV.PROP_FORMAT].indexOf(",") >= 0) {
            tab_field[JV.PROP_FORMAT] = "," + formatStrs.join("");
        } else {
            tab_field[JV.PROP_FORMAT] = formatStrs.join("");
        }
    },
    decorateValue: function (cell, controls) {
        if (controls) {
            let val = cell[JV.PROP_VALUE];
            let showZero = controls[cell[JV.PROP_CONTROL]][JV.PROP_SHOW_ZERO];
            if (showZero && showZero === 'F' ) {
                if (0.0 === 1.0 * (0 + val)) {
                    cell[JV.PROP_VALUE] = "";
                }
            }
        }
    },
    findAndPutDataFieldIdx: function (rptTpl, tab_fields, rstFields, rstFieldsIdx, isEx) {
        //通过FieldID找到相关映射指标的位置IDX并记录下来，方便后续引用
        if (tab_fields) {
            let DTL_STR = isEx?JV.NODE_DETAIL_FIELDS_EX:JV.NODE_DETAIL_FIELDS;
            let detail_fields = rptTpl[JV.NODE_FIELD_MAP][DTL_STR];
            for (let i = 0; i < tab_fields.length; i++) {
                let isFounded = false;
                for (let j = 0; j < detail_fields.length; j++) {
                    if (tab_fields[i]["FieldID"] === detail_fields[j]["ID"]) {
                        isFounded = true;
                        if (rstFields) rstFields.push(tab_fields[i]);
                        if (rstFieldsIdx) rstFieldsIdx.push(j);
                        break;
                    }
                }
                if (!isFounded) {
                    if (rstFields) rstFields.push(tab_fields[i]);
                    if (rstFieldsIdx) {
                        if (rptTpl[JV.NODE_NO_MAPPING_FIELDS] && rptTpl[JV.NODE_NO_MAPPING_FIELDS].length > 0) {
                            for (let discretField of rptTpl[JV.NODE_NO_MAPPING_FIELDS]) {
                                if (discretField[JV.PROP_ID] === tab_fields[i]["FieldID"]) {
                                    rstFieldsIdx.push(discretField);
                                    isFounded = true;
                                    break;
                                }
                            }
                        } else {
                            rstFieldsIdx.push(JV.BLANK_FIELD_INDEX);
                        }
                    }
                }
            }
        }
    },
    findAutoHeightFieldIdx: function(rptTpl, tab_fields, rstFieldsIdx, isEx) {
        if (tab_fields) {
            let DTL_STR = isEx?JV.NODE_DETAIL_FIELDS_EX:JV.NODE_DETAIL_FIELDS;
            let detail_fields = rptTpl[JV.NODE_FIELD_MAP][DTL_STR];
            for (let i = 0; i < tab_fields.length; i++) {
                if (tab_fields[i][JV.PROP_IS_AUTO_HEIGHT]) {
                    for (let j = 0; j < detail_fields.length; j++) {
                        if (tab_fields[i]["FieldID"] === detail_fields[j]["ID"]) {
                            let item = [];
                            item[0] = j;
                            item[1] = tab_fields[i];
                            rstFieldsIdx.push(item);
                            break;
                        }
                    }
                }
            }
            if (!isEx) {
                let dis_detail_fields = rptTpl[JV.NODE_NO_MAPPING_FIELDS];
                for (let i = 0; i < tab_fields.length; i++) {
                    if (tab_fields[i][JV.PROP_IS_AUTO_HEIGHT]) {
                        for (let j = 0; j < dis_detail_fields.length; j++) {
                            if (tab_fields[i]["FieldID"] === dis_detail_fields[j]["ID"]) {
                                let item = [];
                                item[0] = JV.BLANK_FIELD_INDEX;
                                item[1] = tab_fields[i];
                                rstFieldsIdx.push(item);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
};

module.exports = JpcFieldHelper;
