package com.persagy.adm.diagram.manage;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.persagy.adm.diagram.core.ContentParser;
import com.persagy.adm.diagram.core.DataStrategy;
import com.persagy.adm.diagram.core.model.legend.Legend;
import com.persagy.adm.diagram.core.util.GsonUtil;
import com.persagy.adm.diagram.service.BdtpDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

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

/**
 * 图例编辑的相关操作
 * @author zhaoyk
 */
@Service
public class LegendManager {

	private final DataStrategy dataStrategy;

	@Autowired
	private ContentParser parser;

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

	@Autowired
	private BdtpDataService bdtpDataService;

	@Autowired
	private ObjectMapper objectMapper;

	/**
	 * 查询系统下的图例列表
	 */
	public List<Legend> getLegends(String sys){
//		//test 输出数据结构
//		try{
//			System.out.println("-----------------------------------------------------");
//			System.out.println(objectMapper.writeValueAsString(dataStrategy.getLegends(sys)));
//		}catch (Exception e){
//			e.printStackTrace();
//		}

		return dataStrategy.getLegends(sys);
	}

	/**
	 * 保存图例
	 */
	public Legend saveLegend(Map<String, Object> legendMap, String sys) {
		return dataStrategy.saveLegend(parser.parseContent(parser.toJson(legendMap), Legend.class), sys);
	}

	/**
	 * 删除图例
	 */
	public boolean deleteLegend(String legendId, String systemCode){
		return dataStrategy.deleteLegend(legendId, systemCode);
	}

//	@Autowired
//	private GsonUtil gsonUtil;
//	public boolean fixRel(){
//		String DIR_LEGEND = "store/legend/";
//		File dir = new File(DIR_LEGEND);
//		for(File file : dir.listFiles()) {
//			String fn = file.getName();
//			String sys = fn.substring(0, fn.lastIndexOf('.'));
//
//			List<Map> list = gsonUtil.gson().fromJson(FileUtil.readUtf8String(file), List.class);
//			for(Map map : list) {
//				List as = (List) map.get("anchors");
//				if(as != null) {
//					for(Object o : as) {
//						Map am = (Map) o;
//						List rels = (List)am.get("acceptRelations");
//						if(rels != null) {
//							for(int i=0;i<rels.size();i++) {
//								Object item = rels.get(i);
//								if(item instanceof String) {
//									String[] arr = ((String) item).split("/");
//									Map rm = new HashMap();
//									rm.put("graphCode", arr[0]);
//									rm.put("relCode", arr[1]);
//									rels.set(i, rm);
//								} else if(item instanceof Map) {
//									rels.set(i, ((Map<?, ?>) item).get("graphCode") + "/" + ((Map<?, ?>) item).get("relCode"));
//								}
//							}
//						}
//					}
//				}
//			}
//
//			FileUtil.writeUtf8String(gsonUtil.gson().toJson(list), file);
//		}
//
//		return true;
//	}

	@Autowired
	private GsonUtil gsonUtil;
	public boolean fixDiagramType(){
		String dirLegend = "store/legend/";
		File dir = new File(dirLegend);
		for(File file : dir.listFiles()) {
			String fn = file.getName();
			String sys = fn.substring(0, fn.lastIndexOf('.'));

			List<Map> list = gsonUtil.gson().fromJson(FileUtil.readUtf8String(file), List.class);
			for(Map map : list) {
				String dt = (String) map.get("diagramType");
				if (StrUtil.isNotBlank(dt)) {
					ArrayList<String> dts = new ArrayList<>();
					dts.add(dt);
					map.put("diagramTypes", dts);
				}
			}
			FileUtil.writeUtf8String(gsonUtil.gson().toJson(list), file);
		}

		return true;
	}

}
