package com.persagy.adm.diagram.manage;

import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.diagram.core.ContentParser;
import com.persagy.adm.diagram.core.DiagramView;
import com.persagy.adm.diagram.core.model.ConnectPoint;
import com.persagy.adm.diagram.core.model.Label;
import com.persagy.adm.diagram.core.model.Line;
import com.persagy.adm.diagram.core.model.legend.Legend;
import com.persagy.adm.diagram.core.model.style.BaseStyle;
import com.persagy.adm.diagram.core.model.style.LineStyle;
import com.persagy.adm.diagram.entity.DiagramType;
import com.persagy.adm.diagram.core.DataStrategy;
import com.persagy.adm.diagram.core.DiagramDataLoader;
import com.persagy.adm.diagram.core.model.Diagram;
import com.persagy.adm.diagram.core.model.base.XY;
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 ContentParser parser;

	@Autowired
	public DemoDiagramManager(@Qualifier(DataStrategy.implQualifier) 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) {
			buildDiagram(diagram, true);
		}

		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;
	}

	private void loadTemplate(Diagram diagram){
		if (diagram.getTemplateId() != null)
			diagram.setTemplate(dataStrategy.getTemplate(diagram.getTemplateId()));
	}

	private void buildDiagram(Diagram diagram, boolean layout){
		loadTemplate(diagram);
		diagram.init();

		if(layout)
			diagram.layout(new XY(0, 0));
	}

	/**
	 * 创建新的系统图
	 */
	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);
		buildDiagram(diagram, true);

		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);

		dataStrategy.saveDiagram(diagram);
		buildDiagram(diagram, true);

		return diagram;
	}

	@Autowired
	private ObjectMapper objectMapper;
	/**
	 * 加载数据
	 */
	public Diagram loadData(String diagramId, boolean autoLoad){
		Diagram diagram = dataStrategy.getDiagram(diagramId);
		buildDiagram(diagram, false);

		loadData(diagram, autoLoad);

		diagram.layout(new XY(0, 0));

//		//test 输出数据结构
//		try {
//			Diagram d = dataStrategy.getDiagram(diagramId);
//			buildDiagram(d, false);
//			loadData(d, autoLoad);
//			d.layout(new XY(0, 0), true);
//			if(d.getLines().size() == 0) {
//				Line line = new Line();
//				line.setFrom(new ConnectPoint());
//				line.setTo(new ConnectPoint());
//				line.setLineStyle(new LineStyle());
//				Label label = new Label();
//				label.setStyle(new BaseStyle());
//				line.setLabel(label);
//				d.getLines().add(line);
//			}
//			DiagramView dv = new DiagramView(d);
//
//			System.out.println("-----------------------------------------------------");
//			System.out.println(objectMapper.writeValueAsString(dv));
//		}catch (Exception e){
//			e.printStackTrace();
//		}

		return diagram;
	}

	private void loadData(Diagram diagram, boolean autoLoad){
		DiagramDataLoader loader = new DiagramDataLoader(diagram, dataStrategy);
		loader.load();

		if(autoLoad)
			loader.autoLoad();
	}

	/**
	 * 保存系统图
	 */
	public Diagram saveDiagram(Map<String, Object> diagramMap) {
		Diagram diagram = parser.parseContent(parser.toJson(diagramMap), Diagram.class);
		return dataStrategy.saveDiagram(diagram);
	}

}
