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

        addDisplayEvent: function (eventType) {
            if (displayEvents.indexOf(eventType) === -1) {
                displayEvents.push(eventType);
            }
        },

        removeDisplayEvent: function (eventType) {
            for (var i = 0; i < displayEvents.length; i++) {
                if (eventType === displayEvents[i]) {
                    displayEvents.splice(i, 1);
                }
            }
        },

        sendDisplayEvent: function (eventType) {
            if (displayEvents.indexOf(eventType) !== -1) {
                // 触发DisplayEvent
                EventHub.dispatchEvent(EventHub.MSG_TYPE.DISPLAY, {evtType: eventType});
            }
        },

        initInterceptor: function () {
            var own = this;
            $(document).on("keydown", function (e) {
                //对keydown的拦截监听
                var widget = WidgetHelper.findWidgetByTarget(e.target);
                if (widget) {
                    if (eventTypeMap.indexOf(0x2C) != -1) {
                        widget._handleKeyDownEvent(e, true);
                    } else {
                        widget._handleKeyDownEvent(e, false);
                    }
                }
                own.sendDisplayEvent(EventHub.KEY_DONW);
            }).on('keyup', function (e) {
                //对keyup拦截监听
                var widget = WidgetHelper.findWidgetByTarget(e.target);
                if (widget)
                    widget._handleKeyUpEvent(e);
                own.sendDisplayEvent(EventHub.KEY_UP);
            }).on('mousedown', function (e) {
                //对mousedown的拦截监听
                var widget = WidgetHelper.findWidgetByTarget(e.target);
                if (widget)
                    widget._handleMouseDownEvent(e);
                own.sendDisplayEvent(EventHub.MOUSE_DOWN);
            }).on('mouseup', function (e, touchToClick) {
                //对mouseup拦截监听
                var widget = WidgetHelper.findWidgetByTarget(e.target);
                if (widget) {
                    if (touchToClick) {
                        e.button = 0;
                    }
                    widget._handleMouseUpEvent(e);
                }
                own.sendDisplayEvent(EventHub.MOUSE_UP);
            }).on('touchmove', function (e) {
                own.sendDisplayEvent(EventHub.TOUCH_MOVE);
            });

            /*
             * 距离很小的拖拽事件识别成点击事件  begin
             * */
            window.addEventListener("touchstart", function (e) {
                //console.log("touchstart");
                touchStartTimeStamp = e.timeStamp;
                touchStartTarget = e.target;
                touchHadMoved = false;
                touchStartY = e.changedTouches[0].pageY;
                touchStartX = e.changedTouches[0].pageX;
            });

            window.addEventListener("touchmove", function (e) {
                //console.log("touchmove");
                touchHadMoved = true;
            });

            window.addEventListener("touchend", function (e) {
                if (!touchHadMoved || e.target !== touchStartTarget) {
                    //console.log("touchend click[false]");
                    return;
                }
                var changeY = e.changedTouches[0].pageY - touchStartY;
                var changeX = e.changedTouches[0].pageX - touchStartX;
                /**转成非负数**/
                changeX = changeX < 0 ? changeX * -1 : changeX;
                changeY = changeY < 0 ? changeY * -1 : changeY;
                var isClick = false;
                var touchTime = e.timeStamp - touchStartTimeStamp;
                if (touchTime <= 900 && changeX <= 80 && changeY <= 80) {
                    isClick = true;
                }
                //console.log("touchend  time[" + (touchTime) + "] isClick[" + isClick + "] changeY[" + changeY + "] changeX[" + changeX + "]");
                if (isClick) {
                    var widget = WidgetHelper.findWidgetByTarget(e.target);
                    if (widget) {
                        if (widget.id.indexOf("TreeItem", 0) !== -1 || widget.id.indexOf("TabItem", 0) !== -1
                            || widget.id.indexOf("MessageDialog", 0) !== -1) {
                            $(e.target).click();
                        } else {
                            $(e.target).trigger("mouseup", "true");
                        }
                    } else {
                        $(e.target).click();
                    }
                }
            });
            // 距离很小的拖拽事件识别成点击事件  end

        }
    };
})