/**
 * Created by Mai on 2017/5/18.
 */

SHEET_CONTEXTMENU = {
    /**
     * @param spreadObj: Dom Element for create GC.Spread
     * @param spread: GC.Spread created in spreadOBj
     * @param menuObj: Dom Element for contextMenu, eg.:
     * <ul id="spreadContextMenu" class="dropdown-menu" role="menu" style="display: none">
     * <li><a class="dropdown-item localize" data-action="cut">剪切</a></li>
     * <li><a class="dropdown-item localize" data-action="copy">复制</a></li>
     * <li><a class="dropdown-item localize" data-action="paste">粘贴</a></li>
     * <li class="context-header divider"></li>
     * <li class="context-header"><a class="dropdown-item localize" data-action="insert">插入</a></li>
     * <li class="context-header"><a class="dropdown-item localize" data-action="delete">删除</a></li>
     * <li class="context-cell divider"></li>
     * <li class="context-cell context-merge"><a class="dropdown-item localize" data-action="merge">合并</a></li>
     * <li class="context-cell context-unmerge"><a class="dropdown-item localize" data-action="unmerge">取消合并</a></li>
     * </ul>
     * menu must be a, define menu's event must use bind method
     * @returns {control}; use to bind contextmenu event, see eventName
     */
    createNew: function (spreadObj, spread, menuObj) {
        var tools = {
            getHitTest: function (pageX, pageY, sheet) {
                var offset = spreadObj.offset(),
                    x = pageX - offset.left,
                    y = pageY - offset.top;
                return sheet.hitTest(x, y);
            },
            getInfo: function (e) {
                var info = {};
                info.sheet = spread.getActiveSheet();
                info.target = tools.getHitTest(e.pageX, e.pageY, info.sheet);
                info.hitTestType = info.target.hitTestType;
                info.isHideContextMenu = false;
                info.contextMenu = menuObj;
                return info;
            },
            getCellInSelections: function (selections, row, col) {
                var count = selections.length, range;
                for (var i = 0; i < count; i++) {
                    range = selections[i];
                    if (range.contains(row, col)) {
                        return range;
                    }
                }
                return null;
            },
            hideSpreadContextMenu: function () {
                menuObj.hide();
                $(document).off("click.contextmenu");
            },
            processSpreadContextMenu: function (e) {
                var info = tools.getInfo(e), selections = info.sheet.getSelections(), row = info.target.row, col = info.target.col;

                // reSet Selections when no selections
                if (info.hitTestType === GC.Spread.Sheets.SheetArea.colHeader) {
                    if (tools.getCellInSelections(selections, row, col) === null) {
                        info.sheet.setSelection(-1, col, info.sheet.getRowCount(), 1);
                    }
                } else if (info.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader) {
                    if (tools.getCellInSelections(selections, row, col) === null) {
                        info.sheet.setSelection(row, -1, 1, info.sheet.getColumnCount());
                    }
                } else if (info.hitTestType === GC.Spread.Sheets.SheetArea.viewport) {
                    if (tools.getCellInSelections(selections, row, col) === null) {
                        info.sheet.clearSelection();
                        info.sheet.endEdit();
                        info.sheet.setActiveCell(row, col);
                    }
                } else if (info.hitTestType === GC.Spread.Sheets.SheetArea.corner) {
                    info.sheet.setSelection(-1, -1, info.sheet.getRowCount(), info.sheet.getColumnCount());
                };

                menuObj.data("sheetArea", info.hitTestType);
                if (info.isHideContextMenu) {
                    tools.hideSpreadContextMenu();
                } else {
                    if (event.beforeShowContextMenu) {
                        event.beforeShowContextMenu(info);
                    };

                    menuObj.css({left: e.pageX, top: e.pageY});
                    menuObj.show();

                    $(document).on("click.contextmenu", function () {
                        if ($(event.target).parents(menuObj).length === 0) {
                            tools.hideSpreadContextMenu();
                        }
                    });
                }
            }
        };
        var event = {
            beforeShowContextMenu: null
        };

        var control = function () {
            spreadObj.bind("contextmenu", tools.processSpreadContextMenu);
            spreadObj.mouseup(function (e) {
                // hide context menu when the mouse down on SpreadJS
                if (e.button !== 2) {
                    tools.hideSpreadContextMenu();
                }
            });
            $(menuObj, 'a').bind('click', tools.hideSpreadContextMenu);
            $(document).on("contextmenu", function (e) {
                var evt = window.event || e;
                if (!$(evt.target).data('contextmenu')) {
                    evt.preventDefault();
                    return false;
                }
            });
        };
        control.prototype.bind = function (eventName, event) {
            event[eventName] = event;
        };

        return new control();
    },
    /**
     * beforeShowContextMenu: right click spread, menu has different with spread.sheet.selections
     * @Param info: see return of tools.getInfo
     */
    eventName: {
        beforeShowContextMenu: 'beforeShowContextMenu'
    }
}