import { JV } from '../jpc_value_define';
import { JE } from '../jpc_rte';
import JpcTextHelper from './jpc_helper_text';
import { JpcCommonOutputHelper } from './jpc_helper_common_output';
import { JpcAreaHelper } from './jpc_helper_area';
import { JpcFieldHelper } from './jpc_helper_field';
import { IBandDetail, IBands, ICell, ICurrent_RPT, ICustomizeCfg, IDataObj, IDiscrete } from '../../interface/basic';

export const JpcDiscreteHelper = {
    outputDiscreteInfo: function (discreteArray: IDiscrete[], bands: IBands, dataObj: IDataObj | null, unitFactor: number, pageStatus: boolean[], segIdx: number, multiCols: number, multiColIdx: number, $CURRENT_RPT: ICurrent_RPT, customizeCfg: ICustomizeCfg | null) {
        let rst = [];
        if (discreteArray && dataObj) {
            for (let i = 0; i < discreteArray.length; i++) {
                let band = bands[discreteArray[i].BandName];
                if (band && pageStatus[(band.DisplayType) as number] === true) {
                    if (discreteArray[i].text) {
                        rst.push(JpcTextHelper.outputText(discreteArray[i].text, band, unitFactor, 1, 0, 1, 0, multiCols, multiColIdx));
                    }
                    if (discreteArray[i].text_s) {
                        for (let j = 0; j < discreteArray[i].text_s.length; j++) {
                            rst.push(JpcTextHelper.outputText(discreteArray[i].text_s[j], band, unitFactor, 1, 0, 1, 0, multiCols, multiColIdx));
                        }
                    }
                    if (discreteArray[i].discrete_field_s) {
                        let flexiblePrecisionRefObj: any = null, flexibleRefField = null;
                        for (let j = 0; j < discreteArray[i].discrete_field_s.length; j++) {
                            let df = discreteArray[i].discrete_field_s[j];
                            let value = "";
                            if (df.FieldID) {
                                // let map_data_field = JE.F(df[JV.PROP_FIELD_ID], $CURRENT_RPT);
                                let map_data_field = JE.F(df.FieldID, $CURRENT_RPT);
                                if (map_data_field && map_data_field.DataSeq !== JV.BLANK_FIELD_INDEX) {
                                    let data = dataObj[map_data_field.DataNodeName][map_data_field.DataSeq];
                                    if (data && data.length > 0) {
                                        if (data.length > segIdx) {
                                            value = data[segIdx];
                                        } else {
                                            value = data[0];
                                        }
                                    }
                                } else {
                                    if (map_data_field && map_data_field[JV.PROP_AD_HOC_DATA] && map_data_field[JV.PROP_AD_HOC_DATA].length > 0) {
                                        value = map_data_field[JV.PROP_AD_HOC_DATA][0]
                                    } else value = "";
                                }
                                if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                                    let pre_ref_data = null;
                                    if (flexiblePrecisionRefObj === null) {
                                        flexiblePrecisionRefObj = {};
                                        flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                        if (flexibleRefField)
                                            pre_ref_data = dataObj[map_data_field.DataNodeName][flexibleRefField.DataSeq]; //这里的引用指标必须得存在，哪怕不显示，也要存放在指标集合中！
                                        for (let decimalObj of map_data_field.flexiblePrecisionRefObj) {
                                            flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                                        }
                                    }
                                    JpcFieldHelper.resetFlexibleFormat(df, pre_ref_data, flexiblePrecisionRefObj, segIdx, customizeCfg);
                                } else {
                                    JpcFieldHelper.resetFormat(df, map_data_field, customizeCfg);
                                }
                            } else if (df.ParamID) {
                                let param = JE.P(df.ParamID, $CURRENT_RPT);
                                if (param && param.Default_Value) value = param.Default_Value;
                            }
                            let item = JpcCommonOutputHelper.createCommonOutputWithoutDecorate(df, value, true);
                            //position
                            item.area = JpcAreaHelper.outputArea(df.area, band, unitFactor, 1, 0, 1, 0, multiCols, multiColIdx, false, false);
                            rst.push(item);
                        }
                    }
                }
            }
        }
        return rst;
    },
    outputPreviewDiscreteInfo: function (discreteArray: IDiscrete[], bands: IBands, unitFactor: number, pageStatus: boolean[]) {
        let rst = [];
        for (let i = 0; i < discreteArray.length; i++) {
            let band = bands[discreteArray[i].BandName];
            if (band && pageStatus[(band.DisplayType) as number]) {
                if (discreteArray[i].text) {
                    rst.push(JpcTextHelper.outputText(discreteArray[i].text, band, unitFactor, 1, 0, 1, 0, 1, 0));
                }
                if (discreteArray[i].text_s) {
                    for (let j = 0; j < discreteArray[i].text_s.length; j++) {
                        rst.push(JpcTextHelper.outputText(discreteArray[i].text_s[j], band, unitFactor, 1, 0, 1, 0, 1, 0));
                    }
                }
                if (discreteArray[i].discrete_field_s) {
                    for (let j = 0; j < discreteArray[i].discrete_field_s.length; j++) {
                        let df = discreteArray[i].discrete_field_s[j];
                        let value = "";
                        if (df.Default_Value) {
                            value = df.Default_Value;
                        }
                        let item = JpcCommonOutputHelper.createCommonOutputWithoutDecorate(df, value, false);
                        //position
                        item.area = JpcAreaHelper.outputArea(df.area, band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(item);
                    }
                }

            }
        }
        return rst;
    },
    outputPreviewSumInfoOutput: function (band: IBandDetail, unitFactor: number, tab: IDiscrete) {
        let rst = [];
        if (tab.text) {
            rst.push(JpcTextHelper.outputText(tab.text, band, unitFactor, 1, 0, 1, 0, 1, 0));
        }
        if (tab.text_s) {
            for (let j = 0; j < tab.text_s.length; j++) {
                rst.push(JpcTextHelper.outputText(tab.text_s[j], band, unitFactor, 1, 0, 1, 0, 1, 0));
            }
        }
        if (tab.sum_field_s) {
            for (let i = 0; i < tab.sum_field_s.length; i++) {
                let tab_field = tab.sum_field_s[i];
                if (!(tab_field.Hidden)) {
                    // rst.push(me.outputTabField(band, tab_field, null, -1, -1, 1, 0, 1, 0, unitFactor, true, controls, 0));
                    let value = "";
                    let item = JpcCommonOutputHelper.createCommonOutputWithoutDecorate(tab_field, value, false);
                    //position
                    item.area = JpcAreaHelper.outputArea(tab_field.area, band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                    rst.push(item);
                }
            }
        }
        return rst;
    }
}

