/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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.
 */
import {Widget} from "widget/Widget";
import {Composite} from "widget/Composite";
import {ADORE} from "ADORE";
import {EventHub} from "event/EventHub";
import {MessageType} from "event/MessageType";
import {Event} from "event/Event";
import {Keys} from "hotkey/Keys";


/**
 * Created by xiachiquan on 2016/6/3.
 */

export class CheckButton extends Widget {

    private static template0: string = `<div class="adore_check_button">
    <input type="checkbox" tabindex="-1"/>
    <label></label>
    </div>`;

    private static template1: string = `<div class="switch">
    <label>
        <input type="checkbox" tabindex="-1">
        <span class="lever"></span>
    </label>
    </div>`;

    private isSwitch: boolean;
    private groupName: string;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.isSwitch = false;
        if ((ADORE.Switch & style) === ADORE.Switch) {
            this.$thisNode = $(CheckButton.template1).attr("id", this.id);
            this.isSwitch = true;
        } else {
            this.$thisNode = $(CheckButton.template0).attr("id", this.id);
        }
        let tmpId = this.id;
        this.$thisNode.find("input[type=checkbox]").attr("id", this.id + "_checkbox").on("change", function (e) {
            // dispatch onChange event
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: tmpId,
                checked: this.checked + "",
                evtType: Event.ON_CHANGE,
                syncAttrName: this.checked
            });
        });
        if (this.isSwitch === false) {
            this.$thisNode.find("label").attr("for", this.id + "_checkbox");
        }
        let own = this;
        this.$thisNode.find("input").on("focus", function (e) {
            own.sendFocusGain();
        }).on("blur", function (e) {
            own.handleBlur();
        });
    }

    public setName(args: { name: string }) {
        if (this.isSwitch === false) {
            this.$thisNode.find("label").text(args.name);
        }
        //开关不支持设置name
    }

    public setGroupName(args: { group: string }) {
        this.groupName = args.group;
        this.$thisNode.find("input").attr("name", this.groupName);
    }

    public setChecked(args: { isChecked: boolean }) {
        let isChecked = args.isChecked;
        let checked: boolean = (<HTMLInputElement>this.$thisNode.find("input")[0]).checked;
        if (isChecked !== checked) {
            if (this.enabled === false) {
                this.$thisNode.find("input").removeAttr("disabled");
                this.$thisNode.find("input").click();
                this.$thisNode.find("input").attr("disabled", "disabled");
            } else {
                this.$thisNode.find("input").click();
            }
        }
    }

    public setEnabled(args: { enabled: boolean }) {
        this.enabled = args.enabled;
        if (this.enabled) {
            this.$thisNode.find("input").removeAttr("disabled");
        } else {
            this.$thisNode.find("input").attr("disabled", "disabled");
        }
    }

    private handleBlur() {
        this.$thisNode.find("label").removeClass("adore_check_button_focus_label");
    }

    public setFocus() {
        let inputNode = this.$thisNode.find("input");
        if (document.activeElement !== inputNode[0]) {
            inputNode.focus();
        }
        this.$thisNode.find("label").addClass("adore_check_button_focus_label");
    }


    public handleKeyDownEvent(e: KeyboardEvent, hasFilter: boolean) {
        if (e.keyCode === Keys.SPACE || e.keyCode === Keys.ENTER || e.keyCode === Keys.NUMPAD_ENTER) {
            this.$thisNode.find("input").click();
            e.preventDefault(); //拦截底层对SPACE按键的处理
            return;
        }
        super.handleKeyDownEvent(e, hasFilter);
    }

    public handleKeyUpEvent(e: KeyboardEvent) {
        if (e.keyCode === Keys.SPACE || e.keyCode === Keys.ENTER || e.keyCode === Keys.NUMPAD_ENTER) {
            return;
        }
        super.handleKeyUpEvent(e);
    }

}