let JV = require('./jpc_value_define');
let JpcData = {
    createNew: function() {
        let JpcDataRst = {};
        JpcDataRst.dataSeq = [];
        JpcDataRst.analyzeData = function(rptTpl, dataObj) {
            let me = this;
            if ((rptTpl) && (dataObj)) {
                //1. get ID fields
                let masterIDs = [];
                for (let i = 0; i < rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].length; i++) {
                    let mstFieldObj = rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS][i];
                    if ((mstFieldObj[JV.PROP_IS_ID]) && (mstFieldObj[JV.PROP_IS_ID] === 'T')) {
                        masterIDs.push({"idx": i, "seq": mstFieldObj[JV.PROP_ID_SEQ]});
                    }
                }
                let detailIDs = [];
                for (let i = 0; i < rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].length; i++) {
                    let dtlFieldObj = rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS][i];
                    if ((dtlFieldObj[JV.PROP_IS_ID]) && (dtlFieldObj[JV.PROP_IS_ID] === 'T')) {
                        detailIDs.push({"idx": i, "seq": dtlFieldObj[JV.PROP_ID_SEQ]});
                    }
                }
                //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[JV.DATA_MASTER_DATA][masterIDs[i]["idx"]]);
                        mst_dt_len = dataObj[JV.DATA_MASTER_DATA][masterIDs[i]["idx"]].length;
                    }
                    let dtl_fields = [];
                    for (let i = 0; i < detailIDs.length; i++) {
                        dtl_fields.push(dataObj[JV.DATA_DETAIL_DATA][detailIDs[i]["idx"]]);
                        dtl_dt_len = dataObj[JV.DATA_DETAIL_DATA][detailIDs[i]["idx"]].length;
                    }
                    let sIdx = 0;
                    let isEqual = true;
                    for (let i = 0; i < mst_dt_len; i++) {
                        me.dataSeq.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) {
                                me.dataSeq[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
                    let field = dataObj[JV.DATA_DETAIL_DATA][0];
                    me.dataSeq = [[]];
                    for (let i = 0; i < field.length; i++) {
                        me.dataSeq[0].push(i);
                    }
                }
            }
            //alert(3);
        };
        return JpcDataRst;
    }
}

module.exports = JpcData;