<template>
	<div class="flow-canvas">
		<VueFlow
			ref="vueFlow"
			:nodes="nodes"
			:edges="edges"
			:min-zoom="0.1"
			:max-zoom="10"
			:nodes-draggable="nodesDraggable"
			:pan-on-drag="panOnDrag"
			@nodes-initialized="layoutGraph('LR')"
			@node-drag-stop="(e) => eventManager.handleNodeDragStop(e)"
			@viewport-change="(e) => eventManager.handleViewportChange(e)"
			@pane-click="(e) => eventManager.handleClick(e)"
			:class="{ 'custom-cursor': !!stateManager.cursor.value }"
			:style="{ '--custom-cursor': stateManager.cursor.value }"
		>
			<template v-for="v in nodeTypes" :key="v" #[`node-${v}`]="node">
				<node
					:type="v"
					:stateManager="stateManager"
					:node="node"
					:mask="
						stateManager.tool.value === TOOLS.MOVE ||
						stateManager.tool.value === TOOLS.TEXT
					"
				>
					<component
						:is="components[node.data.component]"
						:class="{ active: stateManager.activeNodeID.value === node.id }"
						:active="stateManager.activeNodeID.value === node.id"
						:node="node"
						:config="node.data"
						:data="node.data.data"
						v-bind="node.data"
						@delete-node="deleteNode(node.id)"
						@copy-node="copyNode(node.id)"
						@update-data="(v) => (node.data.data = v)"
						@bring-to-font="bringToFont(node.id)"
						@send-to-back="sendToBack(node.id)"
					/>
				</node>
			</template>
		</VueFlow>
	</div>
	<header-tools @export="exportFlow" @import="importFlow" />
	<zoom
		:zoom="stateManager.zoom.value"
		:step="0.1"
		is-home
		@add="(e) => flowManager.setZoom(e)"
		@sub="(e) => flowManager.setZoom(e)"
		@home="() => fitView({ maxZoom: 1 })"
	/>
	<image-preview ref="imagePreviewRef" />
</template>

<script setup lang="ts">
	import { VueFlow, useVueFlow } from '@vue-flow/core'
	import { computed, ref, markRaw, onMounted, nextTick, provide, onBeforeMount } from 'vue'
	import { useLayout } from '@/utils/treeDiagram'
	import { NODE_TYPE, NODE_COMPONENT } from './tools/index.d'
	// 组件
	import headerTools from './components/header-tools.vue'
	import zoom from '../components/zoom.vue'
	import imagePreview from '../components/image-preview.vue'
	// 节点
	import node from './components/node.vue'
	import resultImage from './components/nodes/result-image.vue'
	import card from './components/nodes/cards/index.vue'
	import text from './components/nodes/text.vue'
	const components = {
		[NODE_COMPONENT.RESULT_IMAGE]: resultImage,
		[NODE_COMPONENT.CARD]: card,
		[NODE_COMPONENT.TEXT]: text
	}

	// 管理器
	import { StateManager } from './manager/StateManager'
	import { EventManager } from './manager/EventManager'
	import { FlowManager } from './manager/FlowManager'
	import { NodeManager } from './manager/NodeManager'
	import { ToolManager, TOOLS } from './manager/ToolManager'

	const props = defineProps({
		config: {
			type: Object,
			default: () => ({})
		}
	})

	const vueFlow = ref<any>()
	const nodeTypes = ref([NODE_TYPE.INPUT, NODE_TYPE.SECONDARY, NODE_TYPE.OUTPUT, NODE_TYPE.ALONE])

	// 状态管理器
	const stateManager = new StateManager({ vueFlow })
	provide('stateManager', stateManager)

	// 事件管理器
	const eventManager = new EventManager({ stateManager, vueFlow })
	stateManager.setManager({ eventManager })
	provide('eventManager', eventManager)

	// 流程管理器
	const flowManager = new FlowManager({ stateManager, vueFlow })
	stateManager.setManager({ flowManager })
	provide('flowManager', flowManager)

	// 节点管理器
	const nodeManager = new NodeManager({ stateManager, vueFlow })
	stateManager.setManager({ nodeManager })
	provide('nodeManager', nodeManager)

	// 工具管理器
	const toolManager = new ToolManager({ stateManager, vueFlow })
	stateManager.setManager({ toolManager })
	provide('toolManager', toolManager)

	const nodes = computed(() => stateManager.nodes_.value)
	const edges = computed(() => stateManager.edges.value)
	const nodesDraggable = computed(() => stateManager.nodesDraggable.value)
	const panOnDrag = computed(() => stateManager.panOnDrag.value)

	const { fitView } = useVueFlow()
	const { layout } = useLayout()
	const index = ref(0)
	async function layoutGraph(direction) {
		if (index.value > 0) return
		index.value++
		setTimeout(() => {
			stateManager.nodes.value = layout(
				stateManager.nodes.value,
				stateManager.edges.value,
				direction,
				{
					nodesep: nodeManager.nodesep,
					ranksep: nodeManager.ranksep
				}
			)
			nextTick(() => {
				fitView({ maxZoom: 1 })
			})
		}, 0)
	}

	/** 删除节点 */
	const deleteNode = (id) => {
		nodeManager.deleteNode(id)
	}
	/** 复制节点 */
	const copyNode = (id) => {
		nodeManager.copyNodeById(id)
	}
	/** 节点zIndex设置最大 */
	const bringToFont = (id) => {
		stateManager.bringToFont(id)
	}
	/** 节点zIndex设置最小 */
	const sendToBack = (id) => {
		stateManager.sendToBack(id)
	}
	// 导出流程
	const exportFlow = () => {
		// flowManager.exportFlow()
		const str = JSON.stringify(stateManager.nodes.value)
		const json = JSON.parse(str)
		localStorage.setItem('flow_json', str)
	}
	// 导入流程
	const importFlow = async () => {
		try {
			stateManager.nodes.value = []
			await nextTick()
			const json = JSON.parse(localStorage.getItem('flow_json') || '[]')
			stateManager.nodes.value = json
			setTimeout(() => {
				nextTick(() => {
					fitView({ maxZoom: 1 })
				})
			}, 0)
		} catch (error) {
			console.log(error)
		}
	}

	const imagePreviewRef = ref<any>()
	/** 打开图片预览 */
	const openImagePreview = (url: string) => {
		imagePreviewRef.value.open(url)
	}
	provide('openImagePreview', openImagePreview)

	onMounted(() => {
		// window['vueFlow'] = vueFlow
		// window['nodes'] = nodes
		nodeManager.createResultNode({
			data: {
				disableDelete: true,
				isHeader: false,
				data: {
					url: props.config.url
				}
			}
		})
	})
	onBeforeMount(() => {
		eventManager.removeEvents() // 移除事件
	})
</script>
<style lang="less">
	@import '@vue-flow/core/dist/style.css';
	@import '@vue-flow/core/dist/theme-default.css';
</style>
<style lang="less" scoped>
	.flow-canvas {
		width: 100%;
		height: 100%;
		> .vue-flow {
			width: 100%;
			height: 100%;
			&.custom-cursor {
				cursor: var(--custom-cursor, pointer);
			}
		}
	}
</style>
