'use strict';

import { JV } from '../jpc_value_define';
import * as strUtil from '../../public/stringUtil';
import { Fields, IControlCollection, ICustomizeCfg, IGroupField, ICell, IOrgGroupField, IRptTpl } from '../../interface/basic';

export let JpcFieldHelper = {
    getValue: function (dataField: string[] | null, valueIdx: number) {
        let rst = '';
        if (dataField && (dataField.length > valueIdx) && (valueIdx >= 0)) {
            rst = dataField[valueIdx];
        }
        return `${rst}`;
    },
    setValue: function (dataField: string[], valueIdx: number, newValue: string) {
        if (dataField && (dataField.length > valueIdx) && (valueIdx >= 0)) {
            dataField[valueIdx] = newValue;
        }
    },
    resetSumFormat: function (ref_tab_fields: Fields[], sum_tab_field: Fields) {
        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.Format) {
                        sum_tab_field.Format = tab_field.Format;
                    }
                    break;
                }
            }
        }
        return rst;
    },
    resetFormat: function (tab_field: Fields, map_field: IGroupField | null, customizeCfg: ICustomizeCfg | null) {
        let rst = false;
        if (map_field && map_field.Precision && map_field.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.Format && tab_field.Format.indexOf(",") >= 0) {
                tab_field.Format = "," + formatStrs.join("");
            } else {
                tab_field.Format = formatStrs.join("");
            }
            map_field.Format = tab_field.Format; //这里顺手把format也赋给map_field,后期统计用得着
            rst = true;
        } else if (map_field && tab_field.Format && tab_field.Format.indexOf("#") >= 0 && (customizeCfg && customizeCfg.fillZero)) {
            tab_field.Format = strUtil.replaceAll(tab_field.Format, '#', '0');
            map_field.Format = tab_field.Format;
            rst = true;
        }
        return rst;
    },
    resetFlexibleFormat: function (tab_field: Fields, ref_field_data: IGroupField | null, flexiblePrecisionRefObj: Record<string, number>, valueIdx: number, customizeCfg: ICustomizeCfg | null) {
        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.Format && tab_field.Format.indexOf(",") >= 0) {
            tab_field.Format = "," + formatStrs.join("");
        } else {
            tab_field.Format = formatStrs.join("");
        }
    },
    decorateValue: function (cell: ICell, controls: IControlCollection | null) {
        if (controls) {
            const showZero = controls[cell.control  as string].ShowZero;
            if (showZero && showZero === 'F') {
                const val = parseFloat(cell.Value  as string);
                if (val === 0) {
                    let chkRst = true;
                    if (typeof cell.Value === 'string' && cell.Value.length > 1) {
                        for (let idx = 0; idx < cell.Value.length; idx++) {
                            if (cell.Value[idx] !== '0' && cell.Value[idx] !== '.') {
                                chkRst = false;
                                break;
                            }
                        }
                    }
                    if (chkRst) cell.Value = '';
                }
            }
        }
    },

    findAndPutDataFieldIdx: (
        rptTpl: IRptTpl,
        tab_fields: { FieldID: number }[] | null,
        rstFields: { FieldID: number }[] | null,
        rstFieldsIdx: (number | IOrgGroupField)[] | null,
        isEx: boolean
    ) => {
        //通过FieldID找到相关映射指标的位置IDX并记录下来，方便后续引用
        if (tab_fields) {
            let DTL_STR = isEx ? '从数据指标_拓展集合' : '从数据指标_集合';
            let detail_fields = rptTpl.指标_数据_映射[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.无映射离散指标_集合 && rptTpl.无映射离散指标_集合.length > 0) {
                            for (let discretField of rptTpl.无映射离散指标_集合) {
                                if (discretField.ID === tab_fields[i].FieldID) {
                                    rstFieldsIdx.push(discretField);
                                    isFounded = true;
                                    break;
                                }
                            }
                        } else {
                            rstFieldsIdx.push(JV.BLANK_FIELD_INDEX);
                        }
                    }
                }
            }
        }
    },
    findAutoHeightFieldIdx: function (rptTpl: IRptTpl, tab_fields: Fields[], rstFieldsIdx: any[], isEx: boolean) {
        if (tab_fields) {
            let DTL_STR = isEx ? JV.NODE_DETAIL_FIELDS_EX : JV.NODE_DETAIL_FIELDS;
            let detail_fields = rptTpl.指标_数据_映射[DTL_STR];
            for (let i = 0; i < tab_fields.length; i++) {
                if (tab_fields[i].isAutoHeight) {
                    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.无映射离散指标_集合;
                for (let i = 0; i < tab_fields.length; i++) {
                    if (tab_fields[i].isAutoHeight) {
                        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;
                            }
                        }
                    }
                }
            }
        }
    }
};


