'use strict';

import { JpcCommonHelper } from './helper/jpc_helper_common';
import { IDataObj, IRptTpl } from '../interface/basic';
import { IDataObjProps } from '../interface/enum';

export const JpcData = {
    createNew() {
        let JpcDataRst = {
            dataSeq: [],
            exDataSeq: [],
            analyzeData(rptTpl: IRptTpl, dataObj: IDataObj) {
                let me = this;
                let _analyse = function (MASTER_FIELD_STR: string, DETAIL_FIELD_STR: string, MASTER_DATA_STR: string, DETAIL_DATA_STR: string, dataSeqArr: any) {
                    //1. get ID fields
                    let masterIDs = [];
                    if (rptTpl.指标_数据_映射[MASTER_FIELD_STR]) {
                        for (let i = 0; i < rptTpl.指标_数据_映射[MASTER_FIELD_STR].length; i++) {
                            let mstFieldObj = rptTpl.指标_数据_映射[MASTER_FIELD_STR][i];
                            if (JpcCommonHelper.getBoolean(mstFieldObj.isID)) {
                                masterIDs.push({ "idx": i, "seq": mstFieldObj.IDSeq });
                            }
                        }
                    }
                    let detailIDs = [];
                    if (rptTpl.指标_数据_映射[DETAIL_FIELD_STR]) {
                        for (let i = 0; i < rptTpl.指标_数据_映射[DETAIL_FIELD_STR].length; i++) {
                            let dtlFieldObj = rptTpl.指标_数据_映射[DETAIL_FIELD_STR][i];
                            if (JpcCommonHelper.getBoolean(dtlFieldObj.isID)) {
                                detailIDs.push({ "idx": i, "seq": dtlFieldObj.IDSeq });
                            }
                        }
                    }
                    //2. sort the ID fields
                    if (masterIDs.length > 1) {
                        masterIDs.sort(function (a, b) {
                            return 1 * a["seq"] - 1 * b["seq"];
                        })
                    }
                    if (detailIDs.length > 1) {
                        detailIDs.sort(function (a, b) {
                            return 1 * a["seq"] - 1 * b["seq"];
                        })
                    }
                    //3. prepare data sequence
                    if (masterIDs.length > 0) {
                        let mst_dt_len = 0, dtl_dt_len = 0, mst_fields = [];
                        for (let i = 0; i < masterIDs.length; i++) {
                            mst_fields.push(dataObj[MASTER_DATA_STR][masterIDs[i]["idx"]]);
                            mst_dt_len = dataObj[MASTER_DATA_STR][masterIDs[i]["idx"]].length;
                        }
                        let dtl_fields = [];
                        for (let i = 0; i < detailIDs.length; i++) {
                            dtl_fields.push(dataObj[DETAIL_DATA_STR][detailIDs[i]["idx"]]);
                            dtl_dt_len = dataObj[DETAIL_DATA_STR][detailIDs[i]["idx"]].length;
                        }
                        let sIdx = 0;
                        let isEqual = true;
                        for (let i = 0; i < mst_dt_len; i++) {
                            dataSeqArr.push([]);
                            //then compare the master/detail ID-field value
                            for (let j = sIdx; j < dtl_dt_len; j++) {
                                isEqual = true;
                                for (let k = 0; k < mst_fields.length; k++) {
                                    if (!(mst_fields[k][i] === dtl_fields[k][j])) {
                                        isEqual = false;
                                        break;
                                    }
                                }
                                if (isEqual) {
                                    dataSeqArr[i].push(j);
                                } else {
                                    sIdx = j;
                                    //below logic is for the data robustness purpose, to avoid those strange record(detail level) which could not match even one of the master record!
                                    if (i < mst_dt_len - 1 && j < dtl_dt_len - 1) {
                                        for (let j1 = j; j1 < dtl_dt_len; j1++) {
                                            isEqual = true;
                                            for (let k = 0; k < mst_fields.length; k++) {
                                                if (!(mst_fields[k][i + 1] === dtl_fields[k][j1])) {
                                                    isEqual = false;
                                                    break;
                                                }
                                            }
                                            if (isEqual) {
                                                sIdx = j1;
                                                break;
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    } else { //if no master data
                        if (dataObj && dataObj[DETAIL_DATA_STR] && dataObj[DETAIL_DATA_STR].length > 0) {
                            //may be bill type report which may only have discrete fields!
                            let field = dataObj[DETAIL_DATA_STR][0];
                            dataSeqArr.push([]);
                            for (let i = 0; i < field.length; i++) {
                                dataSeqArr[0].push(i);
                            }
                        }
                    }
                };
                if ((rptTpl) && (dataObj)) {
                    _analyse('主数据指标_集合', '从数据指标_集合', IDataObjProps.master_data, IDataObjProps.detail_data, me.dataSeq);
                    if (rptTpl.流水式表_拓展信息) {
                        _analyse('主数据指标_拓展集合', '从数据指标_拓展集合', IDataObjProps.master_data_ex, IDataObjProps.detail_data_ex, me.exDataSeq);
                    }
                }
            }
        };
        return JpcDataRst;
    }
};