import { CircleNode, CircleNodeModel, ConnectRule, GraphModel, h, NodeConfig } from '@logicflow/core'
import { nodeStyleHandle } from '../tool'

class StartModel extends CircleNodeModel {
  static extendKey = 'StartModel';
  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 = 18
  }

  getConnectedTargetRules (): ConnectRule[] {
    const rules = super.getConnectedTargetRules()
    const notAsTarget = {
      message: '起始节点不能作为边的终点',
      validate: () => false
    }
    rules.push(notAsTarget)
    return rules
  }

  getNodeStyle (): {
    [x: string]: any;
    r?: number;
    fill?: string;
    stroke?: string;
    strokeWidth?: number;
    strokeDasharray?:string;
    } {
    const style = super.getNodeStyle()
    style.fill = '#ffffff'
    style.stroke = '#2c89ff'
    style.strokeDasharray = '6 3'
    return nodeStyleHandle(this, style)
  }
}

class StartView extends CircleNode {
  static extendKey = 'StartNode';

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

const Start = {
  type: 'snaker:start',
  view: StartView,
  model: StartModel
}

export { StartModel, StartView }
export default Start
