'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2019/11/14
 * @version
 */

const lockUtil = (() => {
    // 从地址栏获取是否锁定
    function getLocked() {
        const search = window.location.search;
        const reg = /locked=(false|true)/;
        const match = search.match(reg);
        return match ? JSON.parse(match[1]) : true;
    }
    function lockTools($range, locked) {
        const $btns = $range.find('.lock-btn-control');
        const toolList = [];
        for (const $btn of $btns) {
            toolList.push({$ref: $($btn), type: 'button'});
        }
        const $texts = $range.find('.lock-text-control');
        for (const $text of $texts) {
            toolList.push({$ref: $($text), type: 'text'});
        }
        toolList.forEach(item => {
            switch (item.type) {
                case 'button':
                    locked ? item.$ref.addClass('disabled') : item.$ref.removeClass('disabled');
                    break;
                case 'text':
                    item.$ref.prop('readOnly', locked);
                    break;
            }
        });
    }
    function lockSpreads(spreads, locked) {
        if (!locked) {
            return;
        }
        spreads.forEach(spread => {
            spread.unbind(GC.Spread.Sheets.Events.ButtonClicked);
            const sheetCount = spread.getSheetCount();
            for(let i = 0; i < sheetCount; i++){
                const sheet = spread.getSheet(i);
                sheet.unbind(GC.Spread.Sheets.Events.ButtonClicked);
                sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
                sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
                sheet.unbind(GC.Spread.Sheets.Events.RangeChanged);
                sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanging);
                sheet.unbind(GC.Spread.Sheets.Events.ClipboardChanged);
                sheet.unbind(GC.Spread.Sheets.Events.CellDoubleClick);
                sheet.unbind(GC.Spread.Sheets.Events.CellClick);
                sheet.unbind(GC.Spread.Sheets.Events.ValueChanged);
                sheet.suspendPaint();
                sheet.suspendEvent();
                sheet.options.isProtected = true;
                const rowCount = sheet.getRowCount();
                const colCount = sheet.getColumnCount();
                for(let row = 0; row < rowCount; row++){
                    for(let col = 0; col < colCount; col++){
                        sheet.getCell(row, col).locked(true);
                    }
                }
                sheet.resumePaint();
                sheet.resumeEvent();
            }
        });
    }
    function lockURL(locked, $url) {
        const originURL = $url.prop('href');
        const originLocked = !locked;
        const reg = new RegExp(`locked=${originLocked}`);
        const curURL = reg.test(originURL) ? originURL.replace(reg, `locked=${locked}`) : `${originURL}&locked=${locked}`;
        $url.prop('href', curURL);
    }
    // 库列表页面，锁定按钮点击操作
    function handleLockClick($lock) {
        const curLocked = !$lock.data().locked;
        $lock.data('locked', curLocked);
        const innerHtml = curLocked ? '<i class="fa fa-unlock-alt"></i>' : '<i class="fa fa-lock"></i>';
        $lock.html(innerHtml);
        const title = curLocked ? '解锁' : '锁定';
        $lock.prop('title', title);
        const $url = $lock.parent().parent().children(':first-child').children(':first-child');
        lockURL(curLocked, $url);
        const $range = $lock.parent().parent();
        lockTools($range, curLocked);
    }
    function lockSpreadsAndTools(spreads, $range, locked) {
        if (!locked) {
            return;
        }
        lockSpreads(spreads, locked);
        lockTools($range, locked);
    }

    return {
        getLocked,
        lockTools,
        lockSpreads,
        lockURL,
        handleLockClick,
        lockSpreadsAndTools
    }
})();
