import { CircleNode, CircleNodeModel, ConnectRule, GraphModel, h, NodeConfig } from '@logicflow/core'
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
import { nodeStyleHandle } from '../tool'

class EndModel extends CircleNodeModel {
  static extendKey = 'EndModel';
  constructor(data: NodeConfig, graphModel: GraphModel) {
    if (!data.text) {
      data.text = ''
    }
    if (data.text && typeof data.text === 'string') {
      data.text = {
        value: data.text,
        x: data.x,
        y: data.y + 40
      }
    }
    super(data, graphModel)
  }

  setAttributes(): void {
    this.r = 20
  }

  getConnectedSourceRules(): ConnectRule[] {
    const rules = super.getConnectedSourceRules()
    const notAsSource = {
      message: '结束节点不能作为边的起点',
      validate: () => false
    }
    rules.push(notAsSource)
    return rules
  }

  getNodeStyle(): {
    [x: string]: any;
    r?: number;
    width?: number;
    height?: number;
    fill?: string;
    stroke?: string;
    strokeWidth?: number;
  } {
    const style = super.getNodeStyle()
    // 设置样式
    if (themeConfig.value.isIsDark) {
      style.fill = "#191919";
      style.stroke = "#fff"
    } else {
      style.fill = "#fff";
      style.stroke = "#3C50E0";
    }
    return nodeStyleHandle(this, style)
  }
}

class EndView extends CircleNode {
  static extendKey = 'EndView';
  getAnchorStyle(): {} {
    return {
      visibility: 'hidden'
    }
  }

  getShape(): h.JSX.Element {
    const { model } = this.props
    const style = model.getNodeStyle()
    const { x, y, r } = model
    const outCircle = super.getShape()
    return h(
      'g',
      {},
      outCircle,
      h('circle', {
        ...style,
        cx: x,
        cy: y,
        r: r - 5
      })
    )
  }
}

const End = {
  type: 'hotline:end',
  view: EndView,
  model: EndModel
}

export { EndView, EndModel }
export default End
