/**
 * Created by Tony on 2016/12/28.
 */


import * as strUtil from '../public/stringUtil';
import *  as scMathUtil from '@sc/util';
import { JV } from './jpc_value_define';
import { Fields, ICurrent_RPT, IDataObj } from '../interface/basic';
import { IDataObjProps } from '../interface/enum';
export const JE = {
    $STR_UTIL: strUtil,
    $SC_MATH_UTIL: scMathUtil,
     //获取把数据中fields的相关对象，并准备赋值到该对象中的某个属性
    F:  (fID: number, $CURRENT_RPT: ICurrent_RPT)=> {
        let rst = null;
        if ($CURRENT_RPT && ($CURRENT_RPT.fields.从数据指标_集合[JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields.从数据指标_集合[JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields.主数据指标_集合[JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields.主数据指标_集合[JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields.主数据指标_拓展集合[JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields.主数据指标_拓展集合[JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields.从数据指标_拓展集合[JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields.从数据指标_拓展集合[JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields.离散指标_集合[JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields.离散指标_集合[JV.PROP_ID + "_" + fID];
        } else if ($CURRENT_RPT && ($CURRENT_RPT.fields.无映射离散指标_集合[JV.PROP_ID + "_" + fID])) {
            rst = $CURRENT_RPT.fields.无映射离散指标_集合[JV.PROP_ID + "_" + fID];
        } else {
            //该方法返回对象可能会导致jpc_flow_tab.ts 的130行 报错, ,by lish
            rst = null;
            // rst = {msg: "the Field-ID is not valid, no result could be found!"};
        }
        return rst;
    },
       //获取把数据中params的相关对象，并准备赋值到该对象中的某个属性
    P:  (pID: number, $CURRENT_RPT: ICurrent_RPT)=> {
        let rst = null;
        if ($CURRENT_RPT && ($CURRENT_RPT.params[JV.PROP_ID + "_" + pID])) {
            rst = $CURRENT_RPT.params["ID_" + pID];
        } 
            // rst = {msg: "the Param-ID is not valid, no result was found!"}; by lish
        return rst;
    },
    getCurrentPage: function ($CURRENT_RPT: ICurrent_RPT) {
        let rst = 0;
        if ($CURRENT_RPT && $CURRENT_RPT.runTimePageData.currentPage) {
            rst = $CURRENT_RPT.runTimePageData.currentPage;
        }
        return rst;
    },
    getTotalPage: function ($CURRENT_RPT: ICurrent_RPT) {
        let rst = 0;
        if ($CURRENT_RPT) {
            rst = $CURRENT_RPT.totalPages;
        }
        return rst;
    },
    getFieldDataLen: function (field: Fields, dataObj: IDataObj) {
        let rst = 0;
        if (field.DataNodeName === 'NA') {
            if (!field.data_field) {
                field.data_field = [];
            }
            rst = field.data_field.length;
        } else {
            if (!field.DataNodeName) {
                // that means this is a self-defined discrete field!
                field.DataNodeName = JV.DATA_DISCRETE_DATA;
                field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA];
                dataObj[JV.DATA_DISCRETE_DATA].push([]);
            }
            rst = dataObj[field.DataNodeName][field.DataSeq].length;
        }
        return rst;
    },
    //向目标对象赋值
    setFieldValue: function (field: Fields, dataObj: IDataObj, valIdx: number, newValue: number) {
        if (field.DataNodeName === "NA") {
            if (!field.data_field) {
                field.data_field = [];
            }
            field.data_field[valIdx] = newValue;
        } else if (!field.DataNodeName) {
            //that means this is a self-defined discrete field!
            field.DataNodeName = IDataObjProps.discrete_data;
            field.DataSeq = dataObj[IDataObjProps.discrete_data].length;
            dataObj[IDataObjProps.discrete_data].push([]);
            dataObj[field.DataNodeName][field.DataSeq][valIdx] = newValue;
        } else {
            dataObj[field.DataNodeName][field.DataSeq][valIdx] = newValue;
        }
    },
    getFieldValue: function (field: Fields, dataObj: IDataObj, valIdx: number, newVal: number|string) {
        let rst = null;
        if (field.DataNodeName === "NA") {
            if (!field.data_field) {
                field.data_field = [];
            }
            if (field.data_field.length > valIdx) {
                rst = field.data_field[valIdx];
            } else {
                if (newVal === null && field.data_field.length > 0) {
                    rst = field.data_field[field.data_field.length - 1];
                }
            }
        } else {
            if (!field.DataNodeName) {
                //that means this is a self-defined discrete field!
                field.DataNodeName = IDataObjProps.discrete_data;
                field.DataSeq = dataObj[IDataObjProps.discrete_data];
                dataObj[IDataObjProps.discrete_data].push([]);
            }
            if (dataObj[field.DataNodeName][field.DataSeq].length > valIdx) {
                rst = dataObj[field.DataNodeName][field.DataSeq][valIdx];
            } else {
                if (newVal === null && dataObj[field.DataNodeName][field.DataSeq].length > 0) {
                    rst = dataObj[field.DataNodeName][field.DataSeq][dataObj[field.DataNodeName][field.DataSeq].length - 1];
                }
            }
        }
        if (rst === null || rst === undefined) rst = newVal;
        return rst;
    },
    getFieldValueArray: function (field: Fields, dataObj: IDataObj) {
        let rst = null;
        if (field.DataNodeName === 'NA') {
            if (!field.data_field) {
                field.data_field = [];
            }
            rst = field.data_field;
        } else {
            if (!field.DataNodeName) {
                // that means this is a self-defined discrete field!
                field.DataNodeName = JV.DATA_DISCRETE_DATA;
                field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA];
                dataObj[JV.DATA_DISCRETE_DATA].push([]);
            }
            rst = dataObj[field.DataNodeName][field.DataSeq];
        }
        if (rst === null || rst === undefined) rst = [];
        return rst;
    },
    setFieldValueArray: function (field: Fields, dataObj: IDataObj, newArr: number[]) {
        if (newArr instanceof Array) {
            if (field.DataNodeName === 'NA') {
                field.data_field = newArr;
            } else {
                if (!field.DataNodeName) {
                    // that means this is a self-defined discrete field!
                    field.DataNodeName = JV.DATA_DISCRETE_DATA;
                    field.DataSeq = dataObj[JV.DATA_DISCRETE_DATA].length;
                    dataObj[JV.DATA_DISCRETE_DATA].push([]);
                }
                dataObj[field.DataNodeName][field.DataSeq] = newArr;
            }
        }
    },
    removeFieldValue: function (field: Fields, dataObj: IDataObj, valIdx: number) {
        if (field.DataNodeName === "NA") {
            if (field.data_field.length > valIdx && valIdx >= 0) {
                field.data_field.splice(valIdx, 1);
            }
        } else {
            if (dataObj[field.DataNodeName][field.DataSeq].length > valIdx && valIdx >= 0) {
                dataObj[field.DataNodeName][field.DataSeq].splice(valIdx, 1);
            }
        }
    },
    insertFieldValue: function (field: Fields, dataObj: IDataObj, valIdx: number, newValue: number) {
        if (field.DataNodeName === "NA") {
            if(!field.data_field) field.data_field=[];
            if (field.data_field.length > valIdx && valIdx >= 0) {
                field.data_field.splice(valIdx, 0, newValue);
            } else if (field.data_field.length <= valIdx) {
                field.data_field[field.data_field.length] = newValue;
            }
        } else {
            if (dataObj[field.DataNodeName][field.DataSeq].length > valIdx && valIdx >= 0) {
                dataObj[field.DataNodeName][field.DataSeq].splice(valIdx, 0, newValue);
            } else if (dataObj[field.DataNodeName][field.DataSeq].length <= valIdx) {
                dataObj[field.DataNodeName][field.DataSeq][dataObj[field.DataNodeName][field.DataSeq].length] = newValue;
            }
        }
    },
    alignFieldDecimal: function(sourceID:number, destID:number,$CURRENT_RPT:ICurrent_RPT) {
        // 把source指标的精度align到dest
        const me = JE;
        let sourceField = null;
        if (isNaN(sourceID)) {
            sourceField = sourceID;
        } else {
            sourceField = me.F(sourceID,$CURRENT_RPT);
        }
        let destField :any;
        if (isNaN(destID)) {
            destField = destID;
        } else {
            destField = me.F(destID,$CURRENT_RPT);
        }
        if ( sourceField instanceof Object && sourceField && sourceField.Precision) {
            destField.Precision = sourceField.Precision;
            destField.fixedPrecisionNum = sourceField.fixedPrecisionNum;
        }
    },
    batchIniFields: function(IDArr:any[],$CURRENT_RPT:ICurrent_RPT) {
        // 批处理查找指标
        const me = JE;
        for (let idxF = 0; idxF < IDArr.length; idxF++) {
            IDArr[idxF] = me.F(IDArr[idxF],$CURRENT_RPT);
        }}
};
