import { useChartEditStore } from '@/store/modules/chart/chartEditStore';
import { throttle, debounce } from 'lodash-es';
import keymaster from 'keymaster';
import { setKeyboardDressShow } from '../utils/chart-util';

// Keymaster可以支持识别以下组合键： ⇧，shift，option，⌥，alt，ctrl，control，command，和⌘

const winCtrlMerge = e => `ctrl+${e}`;
const winShiftMerge = e => `shift+${e}`;
const winAltMerge = e => `alt+${e}`;

export const winKeyboardValue = {
  up: winCtrlMerge('up'),
  right: winCtrlMerge('right'),
  down: winCtrlMerge('down'),
  left: winCtrlMerge('left'),
  copy: winCtrlMerge('c'),
  cut: winCtrlMerge('x'),
  parse: winCtrlMerge('v'),
  delete: 'delete',
  back: winCtrlMerge('z'),
  forward: winCtrlMerge(winShiftMerge('z')),
  group: winCtrlMerge('g'),
  unGroup: winCtrlMerge(winShiftMerge('g')),
  lock: winCtrlMerge('l'),
  unLock: winCtrlMerge(winShiftMerge('l')),
  hide: winCtrlMerge('h'),
  show: winCtrlMerge(winShiftMerge('h'))
};

// 这个 Ctrl 后面还是换成了 ⌘
const macCtrlMerge = e => `⌘+${e}`;
const macShiftMerge = e => `⇧+${e}`;
const macAltMerge = e => `⌥+${e}`;

// 待测试
export const macKeyboardValue = {
  up: macCtrlMerge('arrowup'),
  right: macCtrlMerge('arrowright'),
  down: macCtrlMerge('arrowdown'),
  left: macCtrlMerge('arrowleft'),
  copy: macCtrlMerge('c'),
  cut: macCtrlMerge('x'),
  parse: macCtrlMerge('v'),
  delete: macCtrlMerge('backspace'),
  back: macCtrlMerge('z'),
  forward: macCtrlMerge(macShiftMerge('z')),
  group: macCtrlMerge('g'),
  unGroup: macCtrlMerge(macShiftMerge('g')),
  lock: macCtrlMerge('l'),
  unLock: macCtrlMerge(macShiftMerge('l')),
  hide: macCtrlMerge('h'),
  show: macCtrlMerge(macShiftMerge('h'))
};

// Win 快捷键列表
const winKeyList = [
  winKeyboardValue.up,
  winKeyboardValue.right,
  winKeyboardValue.down,
  winKeyboardValue.left,

  winKeyboardValue.delete,
  winKeyboardValue.copy,
  winKeyboardValue.cut,
  winKeyboardValue.parse,

  winKeyboardValue.back,
  winKeyboardValue.forward,

  winKeyboardValue.group,
  winKeyboardValue.unGroup,

  winKeyboardValue.lock,
  winKeyboardValue.unLock,

  winKeyboardValue.hide,
  winKeyboardValue.show
];

// Mac 快捷键列表
const macKeyList = [
  macKeyboardValue.up,
  macKeyboardValue.right,
  macKeyboardValue.down,
  macKeyboardValue.left,

  macKeyboardValue.delete,
  macKeyboardValue.copy,
  macKeyboardValue.cut,
  macKeyboardValue.parse,

  macKeyboardValue.back,
  macKeyboardValue.forward,

  macKeyboardValue.group,
  macKeyboardValue.unGroup,

  macKeyboardValue.lock,
  macKeyboardValue.unLock,

  macKeyboardValue.hide,
  macKeyboardValue.show
];

// 处理键盘记录
const keyRecordHandle = () => {
  // 默认赋值
  window.$KeyboardActive = {
    ctrl: false,
    space: false
  };

  document.onkeydown = e => {
    const { keyCode } = e;
    if (keyCode == 32 && e.target == document.body) e.preventDefault();

    if ([17, 32].includes(keyCode) && window.$KeyboardActive) {
      setKeyboardDressShow(e.keyCode);
      switch (keyCode) {
        case 17:
          window.$KeyboardActive.ctrl = true;
          break;
        case 32:
          window.$KeyboardActive.space = true;
          break;
      }
    }
  };

  document.onkeyup = e => {
    const { keyCode } = e;
    if (keyCode == 32 && e.target == document.body) e.preventDefault();

    if ([17, 32].includes(keyCode) && window.$KeyboardActive) {
      setKeyboardDressShow();
      switch (keyCode) {
        case 17:
          window.$KeyboardActive.ctrl = false;
          break;
        case 32:
          window.$KeyboardActive.space = false;
          break;
      }
    }
  };
};

// 初始化监听事件
export const useAddKeyboard = () => {
  const chartEditStore = useChartEditStore();
  
  const throttleTime = 50;
  const switchHandle = (keyboardValue, e) => {
    switch (e) {
      // ct+↑
      case keyboardValue.up:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setMove('up');
            return false;
          }, throttleTime)
        );
        break;
      // ct+→
      case keyboardValue.right:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setMove('right');
            return false;
          }, throttleTime)
        );
        break;
      // ct+↓
      case keyboardValue.down:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setMove('down');
            return false;
          }, throttleTime)
        );
        break;
      // ct+←
      case keyboardValue.left:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setMove('left');
            return false;
          }, throttleTime)
        );
        break;

      // 删除 delete
      case keyboardValue.delete:
        keymaster(
          e,
          debounce(() => {
            chartEditStore.removeComponentList();
            return false;
          }, throttleTime)
        );
        break;
      // 复制 ct+v
      case keyboardValue.copy:
        keymaster(
          e,
          debounce(() => {
            chartEditStore.setCopy();
            return false;
          }, throttleTime)
        );
        break;
      // 剪切 ct+x
      case keyboardValue.cut:
        keymaster(
          e,
          debounce(() => {
            chartEditStore.setCut();
            return false;
          }, throttleTime)
        );
        break;
      // 粘贴 ct+v
      case keyboardValue.parse:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setParse();
            return false;
          }, throttleTime)
        );
        break;

      // 撤回 ct+z
      case keyboardValue.back:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setBack();
            return false;
          }, throttleTime)
        );
        break;
      // 前进 ct+sh+z
      case keyboardValue.forward:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setForward();
            return false;
          }, throttleTime)
        );
        break;

      // 创建分组 ct+g
      case keyboardValue.group:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setGroup();
            return false;
          }, throttleTime)
        );
        break;
      // 解除分组 ct+sh+g
      case keyboardValue.unGroup:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setUnGroup();
            return false;
          }, throttleTime)
        );
        break;

      // 锁定 ct+l
      case keyboardValue.lock:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setLock();
            return false;
          }, throttleTime)
        );
        break;
      // 解除锁定 ct+sh+l
      case keyboardValue.unLock:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setUnLock();
            return false;
          }, throttleTime)
        );
        break;

      // 隐藏 ct+h
      case keyboardValue.hide:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setHide();
            return false;
          }, throttleTime)
        );
        break;
      // 解除隐藏 ct+sh+h
      case keyboardValue.show:
        keymaster(
          e,
          throttle(() => {
            chartEditStore.setShow();
            return false;
          }, throttleTime)
        );
        break;
    }
  };
  winKeyList.forEach(key => {
    switchHandle(winKeyboardValue, key);
  });
  macKeyList.forEach(key => {
    switchHandle(macKeyboardValue, key);
  });

  keyRecordHandle();
};

// 卸载监听事件
export const useRemoveKeyboard = () => {
  document.onkeydown = () => {};
  document.onkeyup = () => {};

  winKeyList.forEach(key => {
    keymaster.unbind(key);
  });
  macKeyList.forEach(key => {
    keymaster.unbind(key);
  });
};
