import { JV } from './jpc_value_define';
import { JE } from './jpc_rte';
import { JpcFieldHelper } from './helper/jpc_helper_field';
import { JpcBandHelper } from './helper/jpc_helper_band';
import { JpcCommonHelper } from './helper/jpc_helper_common';
import { JpcDiscreteHelper } from './helper/jpc_helper_discrete';
import JpcTextHelper from './helper/jpc_helper_text';
import { JpcCommonOutputHelper } from './helper/jpc_helper_common_output';
import { JpcAreaHelper } from './helper/jpc_helper_area';
import {
    ICurrent_RPT,
    ICustomizeCfg,
    IBands,
    IControlCollection,
    IRptTpl,
    IDataObj,
} from '../interface/basic';
import { IDataObjProps } from '../interface/enum'
export class JpcBillTabSrvClass {
    disp_fields_idx: number[];

    constructor() {
        this.disp_fields_idx = []; // typescript的class属性初始化都要放constructor里，不然总报错
    };

    // 该方法主要是通过账单报表的信息，获取相对应于报表模板信息中的数据信息的数组的序号，以便后面取值计算
    sorting(rptTpl: IRptTpl) {
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl.账单式表_信息.账单式表_数据.bill_field_s, [], this.disp_fields_idx, false);
    };
    // 获取总页数
    //问题：为什么dataFields第一个元素就可以确定总页数
    paging(rptTpl: IRptTpl, dataObj: IDataObj) {
        let rst = 0;
        function _getDataLength(fields_str: IDataObjProps) {
            let dataFields = dataObj[fields_str];
            if (dataFields && dataFields.length > 0) {
                rst = dataFields[0].length;
            }
        }
        if (rptTpl.指标_数据_映射) {
            if (rptTpl.指标_数据_映射.从数据指标_集合) {
                _getDataLength(IDataObjProps.detail_data);
            } else if (rptTpl.指标_数据_映射.主数据指标_集合) {
                _getDataLength(IDataObjProps.master_data);
            } else if (rptTpl.指标_数据_映射.离散指标_集合) {
                _getDataLength(IDataObjProps.discrete_data);
            } else if (rptTpl.指标_数据_映射.离散参数_集合) {
                rst = 1;
            }
        }
        return rst;
    };

    outputAsPreviewPage(rptTpl: IRptTpl, bands: IBands, controls: IControlCollection, $CURRENT_RPT: ICurrent_RPT) {
        let me = this, rst: any[] = [], vIdx = [];
        //只预览第一页的数据
        let pageStatus = [true, true, false, true, false, false, false, false];//？？
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus, true, false);
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        rst = rst.concat(me.outputPreviewContent(rptTpl, bands, unitFactor, controls, pageStatus));
        rst = rst.concat(JpcDiscreteHelper.outputPreviewDiscreteInfo(rptTpl.账单式表_信息.离散信息, bands, unitFactor, pageStatus));
        return rst;
    };

    outputAsSimpleJSONPage(rptTpl: IRptTpl, dataObj: IDataObj, page: number, bands: IBands, controls: IControlCollection, $CURRENT_RPT: ICurrent_RPT, customizeCfg: ICustomizeCfg) {
        let me = this, rst: any[] = [], tabRstLst = [];
        //1 calculate the band position
        let pageStatus: boolean[] = [true, false, false, false, false, false, false, false];
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus, false, false);
        //2. start to output detail-part
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        //2.1 output content
        tabRstLst.push(me.outputContent(rptTpl, dataObj, page, bands, unitFactor, controls, pageStatus, $CURRENT_RPT, customizeCfg));
        //2.2 output discrete
        tabRstLst.push(JpcDiscreteHelper.outputDiscreteInfo(rptTpl.账单式表_信息.离散信息, bands, dataObj, unitFactor, pageStatus, page - 1, 1, 0, $CURRENT_RPT, customizeCfg));
        for (let i = 0; i < tabRstLst.length; i++) {
            rst = rst.concat(tabRstLst[i]);
            tabRstLst[i] = null;
        }
        return rst;
    };

    outputPreviewContent(rptTpl: IRptTpl, bands: IBands, unitFactor: number, controls: IControlCollection, pageStatus: boolean[]) {
        let rst: any[] = [];
        let tab = rptTpl.账单式表_信息.账单式表_数据;
        let band = bands[tab.BandName];
        if (band) {
            if (pageStatus[(band.DisplayType) as number]) {
                let tab_fields = tab.bill_field_s;
                for (let i = 0; i < tab_fields.length; i++) {
                    let tab_field = tab_fields[i];
                    if (!(tab_field.Hidden)) {
                        let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, "", controls);
                        cellItem.area = JpcAreaHelper.outputArea(tab_field.area, band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(cellItem);
                    }
                }
                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.离散信息) {
                    rst = rst.concat(JpcDiscreteHelper.outputPreviewDiscreteInfo(tab.离散信息, bands, unitFactor, pageStatus));
                }
            }
        }
        return rst;
    };

    outputContent(rptTpl: IRptTpl, dataObj: IDataObj, page: number, bands: IBands, unitFactor: number, controls: IControlCollection, pageStatus: boolean[], $CURRENT_RPT: ICurrent_RPT, customizeCfg: ICustomizeCfg) {
        let me = this, rst: any[] = [];
        let tab = rptTpl.账单式表_信息.账单式表_数据;
        let band = bands[tab.BandName];
        if (band) {
            if (pageStatus[(band.DisplayType) as number]) {
                let tab_fields = tab.bill_field_s;
                // let data_details = null;//bylish
                let data_details = [];
                if (rptTpl.指标_数据_映射.从数据指标_集合) {
                    data_details = dataObj[IDataObjProps.detail_data];
                } else if (rptTpl.指标_数据_映射.主数据指标_集合) {
                    data_details = dataObj[IDataObjProps.master_data];
                }
                let flexiblePrecisionRefObj: any = null, flexibleRefField = null, precision_ref_data = null;
                for (let i = 0; i < tab_fields.length; i++) {
                    let tab_field = tab_fields[i];
                    let data_field = null, map_data_field = JE.F(tab_field.FieldID, $CURRENT_RPT);
                    if (me.disp_fields_idx.length > i && me.disp_fields_idx[i] !== JV.BLANK_FIELD_INDEX && (typeof me.disp_fields_idx[i] !== 'object')) {
                        data_field = data_details[me.disp_fields_idx[i]];
                    } else {
                        if (map_data_field) {
                            data_field = map_data_field.data_field;
                        }
                    }
                    if (map_data_field && map_data_field.Precision && map_data_field.flexiblePrecisionRefObj && map_data_field.Precision.flexibleRefFieldID) {//加了最后一个判断  by lish
                        if (flexiblePrecisionRefObj === null) {
                            flexiblePrecisionRefObj = {};
                            flexibleRefField = JE.F(map_data_field.Precision.flexibleRefFieldID, $CURRENT_RPT);
                            if (flexibleRefField) precision_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(tab_field, precision_ref_data, flexiblePrecisionRefObj, page - 1, customizeCfg);
                    } else {
                        if (page === 1) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                    }
                    if (!(tab_field.Hidden)) {
                        let val = JpcFieldHelper.getValue(data_field, page - 1);
                        let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, val, controls);
                        cellItem.area = JpcAreaHelper.outputArea(tab_field.area, band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(cellItem);
                    }
                }
                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.离散信息) {
                    rst = rst.concat(JpcDiscreteHelper.outputDiscreteInfo(tab.离散信息, bands, dataObj, unitFactor, pageStatus, page - 1, 1, 0, $CURRENT_RPT, customizeCfg));
                }
            }
        }
        return rst;
    }
}