/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE 
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer 
 * Software" and other relevant administrative regulations and international treaties. Without 
 * the written permission of the Company, no person may use (including but not limited to the 
 * illegal copy, distribute, display, image, upload, and download) and disclose the above 
 * technical documents to any third party. Otherwise, any infringer shall afford the legal 
 * liability to the company.
 */
/**
 * Created by liushuai on 2015/1/16.
 */
define(["adoreapp/widget/BaseWidget", "adoreapp/widget/EventHub",
    "adoreapp/hotkey/Keys", "adoreapp/widget/constants/ADORE"], function (BaseWidget, EventHub, Keys, Adore) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                this.templates = attrs.template.split("<!-- split -->");
                this.isSwitch = false;
                if (Adore.Switch & attrs.style === Adore.Switch) {
                    this.jqueryNode = $(this.templates[1]).attr("id", this.id);
                    this.isSwitch = true;
                } else {
                    this.jqueryNode = $(this.templates[0]).attr("id", this.id);
                }
                var tmpId = this.id;
                this.jqueryNode.find("input[type=checkbox]").attr("id", this.id + "_checkbox").on("change", function (e) {
                    // dispatch onChange event
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: tmpId,
                        checked: this.checked + "",
                        evtType: EventHub.ON_CHANGE,
                        syncAttrName: this.checked
                    });
                });
                if (this.isSwitch === false) {
                    this.jqueryNode.find("label").attr("for", this.id + "_checkbox");
                }
                var own = this;
                this.jqueryNode.on("focus", function (e) {
                    own.sendFocusGain();
                });
                this.jqueryNode.find("input").on("focus", function (e) {
                    own.sendFocusGain();
                });
                this.jqueryNode.on("blur", function (e) {
                    own.handleBlur();
                });
                this.jqueryNode.find("input").on("blur", function (e) {
                    e.preventDefault();//阻止该blur事件，防止触发到最外层的blur事件
                    own._setFocus();
                });
                this.enabled = true;
            },

            _setName: function (attrs) {
                var name = attrs.name;
                if (this.isSwitch === false) {
                    this.jqueryNode.find("label").text(name);
                }
                //开关不支持设置name
            },

            _setGroupName: function (attrs) {
                this.groupName = attrs.group;
                this.jqueryNode.find("input").attr("name", this.groupName);
            },

            _setChecked: function (attrs) {
                var isChecked = attrs.isChecked;
                var checked = this.jqueryNode.find("input")[0].checked;
                if (isChecked !== checked) {
                    if (this.enabled === false) {
                        this.jqueryNode.find("input").removeAttr("disabled");
                        this.jqueryNode.find("input").click();
                        this.jqueryNode.find("input").attr("disabled", "disabled");
                    } else {
                        this.jqueryNode.find("input").click();
                    }
                }
            },

            _setEnabled: function (attrs) {
                this.enabled = attrs.enabled;
                if (this.enabled) {
                    this.jqueryNode.find("input").removeAttr("disabled");
                } else {
                    this.jqueryNode.find("input").attr("disabled", "disabled");
                }
            },

            _setFocus: function () {
                //this.jqueryNode.find("input").focus();
                this.jqueryNode.focus();
                this.jqueryNode.find("label").addClass("adore_check_button_focus_label");
            },

            handleBlur: function () {
                this.jqueryNode.find("label").removeClass("adore_check_button_focus_label");
            },

            _handleKeyDownEvent: function (e, hasFilter) {
                if (e.keyCode === Keys.SPACE) {
                    this.jqueryNode.find("input").click();
                    return;
                }
                base._handleKeyDownEvent.call(this, e, hasFilter);
            },

            _handleKeyUpEvent: function (e) {
                if (e.keyCode === Keys.SPACE) {
                    return;
                }
                base._handleKeyUpEvent.call(this, e);
            },

            _layout: function () {
                base._layout.call(this);
            }
        };
    });
});
//<form action="#">
//    <p>
//        <input name="group1" type="checkbox" id="test1" />
//        <label for="test1">Red</label>
//    </p>
//    <p>
//        <input name="group1" type="checkbox" id="test2" />
//        <label for="test2">Yellow</label>
//    </p>
//    <p>
//        <input class="with-gap" name="group1" type="checkbox" id="test3"  />
//        <label for="test3">Green</label>
//    </p>
//    <p>
//        <input name="group1" type="checkbox" id="test4" disabled="disabled" />
//        <label for="test4">Brown</label>
//    </p>
//</form>
