package com.persagy.adm.diagram.manage;

import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.diagram.core.DataStrategy;
import com.persagy.adm.diagram.core.model.Diagram;
import com.persagy.adm.diagram.entity.DiagramType;
import com.persagy.adm.diagram.frame.BdtpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统图编辑/展现(演示版)的相关操作
 * @author zhaoyk
 */
@Service
public class DemoDiagramManager {

	private final DataStrategy dataStrategy;

	@Autowired
	private DiagramManager manager;

	@Autowired
	public DemoDiagramManager(@Qualifier(DataStrategy.IMPL_QUALIFIER) DataStrategy dataStrategy) {
		this.dataStrategy = dataStrategy;
	}

	/**
	 * 查询系统图，树形结构
	 */
	public List<Folder> getDiagrams(String projectId, String systemId, String groupCode) {
		List<DiagramType> types = dataStrategy.getDiagramTypes();
		List<Diagram> diagrams = dataStrategy.getDiagrams(projectId, systemId, groupCode);
		for(Diagram diagram : diagrams) {
			diagram.setTemplate(null); //减少传输数据量，并给前端标识
		}

		BdtpRequest req = BdtpRequest.getCurrent();
//		List<ObjectNode> maj = bdtpDataService.getMajorList(req);
//		List<ObjectNode> sys = bdtpDataService.getSystemList(req);
		//TODO debug
		List<ObjectNode> maj = new ArrayList<>();
		List<ObjectNode> sys = new ArrayList<>();

		List<Folder> folders = new ArrayList<>();
		for (DiagramType type : types) {
			Folder fm = Folder.getFolder(type.getMajor(), Folder.TYPE_MAJ, folders, maj);
			Folder fs = Folder.getFolder(type.getDSystem(), Folder.TYPE_SYS, (List)fm.getChildren(), sys);

			Folder ft = new Folder();
			fs.getChildren().add(ft);
			ft.setName(type.getName());
			ft.setId(type.getCode());
			ft.setType(Folder.TYPE_TYPE);

			for(Diagram diagram : diagrams){
				if(diagram.getType().equals(type.getCode())) {
					ft.getChildren().add(diagram);
				}
			}
		}

		return folders;
	}

	/**
	 * 获取系统图内容
	 */
	public Diagram getDiagram(String diagramId){
		Diagram diagram = dataStrategy.getDiagram(diagramId);
		manager.buildDiagram(diagram);
		return diagram;
	}

	/**
	 * 创建新的系统图
	 */
	public Diagram createDiagram(String name, String type, String projectId, String systemId, String groupCode){
		Diagram diagram = new Diagram();
		diagram.setId(IdUtil.simpleUUID());

		diagram.setName(name);
		diagram.setType(type);
		diagram.setSystem(type.substring(0, 4));

		diagram.setProjectId(projectId);
		diagram.setSystemId(systemId);
		diagram.setGroupCode(groupCode);

		diagram = dataStrategy.saveDiagram(diagram);
		manager.buildDiagram(diagram);

		return diagram;
	}

	/**
	 * 删除系统图
	 */
	public boolean deleteDiagram(String diagramId) {
		return dataStrategy.deleteDiagram(diagramId);
	}

	/**
	 * 设置系统图模板
	 */
	public Diagram setTemplate(String diagramId, String templateId) {
		Diagram diagram = dataStrategy.getDiagram(diagramId);
		diagram.setTemplateId(templateId);

		//重设模板时，重置系统图
		manager.resetDiagram(diagram);

		dataStrategy.saveDiagram(diagram);
		manager.buildDiagram(diagram);

		return diagram;
	}

	/**
	 * 加载数据
	 */
	public Diagram loadData(String diagramId, boolean autoLoad){
		Diagram diagram = dataStrategy.getDiagram(diagramId);
		manager.loadData(diagram);
		return diagram;
	}

	/**
	 * 保存系统图
	 */
	public Diagram saveDiagram(Map<String, Object> diagramMap) {
		Diagram diagram = manager.fromMapData(diagramMap);
		dataStrategy.saveDiagram(diagram);
		manager.buildDiagram(diagram);
		return diagram;
	}

	/**
	 * 重置系统图
	 */
	public Diagram resetDiagram(String diagramId) {
		Diagram diagram = dataStrategy.getDiagram(diagramId);
		manager.resetDiagram(diagram);

		manager.buildDiagram(diagram);
		return diagram;
	}

}
