// let NumberFormat = require('../../libs/number-format.js');
import NumberFormat from '../util/number-format';
// import { debug } from 'util';
let customUtil = {};

var getActualAction = function (type, action, callback) {
    let ac;
    //@king
    let aco = ac = GMS.getContext().getCollector().getElement(type, action);
    if (typeof (ac) == 'function') {
        try {
            aco = ac();
        } catch (e) {
            console.log(e);
        }
        if (aco instanceof Promise || Object.prototype.toString.call(pfe) == '[object Promise]') {
            aco.then(res => {
                ac = res.default;
                if (typeof (ac) == 'function') {
                    callback(new ac());
                } else {
                    callback(ac);
                }
            });
            return;
        } else {
            aco = new aco();
        }
    }
    callback(aco);
};

customUtil.executeAction = function (dom, sels, action, param, type, extraActionObject, queryHandler, currentDataQueryConditionSet, callback) {
    getActualAction('actions', action, function (aco) {
        aco.execute({
            dom: dom,
            action: action,
            selects: sels,
            type: type,
            extraActionObject: extraActionObject,
            queryHandler: queryHandler,
            currentDataQueryConditionSet: currentDataQueryConditionSet,
            callback: callback
        }, param);
    })
};

customUtil.hasActionDisplay = function (action) {
    //@king
    let ac = GMS.getContext().getCollector().getElement('actionDisplay', action);
    if (!ac) {
        return false;
    }
    return true;
};

customUtil.hasActionEnable = function (action) {
    //@king
    let ac = GMS.getContext().getCollector().getElement('actionEnable', action);
    if (!ac) {
        return false;
    }
    return true;
};

/**
 * 
 * @param {当前组件} dom 
 * @param {列表所选数据} sels 
 * @param {动作名称} action 
 * @param {所传参数} param 
 * @param {类型} type 
 * @param {当前查询name与页签name对象集合} currentNameObject 
 * @returns 
 */
customUtil.actionDisplay = function (dom, sels, action, param, type, currentNameObject) {

    return new Promise((resolve, reject) => {
        getActualAction('actionDisplay', action, function (aco) {
            let callback = function (data) {
                resolve(data);
            };
            try {
                aco.display({
                    dom: dom,
                    action: action,
                    selects: sels,
                    type: type,
                    currentNameObject: currentNameObject,
                    queryHandler: null
                }, param, callback);
            } catch (error) {
                let result = {};
                result.key = param._key;
                result.result = false;
                resolve(result);
            }
        })
    });
};
customUtil.actionEnable = function (dom, sels, action, param, type) {
    return new Promise((resolve, reject) => {
        getActualAction('actionEnable', action, function (aco) {
            let callback = function (data) {
                resolve(data);
            };
            try {
                aco.enable({
                    dom: dom,
                    action: action,
                    selects: sels,
                    type: type,
                    queryHandler: null
                }, param, callback);
            } catch (error) {
                let result = {};
                result.key = param._key;
                result.enable = true;
                resolve(result);
            }
        });
    });
};

customUtil.dateTimeFormat = function (val, format) {
    if (val == '' || val == null) {
        return '';
    }
    // 
    //解决查询列表时间加一天的问题，由于格林威治时间与中国标准时间不一致导致转换时间戳后时间会多加10十小时，需要再减去
    let timeDate = new Date(new Date(val).getTime());
    if (Object.prototype.toString.call(val) === '[object String]') {
        if (val.indexOf('CST') > 0) {
            timeDate = new Date(new Date(val).getTime() - 50400000);
        }
    }
    let year = timeDate.getFullYear(); //当前年份
    let month = timeDate.getMonth() + 1; //当前月份
    let day = timeDate.getDate(); //天
    let hour = timeDate.getHours(); //小时
    let min = timeDate.getMinutes(); //分
    let sec = timeDate.getSeconds(); //秒
    let preArr = Array.apply(null, Array(10)).map(function (elem, index) {
        return '0' + index;
    }); //开个长度为10的数组 格式为 00 01 02 03
    if (!format) {
        format = 'yyyy-MM-dd';
    }
    let newTime = format.replace(/YY/g, year)
        .replace(/yyyy/g, year)
        .replace(/MM/g, preArr[month] || month)
        .replace(/DD/g, preArr[day] || day)
        .replace(/dd/g, preArr[day] || day)
        .replace(/hh/g, preArr[hour] || hour)
        .replace(/HH/g, preArr[hour] || hour)
        .replace(/mm/g, preArr[min] || min)
        .replace(/ss/g, preArr[sec] || sec);
    return newTime;
};

customUtil.numberFormat = function (val, format) {
    return new NumberFormat().format(val, format);
    //   if (val == undefined || val == '' || val == null) {
    //     return '';
    //   }
    //   if (!format) {
    //     return val;
    //   }
    //   moneystamp = val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    //   return moneystamp += ".00";
};

customUtil.strFormat = function (str, args) {
    let result = str;
    if (!args) {
        return result;
    }
    if (args instanceof Array) {
        if (args.length == 0) {
            return result;
        }
        for (let i = 0, len = args.length; i < len; i++) {
            if (args[i] != undefined) {
                let reg = new RegExp("({)" + i + "(})", "g");
                result = result.replace(reg, args[i]);
            }
        }
    } else if (args instanceof Object) {
        for (let key in args) {
            if (args[key] != undefined) {
                let reg = new RegExp("({" + key + "})", "g");
                result = result.replace(reg, args[key]);
            }
        }
    }
    return result;
};

customUtil.api = {
    count: '/bcp/query/advance/count',
    counts: '/bcp/query/advance/counts',
    query: '/bcp/query/advance/query',
    total: '/bcp/query/advance/total',
    subTotal: '/bcp/query/advance/subTotal',
    groupCount: '/bcp/query/advance/groupCount',
    sceneCreate: '/gov/common/query/advance/scene/',
    sceneQuery: '/gov/common/query/advance/scene',
    sceneDelete: '/gov/common/query/advance/scene/deleted'
};

export default customUtil;

// module.exports = customUtil;
