/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 huangdeyun on 2015/7/7.
 */
define(["adoreapp/widget/WidgetHelper", "adoreapp/hotkey/DocumentEventInterceptor"], function (WidgetHelper, DocumentEventInterceptor) {
    var lastPanelID, lastOverflowYValue;
    return {
        ChangeTheme: 0,
        SoftwareKeyboardShowed: 1,
        SoftwareKeyboardHided: 2,
        AddDisplayEvent: 3,
        RemoveDisplayEvent: 4,

        _notify: function (attr) {
            switch (attr.appEventType) {
                case this.ChangeTheme:
                    this.changeTheme(attr);
                    break;
                case this.SoftwareKeyboardShowed:
                    this.keyboardShowed = true;
                    this.proportion = attr.proportion;
                    this._handleSoftwareKeyboardShowed();
                    break;
                case this.SoftwareKeyboardHided:
                    this.keyboardShowed = false;
                    this.handleSoftwareKeyboardHided();
                    break;
                case this.AddDisplayEvent:
                    DocumentEventInterceptor.addDisplayEvent(attr.eventType);
                    break;
                case this.RemoveDisplayEvent:
                    DocumentEventInterceptor.removeDisplayEvent(attr.eventType);
                    break;
            }
        },

        changeTheme: function (attr) {
            var materializeThemeNode = $("#materialize_theme");
            var adoreThemeNode = $("#adore_theme");
            materializeThemeNode.attr("href", attr.materializeTheme);
            adoreThemeNode.attr("href", attr.adoreTheme);
        },

        _handleSoftwareKeyboardShowed: function () {
            if (this.keyboardShowed === true) {
                var keyboardHeightProportion = this.proportion;//软键盘高度比例
                //console.log("proportion["+keyboardHeightProportion+"]");
                var element = document.activeElement;
                var widget = WidgetHelper.findWidgetByTarget(element);
                if (widget && widget.id.indexOf("Text", 0) !== -1) {//只有文本框才处理软键盘遮挡输入域
                    //计算softKeyboard的高度
                    var softKeyboardHeight = keyboardHeightProportion * window.innerHeight;
                    this.scroll(widget.panelID, widget.jqueryNode[0], softKeyboardHeight);
                }
            }
        },

        handleSoftwareKeyboardHided: function () {
            $("#softKeyboardDiv").remove();
            if (lastPanelID) {
                $("#" + lastPanelID).css("overflow-y", lastOverflowYValue);
                lastPanelID = null;
            }
        },

        /**
         * 获取元素相对于屏幕下方的的距离
         * */
        getElementViewBottom: function (element) {
            var top = element.getBoundingClientRect().top;
            return window.innerHeight - top - element.offsetHeight;
        },

        /**
         * 控制滚动条往上滚，不让软键盘遮挡textNode
         * */
        scroll: function (panelID, textNode, softKeyboardHeight) {
            if (lastPanelID !== panelID) {
                this.handleSoftwareKeyboardHided();
            }
            var top = this.getElementViewBottom(textNode);
            var i;//i用于for循环
            if (softKeyboardHeight - top >= 1) {//软键盘已经把输入域遮挡
                var containerNode = $("#" + panelID);
                lastOverflowYValue = containerNode.css("overflow-y");
                lastPanelID = panelID;
                //小窗口交易只需滚小窗口的内容区即可
                if ($("div.adore_shell_content").length !== 0) {
                    var adoreShells = $("div.adore_shell_content");
                    for (i = 0; i < adoreShells.length; i++) {
                        if ($(adoreShells[i]).find("#" + panelID).length !== 0) {
                            containerNode = $(adoreShells[i]);
                            break;
                        }
                    }
                }
                var container = containerNode[0];
                containerNode.css({"overflow-y": "auto"});
                //往上滚
                container.scrollTop = container.scrollTop + (softKeyboardHeight - top);
                var afterTop = this.getElementViewBottom(textNode);
                if (softKeyboardHeight - afterTop >= 1) {
                    //滚了一次之后，软键盘还是遮挡textNode，往container容器添加一个高度为softKeyboardHeight的透明div
                    var tempDivNode = $("<div></div>").css({
                        height: softKeyboardHeight + "px",
                        width: "1px"
                    }).attr({id: "softKeyboardDiv"});

                    //滚动区是绝对布局的情况下，需要定位空div的top位置让他处于滚动区的最底部
                    if (containerNode.css("position") === "absolute") {
                        var children = containerNode.children();
                        var totalHeight = 0;
                        for (i = 0; i < children.length; i++) {
                            totalHeight += children[i].offsetHeight;
                        }
                        var tempDivTop = container.offsetHeight >= totalHeight ? container.offsetHeight : totalHeight;
                        tempDivNode.css({
                            position: "absolute",
                            top: tempDivTop + "px"
                        });
                    }
                    tempDivNode.appendTo(container);
                    //再往上滚
                    container.scrollTop = container.scrollTop + (softKeyboardHeight - afterTop);
                }
            }
        }
    };
});
