import { Cell, Graph } from "@antv/x6";
import { TopicType } from "@/enum";
import { MindMapProjectInfo } from "@/types";
import { handleSetAttr } from "@/utils/hander";

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

export const bindMindmapKeys = (
  graph: Graph,
  setMindProjectInfo?: (data: any) => 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, graph, setMindProjectInfo);
    }
  });

  // delete 删除
  graph.bindKey("delete", (e: KeyboardEvent) => {
    e.preventDefault();
    const topicIds = graph
      .getSelectedCells()
      .filter((cell) => {
        return cell.isNode() && !(cell.data?.ignoreDrag || cell.data?.lock )
      })
      .map((cell) => cell.id);
    deleteTopics(topicIds, graph, 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) => {
    const cells = graph.getSelectedCells();
    localStorage.setItem(
      "mindmap-copy-data",
      JSON.stringify(cells)
    );
    navigator.clipboard.writeText("  ");
    // 剪切后删除节点
    const topicIds = graph
      .getSelectedCells()
      .filter((cell) => {
        return cell.isNode() && !(cell.data?.ignoreDrag || cell.data?.lock )
      })
      .map((cell) => cell.id);
    deleteTopics(topicIds, graph, setMindProjectInfo);
  });

  // 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();
  });
};
