import { BaseEdgeModel, BaseNodeModel } from '@logicflow/core';

/**
 * 节点样式处理方法
 * @param _this
 * @param {*} style
 * @returns
 */
export const nodeStyleHandle = (
	_this: BaseNodeModel,
	style: {
		[x: string]: any;
		r?: number;
		fill?: string;
		stroke?: string;
		strokeWidth?: number;
	}
): {
	[x: string]: any;
	r?: number;
	fill?: string;
	stroke?: string;
	strokeWidth?: number;
} => {
	if (_this.properties.state === 'active') {
		style.stroke = '#00ff00';
	} else if (_this.properties.state === 'history') {
		style.stroke = '#ff0000';
	}
	return style;
};
/**
 * 边样式处理方法
 * @param _this
 * @param {*} style
 * @returns
 */
export const edgeStyleHandle = (
	_this: BaseEdgeModel,
	style: {
		[x: string]: any;
		r?: number;
		fill?: string;
		stroke?: string;
		strokeWidth?: number;
	}
): {
	[x: string]: any;
	r?: number;
	fill?: string;
	stroke?: string;
	strokeWidth?: number;
} => {
	if (_this.properties.state === 'active') {
		style.stroke = '#00ff00';
	} else if (_this.properties.state === 'history') {
		style.stroke = '#ff0000';
	}
	return style;
};
/**
 * 将获取到的数据转换成logic flow需要的数据
 * @returns
 * @param data
 */
export const resDataToLogicFlow = (data: any): any => {
	return JSON.parse(data.externalData);
};

/**
 * 将LogicFlow的数据转成提交的格式
 * @returns
 * @param data
 */
export const logicFlowJsonToSubmit = (data: any): object => {
	let returnData = {};
	// data的数据由流程定义文件信息+logicFlow数据构成
	// 先构建成流程对象
	const processObj = <any>{
		name: data.name, // 流程定义名称
		code: data.code, // 流程定义code
		flowType: data.flowType, // 模板类型
		isMainHandlerShow: data.isMainHandlerShow, // 是否显示主办人
		description: data.description, // 流程定义描述
		id: data.id, // 流程id
		steps: [], // 所有步骤
	};

	// /**
	//  * 获取开始节点
	//  * @returns
	//  */
	const getStartNode = () => {
		return data.nodes.filter((node: any) => {
			// 是开始节点，且非子流程中的开始节点
			return node.type === 'hotline:start';
		});
	};
	// 获取结束节点
	const getEndNode = () => {
		return data.nodes.filter((node: any) => {
			return node.type === 'hotline:end';
		});
	};
	// /**
	//  * 构建节点属性
	//  * @param {}} node
	//  * @returns
	//  */
	const buildNode = (node: any) => {
		return {
			name: node.text instanceof String || node.text === undefined ? node.text : node.text.value,
			...node.properties,
			handlerTypeItems: formatArrayHandlerTypeItems(node),
			code: node.id,
			nextSteps: node.nextSteps,
		};
	};
	/**
	 * 处理数组类型
	 * @param {} node
	 */
	const formatArrayHandlerTypeItems = (node: any) => {
		const nodeName = node.type.replace('hotline:', '');
		let data: string[] = [];
		if (['start', 'task', 'end'].includes(nodeName)) {
			if (node.properties.handlerTypeItems && Object.prototype.toString.call(node.properties.handlerTypeItems) === '[object String]') {
				data = JSON.parse(node.properties.handlerTypeItems);
			}
		}
		return data;
	};

	const startNode = getStartNode();
	const endNode = getEndNode();
	if (!startNode[0]) {
		// 开始节点不存在
		return { error: '开始节点不存在' };
	}
	if (startNode.length > 1) {
		// 开始节点存在多个
		return { error: '开始节点存在多个' };
	}
	if (!endNode[0]) {
		// 结束节点不存在
		return { error: '结束节点不存在' };
	}
	if (endNode.length > 1) {
		// 结束节点存在多个
		return { error: '结束节点存在多个' };
	}
	for (let node of data.nodes) {
		node.nextSteps = [];
		const edges = data.edges.filter((v: any) => v.sourceNodeId === node.id); // 找到当前节点的边
		for (let edge of edges) {
			if (edge.sourceNodeId === node.id) {
				node.nextSteps.push({
					code: edge.targetNodeId,
				});
			}
		}
		processObj.steps.push(buildNode(node));
	}
	for (let i of processObj.steps) {
		//普通节点字段非空校验
		if (['start', 'task', 'end'].includes(i.code)) {
			if (!i.stepType && String(i.stepType) !== '0') {
				return { error: `请选择${i.name}的节点类型` };
			}
			if (!i.businessType && String(i.businessType) !== '0') {
				return { error: `请选择${i.name}的业务类型` };
			}
			if (!i.executeMode && String(i.executeMode) !== '0') {
				return { error: `请选择${i.name}的执行者模式` };
			}
			if (!i.handlerType && String(i.handlerType) !== '0') {
				return { error: `请选择${i.name}的办理者类型` };
			}
		}
	}
	returnData = {
		...processObj,
	};

	return returnData;
};
