import {
  ImageFillType,
  TopicType,
  BorderSize,
  LineType,
  ConnectorType,
  StructureType,
} from "../enum";
import { Edge } from "@antv/x6";
import { merge, cloneDeep } from "lodash-es";
import { MindMapProjectInfo } from "types";

// 通用连接桩
export const ports = {
  groups: {
    top: {
      position: 'top',
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#5F95FF',
          strokeWidth: 1,
          fill: '#fff',
          style: {
            visibility: 'hidden',
          },
        },
      },
    },
    right: {
      position: 'right',
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#5F95FF',
          strokeWidth: 1,
          fill: '#fff',
          style: {
            visibility: 'hidden',
          },
        },
      },
    },
    bottom: {
      position: 'bottom',
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#5F95FF',
          strokeWidth: 1,
          fill: '#fff',
          style: {
            visibility: 'hidden',
          },
        },
      },
    },
    left: {
      position: 'left',
      attrs: {
        circle: {
          r: 4,
          magnet: true,
          stroke: '#5F95FF',
          strokeWidth: 1,
          fill: '#fff',
          style: {
            visibility: 'hidden',
          },
        },
      },
    },
  },
  items: [
    {
      group: 'top',
      args: {
        dy: 2,
      },
    },
    {
      group: 'right',
      args: {
        dx: -2,
      },
    },
    {
      group: 'bottom',
      args: {
        dy: -2,
      },
    },
    {
      group: 'left',
      args: {
        dx: 2,
      },
    },
  ],
}

// 通用数据
export const defaultData = {
  opacity: 100,
  text: {
    fontFamily: "微软雅黑",
    color: "#000000",
    fontSize: 14,
    lineHeight: 1.25,
    textAlign: "center",
    textVAlign: "middle",
    bold: false,
    italic: false,
    underline: false,
    strikethrough: false,
  },
  fill: {
    fillType: "color",
    color1: "#fff",
    color2: "#eee",
    gradientType: "linear",
    gradientValue: 0,
    objectFit: ImageFillType.Fill,
    imageUrl: "",
  },
  stroke: {
    type: "solid",
    color: "#323232",
    width: 1,
  },
  lock: false,
};

// 默认边数据
export const BaseEdge: Edge.Properties = {
  router: {
    name: "manhattan",
    args: {
      excludeShapes: ["page-container-node"],
      padding: 10,
    },
  },
  attrs: {
    line: {
      stroke: "#323232",
      strokeDasharray: LineType.solid,
      strokeWidth: 2,
      sourceMarker: {
        name: "",
      },
      targetMarker: {
        name: "block",
      },
    },
    text: defaultData.text,
  },
  data: {
    connectorType: ConnectorType.Rounded,
  },
};

// 根据主题获取默认数据
export const getDefaultDataByTheme = (themeKey: string) => {
  const theme = {
    pageStyle: {
      background: "#fff",
    },
    nodeStyle: {
      strokeColor: "#000",
      fillColor: "#fff",
      textColor: "#000",
    },
    edgeStyle: {
      strokeColor: "#000",
      textColor: "#000",
    },
  };
  const { nodeStyle, edgeStyle } = theme || {};
  // 找到主题配置，合并默认数据
  return {
    nodeDefaultData: merge(cloneDeep(defaultData), {
      fill: {
        color1: nodeStyle?.fillColor || defaultData.fill.color1,
      },
      stroke: {
        color: nodeStyle?.strokeColor || defaultData.stroke.color,
      },
      text: {
        color: nodeStyle?.textColor || defaultData.text.color
      }
    }),
    edgeDefaultData: merge(cloneDeep(BaseEdge), {
      attrs: {
        line: {
          stroke: edgeStyle?.strokeColor,
        },
        text: {
          color: edgeStyle?.textColor
        }
      }
    }),
    ports
  }
}

// 脑图默认主题数据
export const topicData = {
  ...defaultData,
  type: TopicType.main,
  borderSize: BorderSize.medium,
  fixedWidth: false,
  collapsed: false,
  linkTopicId: undefined,
  edge: {
    color: "#323232",
    width: 3,
  },
  children: [],
  extraModules: undefined,
  links: [],
  summary: undefined,
  isSummary: false,
};

export const defaultProject: MindMapProjectInfo = {
  name: "新建脑图",
  desc: "",
  version: "",
  author: "",
  structure: StructureType.right,
  pageSetting: {
    backgroundType: "color",
    backgroundColor: "#ffffff",
    backgroundImage: "",
    branchY: 10,
    branchX: 44,
    subTopicY: 16,
    subTopicX: 20,
    alignSameTopic: false,
    showWatermark: false,
    watermarkText: "",
  },
  // 主题
  theme: "default",
  topics: [],
};
