import { eqi18n } from '@easyquery/core'

export function registerTimePickerWidget($) {

    //TimePicker widget
    $.widget("eqjs.TimePicker", {
    
        options: {
            internalFormat: "hh:mm", //:ss"

            onChange: function () {
            },

            setValue: function (val) {
            }

        },

        _convertToInternalFormat: function (time) {
            var result = time;// + ':00';
            return result;
        },

        _raiseOnChange: function () {
            if (this.options && this.options.onChange) {
                this.options.onChange(this.time());
            }
        },

        _create: function () {
            var self = this;

            this.element.attr('maxLength', 5);

            this.time("");


            this.element.bind({
                focus: function () {
                },

                change: function () {
                },

                blur: function () {
                    //self._raiseOnChange();
                },

                keypress: function (e) {
                    var chc = e.charCode || e.which;  //to support IE 8
                    var chr = String.fromCharCode(chc);
                    var newVal = self.element.val().replace("_", chr);

                    if (self._isValidValue(newVal)) {
                        self.element.val(newVal); //.select();
                    }

                    e.preventDefault();
                },

                keydown: function (e) {
                    if (e.which == 8 || e.which == 46) {
                        var value = self.element.val();
                        var idx = value.indexOf("_");
                        if (idx < 0) {
                            idx = value.length;
                        }
                        if (idx > 0) {
                            idx--;
                            if (value.charAt(idx) == ":") {
                                idx--;
                            }
                            value = self._setCharAt(value, idx, "_");
                            self.element.val(value);
                        }
                        e.preventDefault();
                    }
                    else if (e.which == 13) {
                        self._raiseOnChange();
                    }
                    else if (e.which == 27) {
                        self.element.hide();
                    }
                },

                focusout: function () {
                    if (self.element.is(':visible')) {
                        self._raiseOnChange();
                    }
                }


            });

            this._render();
        },

        _setCharAt: function (str, index, chr) {
            if (index > str.length - 1) return str;
            return str.substr(0, index) + chr + str.substr(index + 1);
        },

        _render: function () {
            //this._checkValue();
            //this.element.val(this.options.value);
        },


        _toEditFormat: function (val) {
            if (this._isValidTime(val)) {
                var matches = val.match(/^\d\d:\d\d/);
                if (matches.length > 0) {
                    val = matches[0];
                }
                return val;
            }
            else {
                return this._getInitEditValue();
            }
        },

        _getInitEditValue: function () {
            return "__:__";
        },

        _fixValue: function (val) {
            val = val.replace(/\_/g, "0");
//            if (val.match(/:/g).length < 2) {
//                val += ":00";
//            }
            return val;
        },

        _isValidValue: function (val) {
            var timeVal = this._fixValue(val);
            return this._isValidTime(timeVal);
        },

        _isValidTime: function (val) {
            var timePattern = /^(([0-1]?[0-9])|([2][0-4])):([0-5]?[0-9])(:([0-5]?[0-9]))?$/i;
            var result = timePattern.test(val);
            return result;
        },

        time: function () {
            if (arguments.length > 0) {
                var val = arguments[0];
                val = this._toEditFormat(val);
                this.element.val(val);
                this._render();
                return this;
            }
            else {
                var result = this._fixValue(this.element.val());
                return result;
            }
        }
    });
}

