// 自定义组件
import { Node, Rect, Point, Direction, registerNode } from '@topology/core';
import { getDatas,ces } from "@/utils/topology/registerIconssetting.js";
import {assembleSvg} from "./setting"
/**
 * 普通icon作为图标的方法一下
 */
export function myShape(ctx, node) {
  ctx.beginPath();
  // 必须判空再填充
  (node.fillStyle || node.bkType) && ctx.fill();
  ctx.stroke();
}
export function myTextRect(node) {
  node.textRect = new Rect(
    node.rect.x + 10,
    node.rect.y + (node.rect.height * 2) / 3,
    node.rect.width - 20,
    node.rect.height / 3 - 5
  );
  node.fullTextRect = node.rect;
}
export function myIconRect(node) {
  // console.log(node)
  // node.iconRect = new Rect(node.rect.x, node.rect.y + 10, node.rect.width, node.rect.height);
  // node.fullIconRect = node.rect;
}

export function myAnchors(node) {
  // for (let i = 1; i <= 9; i += 1) {
  //   node.anchors.push(new Point(node.rect.x, node.rect.y + node.rect.height * (i * 0.1), Direction.Left));
  //   node.anchors.push(new Point(node.rect.x + node.rect.width * (i * 0.1), node.rect.y, Direction.Up));
  //   node.anchors.push(new Point(node.rect.x + node.rect.width, node.rect.y + node.rect.height * (i * 0.1), Direction.Right));
  //   node.anchors.push(new Point(node.rect.x + node.rect.width * (i * 0.1), node.rect.y + node.rect.height, Direction.Bottom));
  // }
  //中间点
  node.anchors.push(new Point(node.rect.x, node.rect.y + node.rect.height / 2, Direction.Left));
  node.anchors.push(new Point(node.rect.x + node.rect.width / 2, node.rect.y, Direction.Up));
  node.anchors.push(new Point(node.rect.x + node.rect.width, node.rect.y + node.rect.height / 2, Direction.Right));
  node.anchors.push(new Point(node.rect.x + node.rect.width / 2, node.rect.y + node.rect.height, Direction.Bottom));
  // demo，其他自定义锚点。这里只是示例。
  // for (let i = 10; i < 360; i += 10) {
  //   if (i % 90 === 0) {
  //     continue;
  //   }

  //   const direction = Math.floor(i / 90);
  //   const pt = new Point(
  //     node.rect.center.x + (Math.sin((i / 180) * Math.PI) * node.rect.width) / 2,
  //     node.rect.center.y + (Math.cos((i / 180) * Math.PI) * node.rect.height) / 2,
  //     direction
  //   );
  //   pt.hidden = true;
  //   node.anchors.push(pt);
  // }
}
export function getRegisterNode(name) {
  registerNode(name, myShape, myAnchors, myIconRect, null);
}
export function registerIcons(){
  for (let i of getDatas()) {
    if (i.children) {
      for (let j of i.children) {
        getRegisterNode(j.data.name);
      }
    }
  }
}

export function registerSvgs(){
  for (let i of assembleSvg()) {
    if (i.children) {
      for (let j of i.children) {
        getSvgToCavansRegisterNode(j.data.name);
      }
    }
  }
}
export function getSvgToCavansRegisterNode(name) {
  registerNode(name, myIconRect, myAnchors, myIconRect, null);
}
export function svgTocavansShape(ctx, node) {
  node.tmp={};
      node.tmp.x=node.rect.x;
      node.tmp.y=node.rect.y;
      node.tmp.w=node.rect.width/100;
      node.tmp.h=node.rect.height/100;
    
      ctx.beginPath();
        node.tmp.lastX=node.tmp.x+0*node.tmp.w;
        node.tmp.lastY=node.tmp.y+0*node.tmp.h;
        ctx.moveTo(node.tmp.lastX, node.tmp.lastY);
        node.tmp.lastX=node.tmp.x+100*node.tmp.w;
        node.tmp.lastY=node.tmp.y+100*node.tmp.h;
        ctx.lineTo(node.tmp.lastX, node.tmp.lastY);
        ctx.stroke();
        ctx.beginPath();
        node.tmp.lastX=node.tmp.x+100*node.tmp.w;
        node.tmp.lastY=node.tmp.y+0*node.tmp.h;
        ctx.moveTo(node.tmp.lastX, node.tmp.lastY);
        node.tmp.lastX=node.tmp.x+0*node.tmp.w;
        node.tmp.lastY=node.tmp.y+100*node.tmp.h;
        ctx.lineTo(node.tmp.lastX, node.tmp.lastY);
        ctx.stroke();node.tmp=null
}