import { Cell, Graph } from "@antv/x6";
import { printHandle } from "./index";
import { TopicType } from "@/enum";
import { MindMapProjectInfo } from "@/types";
import {
  handleInsertText,
  handlePaste,
  handleCopy,
  handleCreateEdge,
  handleDuplicate,
  handleSetAttr,
  setCellZIndex,
  alignCell,
  handleGroup,
  handleUnGroup,
  handleMove,
  handleLock,
  handleUnLock,
} from "./hander";

import {
  addPeerTopic,
  addChildrenTopic,
  deleteTopics,
  handleMindmapPaste,
} from "./mindmapHander";

export const bindKeys = (graph: Graph) => {
  // Ctrl + A 全选
  graph.bindKey("ctrl+a", (e: KeyboardEvent) => {
    graph.getCells().forEach((cell) => {
      if (!cell.getData()?.isPage) {
        graph.select(cell);
      }
    });
  });

  // Ctrl + F 查找替换 todo
  graph.bindKey("ctrl+f", (e: KeyboardEvent) => {});

  // Ctrl + + 放大
  graph.bindKey("ctrl+=", (e: KeyboardEvent) => {
    graph.zoomTo(graph.zoom() + 0.1);
  });

  // Ctrl + - 缩小
  graph.bindKey("ctrl+-", (e: KeyboardEvent) => {
    graph.zoomTo(graph.zoom() - 0.1);
  });

  // esc 取消
  graph.bindKey("esc", (e: KeyboardEvent) => {
    graph.cleanSelection();
    graph.trigger("cancel");
  });

  // Ctrl + P 打印
  graph.bindKey("ctrl+p", (e: KeyboardEvent) => {
    graph.toPNG((dataUri) => {
      printHandle(dataUri);
    });
  });

  // T 插入文本
  graph.bindKey("t", (e: KeyboardEvent) => {
    handleInsertText(graph);
  });

  // L 开启插入边线
  graph.bindKey("l", (e: KeyboardEvent) => {
    handleCreateEdge(graph);
  });

  // Ctrl + K 插入链接
  graph.bindKey("ctrl+k", (e: KeyboardEvent) => {
    // todo
  });

  // Ctrl+Alt+M 插入评论
  graph.bindKey("ctrl+alt+m", (e: KeyboardEvent) => {
    // todo
  });

  // Ctrl+B 加粗
  graph.bindKey("ctrl+b", (e: KeyboardEvent) => {
    handleSetAttr(graph, "text.bold", true);
  });

  // Ctrl+I 斜体
  graph.bindKey("ctrl+i", (e: KeyboardEvent) => {
    handleSetAttr(graph, "text.italic", true);
  });

  // Ctrl+U 下划线
  graph.bindKey("ctrl+u", (e: KeyboardEvent) => {
    handleSetAttr(graph, "text.textDecoration", "underline");
  });

  // Ctrl+c 复制
  graph.bindKey("ctrl+c", (e: KeyboardEvent) => {
    handleCopy(graph);
  });

  // Ctrl+x 剪切
  graph.bindKey("ctrl+x", (e: KeyboardEvent) => {
    graph.cut(graph.getSelectedCells());
  });

  // Ctrl+v 粘贴
  graph.bindKey("ctrl+v", (e: KeyboardEvent) => {
    handlePaste(graph);
  });

  // Ctrl+z 撤销
  graph.bindKey("ctrl+z", (e: KeyboardEvent) => {
    graph.undo();
  });

  // Ctrl+y 重做
  graph.bindKey("ctrl+y", (e: KeyboardEvent) => {
    graph.redo();
  });

  // Ctrl+d 复用
  graph.bindKey("ctrl+d", (e: KeyboardEvent) => {
    e.preventDefault();
    handleDuplicate(graph);
  });

  // Ctrl+] 置于顶层
  graph.bindKey("ctrl+]", (e: KeyboardEvent) => {
    setCellZIndex("top", graph.getSelectedCells());
  });

  // Ctrl+[ 置于底层
  graph.bindKey("ctrl+[", (e: KeyboardEvent) => {
    setCellZIndex("bottom", graph.getSelectedCells());
  });

  // Ctrl+shift+] 上移一层
  graph.bindKey("ctrl+shift+]", (e: KeyboardEvent) => {
    setCellZIndex("up", graph.getSelectedCells());
  });

  // Ctrl+shift+] 下移一层
  graph.bindKey("ctrl+shift+[", (e: KeyboardEvent) => {
    setCellZIndex("down", graph.getSelectedCells());
  });

  // Ctrl+l 锁定
  graph.bindKey("ctrl+l", (e: KeyboardEvent) => {
    e.preventDefault();
    handleLock(graph);
  });

  // Ctrl+shift+l 解锁
  graph.bindKey("ctrl+shift+l", (e: KeyboardEvent) => {
    e.preventDefault();
    handleUnLock(graph);
  });

  // Ctrl+g 组合
  graph.bindKey("ctrl+g", (e: KeyboardEvent) => {
    e.preventDefault();
    handleGroup(graph, graph.getSelectedCells());
  });

  // Ctrl+shift+g 解组
  graph.bindKey("ctrl+shift+g", (e: KeyboardEvent) => {
    e.preventDefault();
    handleUnGroup(graph);
  });

  // ↑ 向上移动
  graph.bindKey("up", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "y", -10);
    }
  });

  // ↓ 向下移动
  graph.bindKey("down", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "y", 10);
    }
  });

  // ← 向左移动
  graph.bindKey("left", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "x", -10);
    }
  });

  // → 向右移动
  graph.bindKey("right", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "x", 10);
    }
  });

  // shift+← 向左微移
  graph.bindKey("shift+left", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "x", -1);
    }
  });

  // shift+→ 向右微动
  graph.bindKey("shift+right", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "x", 1);
    }
  });

  // shift+↑ 向上微动
  graph.bindKey("shift+up", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.stopPropagation();
      e.preventDefault();
      handleMove(graph, "y", -1);
    }
  });

  // shift+↓ 向下微动
  graph.bindKey("shift+down", (e: KeyboardEvent) => {
    if (graph.getSelectedCells().length) {
      e.preventDefault();
      handleMove(graph, "y", 1);
    }
  });

  // delete/backspace 删除
  graph.bindKey(["delete", "backspace"], (e: KeyboardEvent) => {
    graph.removeCells(graph.getSelectedCells());
  });

  // Alt+l 左对齐
  graph.bindKey("alt+l", (e: KeyboardEvent) => {
    alignCell("left", graph.getSelectedCells());
  });

  // Alt+r 右对齐
  graph.bindKey("alt+r", (e: KeyboardEvent) => {
    alignCell("right", graph.getSelectedCells());
  });

  // Alt+c 居中对齐
  graph.bindKey("alt+c", (e: KeyboardEvent) => {
    alignCell("hcenter", graph.getSelectedCells());
  });

  // Alt+t 顶部对齐
  graph.bindKey("alt+t", (e: KeyboardEvent) => {
    alignCell("top", graph.getSelectedCells());
  });

  // Alt+m 垂直居中对齐
  graph.bindKey("alt+m", (e: KeyboardEvent) => {
    alignCell("vcenter", graph.getSelectedCells());
  });

  // Alt+b 底部对齐
  graph.bindKey("alt+b", (e: KeyboardEvent) => {
    alignCell("bottom", graph.getSelectedCells());
  });

  // Alt+h 水平平均分布
  graph.bindKey("alt+h", (e: KeyboardEvent) => {
    alignCell("h", graph.getSelectedCells());
  });

  // Alt+v 垂直平均分布
  graph.bindKey("alt+v", (e: KeyboardEvent) => {
    alignCell("v", graph.getSelectedCells());
  });
};

export const bindMindmapKeys = (
  graph: Graph,
  mindProjectInfo?: MindMapProjectInfo,
  setMindProjectInfo?: (info: MindMapProjectInfo) => void
) => {
  // Enter 增加同级主题
  graph.bindKey("enter", (e: KeyboardEvent) => {
    e.preventDefault();
    const branch = graph
      .getSelectedCells()
      .find((cell) => cell.data?.type !== TopicType.sub && cell.isNode());
    if (branch) {
      addPeerTopic(branch, graph, setMindProjectInfo);
      return;
    }
    const sub = graph
      .getSelectedCells()
      .find((cell) => cell.data?.type === TopicType.sub && cell.isNode());
    if (sub) {
      sub.isNode() && addPeerTopic(sub, graph, setMindProjectInfo);
    }
  });

  // Tab 增加子主题
  graph.bindKey(["tab", "insert"], (e: KeyboardEvent) => {
    e.preventDefault();
    const node = graph.getSelectedCells().find((cell) => cell.isNode());
    if (node) {
      node.isNode() && addChildrenTopic(node, setMindProjectInfo, graph);
    }
  });

  // delete 删除
  graph.bindKey("delete", (e: KeyboardEvent) => {
    e.preventDefault();
    const topicIds = graph
      .getSelectedCells()
      .filter((cell) => cell.isNode)
      .map((cell) => cell.id);
    deleteTopics(topicIds, setMindProjectInfo);
  });

  // Ctrl + A 全选
  graph.bindKey("ctrl+a", (e: KeyboardEvent) => {
    const cells = graph.getNodes();
    graph.select(cells);
  });

  // Ctrl + + 放大
  graph.bindKey("ctrl+=", (e: KeyboardEvent) => {
    graph.zoomTo(graph.zoom() + 0.1);
  });

  // Ctrl + - 缩小
  graph.bindKey("ctrl+-", (e: KeyboardEvent) => {
    graph.zoomTo(graph.zoom() - 0.1);
  });

  // esc 取消
  graph.bindKey("esc", (e: KeyboardEvent) => {
    graph.cleanSelection();
    graph.trigger("cancel");
  });

  // Ctrl+Alt+M 插入评论
  graph.bindKey("ctrl+alt+m", (e: KeyboardEvent) => {
    // todo
  });

  // Ctrl+B 加粗
  graph.bindKey("ctrl+b", (e: KeyboardEvent) => {
    handleSetAttr(graph, "text.bold", true);
  });

  // Ctrl+I 斜体
  graph.bindKey("ctrl+i", (e: KeyboardEvent) => {
    handleSetAttr(graph, "text.italic", true);
  });

  // Ctrl+U 下划线
  graph.bindKey("ctrl+u", (e: KeyboardEvent) => {
    handleSetAttr(graph, "text.textDecoration", "underline");
  });

  // Ctrl+c 复制
  graph.bindKey("ctrl+c", (e: KeyboardEvent) => {
    localStorage.setItem("mindmap-copy-data", JSON.stringify(graph.getSelectedCells()));
    navigator.clipboard.writeText("  ");
  });

  // Ctrl+x 剪切
  graph.bindKey("ctrl+x", (e: KeyboardEvent) => {
    graph.cut(graph.getSelectedCells());
  });

  // Ctrl+v 粘贴
  graph.bindKey("ctrl+v", (e: KeyboardEvent) => {
    setMindProjectInfo && handleMindmapPaste(graph, setMindProjectInfo);
  });

  // Ctrl+z 撤销
  graph.bindKey("ctrl+z", (e: KeyboardEvent) => {
    graph.undo();
  });

  // Ctrl+y 重做
  graph.bindKey("ctrl+y", (e: KeyboardEvent) => {
    graph.redo();
  });
};
