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，有连接线
	superiorID_?: string// 上级节点ID，没有连接线
	superiorNodeType?: string// 上级节点类型
	superiorGenerateImg?: string// 上级生成节点图片
	disableDelete?: boolean// 是否禁用删除
	disableCopy?: boolean// 是否禁用复制
	createIndexPosition?: number// 创建索引位置
	isActive?: boolean// 是否激活
	versionImgUpdataList?: Array<any>// 版本节点
}
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.deleteSubordinateAllNodes(id, { isElMessageBox: true })
	}
	/** 添加节点 */
	addNode(node: any) {
		this.stateManager.addNode(node)
		this.stateManager.setActiveNodeID(node.id)
	}

	/** 创建节点 */
	createNode(options: NodeOptions) {
		const superiorID = options?.data?.superiorID
		const superiorID_ = options?.data?.superiorID_// 上级节点ID，用于创建子节点时使用
		//获取上级节点所生成的最后一个node，设置位置为最后一个节点的xy 加上 节点间距
		const superiorGenerateNodes = superiorID?this.stateManager.getSubordNodes(superiorID):[]
		const currentNode = superiorGenerateNodes.find((node) => {
			return (node.data.createIndexPosition === options?.data?.createIndexPosition && options?.data?.createIndexPosition)
		})
		const endGenerateNode = superiorGenerateNodes.reduce((max, current) => {
			return current.data.createIndexPosition > max.data.createIndexPosition ? current : max
		}, superiorGenerateNodes[0])
		let snode = null as any
		if(superiorID){
			snode = this.stateManager.flowManager.getNodeById(superiorID)
		}else if(superiorID_){
			snode = this.stateManager.flowManager.getNodeById(superiorID_)
		}else{
			snode = this.stateManager.flowManager.getLastNode()
		}
		const id = options.id || createId()
		const positionX = options.positionX || 0
		const positionY = options.positionY || 0
		const position = options.position ||
			(
				currentNode ?//当前节点位置，覆盖操作
				currentNode.position :
				endGenerateNode ?//最大子级位置
				{
					x: endGenerateNode.position.x + positionX,
					y: endGenerateNode.position.y + positionY + this.ranksep + 200
				} : snode ?
				{
					x: snode.position.x + snode.dimensions.width + this.nodesep + positionX,
					y: snode.position.y + positionY
				} :
				{
					x: positionX,
					y: positionY
				}
			)
		const data = options?.data || {}
		data['component'] = options.component
		const options_ = {
			id,
			position,
			data
		}
		if(currentNode)this.stateManager.deleteNode(currentNode.id)
		this.addNode(options_)
		return options_;
	}
	/** 创建结果节点 */
	createResultNode(options?: NodeOptions) {
		const options_ = {
			...(options ? options : {}),
			component: NODE_COMPONENT.RESULT_IMAGE,
			positionY: options?.positionY || 0,
			data: {
				tier: NODE_DATATIER.RESULT_IMAGE,
				type: NODE_DATATYPE.RESULT_IMAGE,
				createIndexPosition: options?.data?.createIndexPosition || 1,
				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,
				createIndexPosition: options?.data?.createIndexPosition || 1,
				...(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 copyNode = JSON.parse(JSON.stringify(node))
		const flowNode = this.stateManager.flowManager.getNodeById(id)
		if (!node) return console.warn(`${id}找不到对应节点`)
		if (node.data?.disableCopy) return console.warn(`${id}节点已禁用复制`)
		copyNode.data.data.imageProcessTasks = copyNode.data.data.imageProcessTasks.filter((item) => item.taskId === copyNode.data.data.selectTaskId)
		const node_ = {
			...copyNode,
			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
		node_.data.data.imageProcessTasks.forEach((item) => {
			item.canvasId = null
		})
		this.stateManager.addNode(node_)
	}
	dispose() {}
}
