/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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/15.
 */
define(["adoreapp/widget/WidgetManager"
    , "adoreapp/layout/HeaderFooterLayoutData"
    , "adoreapp/layout/RowGridLayoutData"
    , "adoreapp/layout/SashLayoutData"
    , "adoreapp/widget/EventHub"
    , "adoreapp/util/TabUtil"
    , "adoreapp/hotkey/KeystrokeGenerator"
    , "adoreapp/hotkey/Keys"], function (WidgetManager, HeaderFooterLayoutData, RowGridLayoutData, SashLayoutData, EventHub, TabUtil, KeystrokeGenerator, Keys) {
    "use strict";
    var manager = WidgetManager;
    return Fiber.extend(function () {
        return {
            // The `init` method serves as the constructor.
            init: function (id, parent) {
                this.id = id;
                this.parent = parent;
                manager.register(this);
                if (this.parent) {
                    this.father = manager.byId(this.parent);
                    this.father.addChild(this);
                }
            },

            sendFocusGain: function () {
                var id = this.id;
                EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {id: id, evtType: EventHub.ON_FOCUS});
            },
            // Public

            append: function () {
                if (this.parent) {
                    this.jqueryNode.appendTo(this.father.attachNode)
                } else {
                    this.jqueryNode.appendTo(document.body);
                }
            },

            _handleMouseDownEvent: function (e) {
                EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                    id: this.id,
                    evtType: EventHub.MOUSE_DOWN,
                    button: e.button
                });
            },

            _handleMouseUpEvent: function (e) {
                EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                    id: this.id,
                    evtType: EventHub.MOUSE_UP,
                    button: e.button
                });
            },

            _handleKeyDownEvent: function (e, hasFilter) {
                var filter = false;
                var keyStroke = KeystrokeGenerator.generateKeyStroke(e);
                var keyEventObj = {
                    id: this.id,
                    evtType: EventHub.KEY_DONW,
                    altKey: e.altKey,
                    shiftKey: e.shiftKey,
                    ctrlKey: e.ctrlKey,
                    keyCode: e.keyCode,
                    keyStroke: keyStroke
                };
                if (e.keyCode == Keys.TAB)
                    e.preventDefault();
                if (hasFilter) {
                    filter = EventHub.dispatchEvent(EventHub.MSG_TYPE.DISPLAY_FILTER, keyEventObj);
                }
                if (!filter)
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, keyEventObj);
            },

            _handleKeyUpEvent: function (e) {
                var keyStroke = KeystrokeGenerator.generateKeyStroke(e);
                var keyEventObj = {
                    id: this.id,
                    evtType: EventHub.KEY_UP,
                    altKey: e.altKey,
                    shiftKey: e.shiftKey,
                    ctrlKey: e.ctrlKey,
                    keyCode: e.keyCode,
                    keyStroke: keyStroke
                };
                EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, keyEventObj);
            },

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

            _setVisible: function (attrs) {
                var visible = attrs.visible;
                if (visible) {
                    this._css({cssStyle: {"display": "block"}});
                    /** tabs refresh begin **/
                    var tabs = this.jqueryNode.find("ul.adore_tabs");
                    for (var i = 0; i < tabs.length; i++) {
                        var tmp = $(tabs[i]);
                        if (tmp.find("li.adore_tab").length !== 0) {
                            //tmp.tabs();
                            TabUtil.tabsSwitch(tmp);
                            TabUtil.refreshTabs(tmp);
                        }
                    }
                    /** tabs refresh end **/
                } else {
                    this._css({cssStyle: {"display": "none"}});
                }
            },

            _dispose: function () {
                manager.unregister(this.id);
                this.jqueryNode.remove();
                if (this.father) {
                    var index = this.father.children.indexOf(this);
                    this.father.children.splice(index, 1);
                }
            },

            _setFocus: function () {

            },

            _setLayoutData: function (attrs) {
                var layout = this.father ? this.father.layout : undefined;
                if (layout) {
                    var layoutData = attrs.layoutData;
                    var layoutType = layout.toString();
                    if (layoutType === "HeaderFooterLayout") {
                        this.layoutData = new HeaderFooterLayoutData();
                        if (layoutData.style === this.layoutData.HEADER) {
                            layout.setHeader_height(layoutData.height);
                        } else if (layoutData.style === this.layoutData.FOOTER) {
                            layout.setFooter_height(layoutData.height)
                        }
                        this.layoutData.setData(attrs.layoutData);
                    } else if (layoutType === "RowGridLayout") {
                        this.layoutData = new RowGridLayoutData();
                        this.layoutData.setData(attrs.layoutData);
                    } else if (layoutType === "SashLayout") {
                        this.layoutData = new SashLayoutData();
                        this.layoutData.setData(attrs.layoutData);
                    }
                }
            },

            _setBackgroundColor: function (attrs) {

            },

            _css: function (attrs) {
                if (this.jqueryNode) {
                    this.jqueryNode.css(attrs.cssStyle);
                } else {
                    if (!this.cssStyle)
                        this.cssStyle = {};
                    this.cssStyle = $.extend(this.cssStyle, attrs.cssStyle);
                }
            },

            _toString: function () {
                return "";
            },

            _layout: function () {
                var layout = this.father ? this.father.layout : undefined;
                if (layout) {
                    var layoutType = layout.toString();
                    if (layoutType === "RowGridLayout") {
                        if (this.layoutData && this.father.toString() !== "ToolBar") {
                            var column = this.layoutData.getColumn();
                            var offset = this.layoutData.getOffset();
                            var row = this.layoutData.getRow();
                            var style = column + " col " + offset;
                            var rowComponent = this.father.layout.getRow(row);
                            this.jqueryNode.addClass(style);
                            if (this.father.instance) {
                                this.btnNode.removeClass("adore_toolbar_color");
                                this.btnNode.find('span').removeClass("adore_toolbar_btnSpan");
                            }
                            this.jqueryNode.appendTo(rowComponent);
                        } else {
                            if (this.father.instance) {
                                if (this.tooltip) {
                                    this.jqueryNode.find("i").removeAttr("class");
                                    this._setText({text: this.tooltip});
                                    this.btnNode.addClass("adore_toolbar_color");
                                    this.btnNode.find('span').addClass("adore_toolbar_btnSpan");
                                }
                            }
                            this.append();
                        }
                    } else if (layoutType === "FillLayout") {
                        this.jqueryNode.css({
                            position: "absolute",
                            top: 0,
                            bottom: 0,
                            left: 0,
                            right: 0
                            //多余的属性，去掉
                            /*, width: "100%", height: "100%"*/
                        });
                        this.append();
                    }
                }

                if (this.jqueryNode && this.cssStyle) {
                    this.jqueryNode.css(this.cssStyle);
                    this.cssStyle = null;
                }
            }
        };
    });
});