import { createId } from '../../tools/tools'
import { NODE_DATATYPE, NODE_COMPONENT, NODE_DATATIER } from '../tools/index.d'
interface NodeData {
	type?: string
	component?: any// 节点组件
	data?: object// 节点数据
	tier?: string// 节点层级
	isHeader?: boolean// 是否显示头
	superiorID?: string// 上级节点ID
	disableDelete?: boolean// 是否禁用删除
	disableCopy?: boolean// 是否禁用复制
}
interface NodeOptions {
	id?: string
	position?: { x: number, y: number }
	positionX?: number
	positionY?: number
	component?: any
	data?: NodeData
}// 不可传入type class （内部使用）

export class NodeManager {
	stateManager: any
	vueFlow: any
	nodesep = 100 // 节点间距
	ranksep = 100 // 层级间距
	constructor(options) {
		this.stateManager = options.stateManager;
		this.vueFlow = options.vueFlow
	}

	/** 删除节点 */
	deleteNode(id: string) {
		this.stateManager.deleteNode(id)
	}
	/** 添加节点 */
	addNode(node: any) {
		this.stateManager.addNode(node)
	}

	/** 创建节点 */
	createNode(options: NodeOptions) {
		const superiorID = options?.data?.superiorID
		const snode = superiorID ? this.stateManager.flowManager.getNodeById(superiorID) : this.stateManager.flowManager.getLastNode();
		const id = options.id || createId()
		const positionX = options.positionX || 0
		const positionY = options.positionY || 0
		const position = options.position ||
			(!snode ?
				{ x: positionX, y: positionY } :
				{
					x: snode.position.x + snode.dimensions.width + this.nodesep + positionX,
					y: snode.position.y + positionY
				})
		const data = options?.data || {}
		data['component'] = options.component
		const options_ = {
			id,
			position,
			data
		}
		this.addNode(options_)
		return options_;
	}
	/** 创建结果节点 */
	createResultNode(options?: NodeOptions) {
		const options_ = {
			...(options ? options : {}),
			component: NODE_COMPONENT.RESULT_IMAGE,
			data: {
				tier: NODE_DATATIER.RESULT_IMAGE,
				type: NODE_DATATYPE.RESULT_IMAGE,
				isHeader: true,
				...(options?.data || {}),
			},
		}
		return this.createNode(options_)
	}
	/** 创建卡片选择节点 */
	createCardsSelect(options?: NodeOptions) {
		const options_ = {
			...(options ? options : {}),
			component: NODE_COMPONENT.CARD,
			positionY: 50,
			data: {
				tier: NODE_DATATIER.CARDS_SELECT,
				type: NODE_DATATYPE.CARDS_SELECT,
				...(options?.data || {}),
			},
		}
		return this.createNode(options_)
	}
	/** 创建卡片节点 */
	createCardNode(options?: NodeOptions) {
		const options_ = {
			...(options ? options : {}),
			component: NODE_COMPONENT.CARD,
			data: {
				...(options?.data || {}),
			}
		}
		return this.createNode(options_)
	}
	/** 创建文本节点 */
	createTextNode(options?: NodeOptions) {
		const options_ = {
			...(options ? options : {}),
			component: NODE_COMPONENT.TEXT,
			data: {
				...(options?.data || {}),
			}
		}
		return this.createNode(options_)
	}

	copyNodeById(id: string) {
		const node = this.stateManager.getNodeById(id)
		const flowNode = this.stateManager.flowManager.getNodeById(id)
		if (!node) return console.warn(`${id}找不到对应节点`)
		if (node.data?.disableCopy) return console.warn(`${id}节点已禁用复制`)
		const node_ = {
			...JSON.parse(JSON.stringify(node)),
			id: createId(),
			position: {
				x: node.position.x,
				y: node.position.y + (flowNode?.dimensions?.height || 0) + this.ranksep,
			}
		}
		delete node_.data?.superiorID
		delete node_.data?.disableDelete
		this.stateManager.addNode(node_)
	}
}
