//#import Util.js
//#import $owlService:services/product/PriceUtil.jsx
//#import $owl_mall_setting:services/modelService.jsx

var CommonUtilApi = new JavaImporter(
    Packages.java.net.URLEncoder
);

var CommonUtil = (function () {
    var f = {
        checkMobile: function (mobile) {
            var regex_mobile = owl_mall_settingService.get("owl_mall_setting_0_regex_mobile");

            var mobileRegex = new RegExp(regex_mobile.args_value);
            if (mobileRegex.test(mobile)) {
                return true;
            }
            return false;
        },
        checkLoginId: function (loginId) {
            var regex = /^[a-zA-Z0-9][a-zA-Z0-9_]*$/;
            if (regex.test(loginId)) {
                return true;
            }
            return false;
        },
        checkEmail: function (email) {
            var regex = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
            if (regex.test(email)) {
                return true;
            }
            return false;
        },
        formatIdCard: function (idCard) {
            if (!idCard) {
                return "";
            }
            if (idCard.length < 17) {
                return idCard;
            }
            return idCard.substring(0, 4) + '****' + idCard.substring(14);
        },
        getRanString: function (len) {
            var str = "";
            var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

            for (var i = 0; i < len; i++) {
                var pos = Math.round(Math.random() * (arr.length - 1));
                str += arr[pos];
            }
            return str;
        },
        getRanNumber: function (len) {
            var str = "";
            var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

            for (var i = 0; i < len; i++) {
                var pos = Math.round(Math.random() * (arr.length - 1));
                str += arr[pos];
            }
            return str;
        },
        isExist: function (objIds, objId) {
            if (objIds) {
                for (var i = 0; i < objIds.length; i++) {
                    if (objIds[i] == objId) {
                        return true;
                    }
                }
            }
            return false;
        },
        isNeedGetFromPigeon: function (refreshIds, id) {
            if (refreshIds) {
                var ids = refreshIds.split(",");
                for (var i = 0; i < ids.length; i++) {
                    if (ids[i] == id) {
                        return true;
                    }
                }
            }
            return false;
        },
        getObjectById: function (objs, objId) {
            if (objs) {
                for (var i = 0; i < objs.length; i++) {
                    var jObj = objs[i];
                    if (jObj.id == objId) {
                        return jObj;
                    }
                }
            }
            return null;
        },
        getMinPrice: function (product) {
            if (!product) return null;
            var minPrice;
            var skus = product.items;
            if (skus && skus.length) {
                skus.forEach(function (item, index) {
                    var sell_price = Number(item.sell_price);
                    if (index == 0) {
                        minPrice = sell_price;
                    } else if (sell_price < minPrice) {
                        minPrice = sell_price;
                    }
                })
            } else {
                minPrice = (product.price_info && product.price_info.sell_price) || null;
            }
            return PriceUtil.toYuan(minPrice);
        },
        convertDateTime: function (time, cFormat) {
            if (arguments.length === 0) {
                time = new Date();
            }
            var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
            var date;
            if (typeof time === 'object') {
                date = time;
            } else {
                if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
                    time = parseInt(time);
                }
                if ((typeof time === 'number') && (time.toString().length === 10)) {
                    time = time * 1000;
                }
                date = new Date(time);
            }
            var formatObj = {
                y: date.getFullYear(),
                m: date.getMonth() + 1,
                d: date.getDate(),
                h: date.getHours(),
                i: date.getMinutes(),
                s: date.getSeconds(),
                a: date.getDay()
            };
            return format.replace(/{([ymdhisa])+}/g, function (result, key) {
                var value = formatObj[key];
                // Note: getDay() returns 0 on Sunday
                if (key === 'a') {
                    return ['日', '一', '二', '三', '四', '五', '六'][value]
                }
                return value.toString().padStart(2, '0')
            });
        },
        convertTimestamp: function (time) {
            var dateTime = CommonUtil.convertDateTime(time);
            dateTime = dateTime.substring(0, 19);
            dateTime = dateTime.replace(/-/g, '/');
            return new Date(dateTime).getTime();
        },
        Map: function (field) {
            this.data = [];
            this.length = 0;
            this.clear = function () {
                this.length = 0;
                this.data.length = 0;
            };
            this.put = function (item) {
                var key = item[field];
                if (!item || key == null) {
                    return false;
                }
                var vIndex = -1;
                this.data.forEach(function (obj, index) {
                    if (obj.key == key) {
                        vIndex = index;
                    }
                });
                item = {key: key, value: item};
                if (vIndex > -1) {
                    this.data[vIndex] = item;
                } else {
                    this.data.push(item);
                }
                this.length = this.data.length;
                return true;
            };
            this.get = function (key) {
                if (!key) {
                    return null;
                }
                var item = this.data.find(function (item) {
                    return item.key == key;
                });
                return typeof item == 'undefined' ? undefined : item.value;
            };
            this.has = function (key) {
                if (!key) {
                    return false;
                }
                var item = this.data.find(function (obj) {
                    return obj.key == key;
                });
                return typeof item != 'undefined';
            }
        },
        convertMap: function (field, items) {
            if (!items || !field || typeof field != 'string') {
                return new this.Map(field);
            }
            var map = new this.Map(field);
            items.forEach(function (item, index) {
                map.put(item);
            });
            return map;
        },
        Set: function () {
            this.data = [];
            this.length = 0;
            this.clear = function () {
                this.length = 0;
                this.data.length = 0;
            };
            this.add = function (item) {
                if (this.data.indexOf(item) == -1) {
                    this.data.push(item);
                    this.length = this.data.length;
                    return true;
                }
                return false;
            };
            this.has = function (key) {
                if (!key) {
                    return false;
                }
                return this.data.indexOf(key) != -1;
            };
        },
        convertSet: function (field, items) {
            if (!items || !field || typeof field != 'string') {
                return new this.Set();
            }
            var set = new this.Set();
            items.forEach(function (item, index) {
                set.add(item[field]);
            });
            return set;
        },
        encodeUrl: function (content) {
            return CommonUtilApi.URLEncoder.encode(content, "UTF-8") + "";
        },
        parseDate: function (d) {
            if (isNaN(d)) {
                return new Date(d);
            } else {
                return new Date(Number(d));
            }
        },
        cleanHtml: function (str) {
            return str.replace(/<[^>]*>/g, '');
        }
    };
    return f;
})();