package com.dgt.graphdemo.controller;

import com.alibaba.fastjson.JSON;
import com.dgt.graphdemo.entity.fqz.*;
import com.dgt.graphdemo.utils.Constants;
import com.dgt.graphdemo.utils.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


@Controller
@RequestMapping("/graph")
public class JsonController {

	//校验系列案名称格式
	private static final String PATTERN = ".{11,100}((([\\d]{4}(((0[13578]|1[02])((0[1-9])|([12][0-9])|(3[01])))|(((0[469])|11)((0[1-9])|([12][0-9])|30))|(02((0[1-9])|(1[0-9])|(2[0-8])))))|((((([02468][048])|([13579][26]))00)|([0-9]{2}(([02468][048])|([13579][26]))))(((0[13578]|1[02])((0[1-9])|([12][0-9])|(3[01])))|(((0[469])|11)((0[1-9])|([12][0-9])|30))|(02((0[1-9])|(1[0-9])|(2[0-9])))))).*案)";


//	@RequestMapping("/index.do")
//	public String indexTograph(){
//		return "/graph/indexNew/index.v3";
//	}
	@RequestMapping("/index.do")
	public void indexTograph(HttpServletRequest request, HttpServletResponse response)throws Exception{
		request.getRequestDispatcher("/WEB-INF/jsp/graph/indexNew/newIndex.v3.jsp").forward(request, response);

	}

	@RequestMapping("/detail.do")
	public void indexTograph1(HttpServletRequest request, HttpServletResponse response)throws Exception{
		request.getRequestDispatcher("/WEB-INF/jsp/graph/detail/detail.jsp").forward(request, response);

	}
	// 模糊查询实体
	@RequestMapping("searchEntity")
	public void searchEntityByCase(HttpServletRequest request,
								   HttpServletResponse response, Nodes node, String searchType,
								   Pager<Node> pager) throws Exception {
		if (StringUtils.isBlank(node.getEntityName())) {
			//throw new WeaverException("搜索内容不能为空！");
		}
        String key = node.getEntityName() +".json";

		if ( Constants.JSONFILEMAP.containsKey(key)) {
			String result = Constants.JSONFILEMAP.get(key);
			response.setContentType("text/json; charset=utf-8");
			response.setHeader("Cache-Control", "no-cache"); // 取消浏览器缓存
			PrintWriter out = response.getWriter();
			out.print(result);
//		System.out.println("result====" + result);
			out.flush();
			out.close();
		}
	}



	//精确查询实体
	@RequestMapping("/expandGraph")
	public void expandGraph(HttpServletRequest request, HttpServletResponse response, String result, Nodes node, String graphweaverIds, String weaverData, String tagList, String entityId, String roleProperty, String weaverNode, String weaverEdge) throws Exception {
        GraphNew graphOld=com.alibaba.fastjson.JSON.parseObject(result, GraphNew.class);

	   if (!StringUtils.isEmpty(result)){
		   //取出里面存储的每个节点上一次存储的相关信息
		   Map<String, Map<String, Map<String,String>>> nodeLinkRelOld = graphOld.getNodeLinkRelmap();
		   Map<String, Node> weaverNodeOld = graphOld.getWeaverNodeMap();



		   String key=  node.getEntityId()+".json";
		   if (Constants.JSONFILEMAP.containsKey(key)){
			   String jsontest = Constants.JSONFILEMAP.get(key);
			   Graph graphNew = JSON.parseObject(jsontest, Graph.class);
			   Map<String, Map<String, Map<String, String>>> nodeLinkRelNew = graphNew.getNodeLinkRelmap();
			   for(String rel: nodeLinkRelOld.keySet()){
				   Map<String, Map<String, String>> mapold = nodeLinkRelOld.get(rel);
				   if (nodeLinkRelNew.containsKey(rel)){
				   	   //这里得到了关系下面某个target -> source
					   Map<String, Map<String, String>> mapnew = nodeLinkRelNew.get(rel);
					  // mapnew.putAll(mapold);
					   for (String key1:mapold.keySet()){
						   Map<String, String> map2old = mapold.get(key1);
						   if (!mapnew.containsKey(key1)){
                                mapnew.put(key1,map2old);
						   }else{
							   Map<String, String> map2new = mapnew.get(key1);
							   for (String key3:map2old.keySet()){
								   map2new.put(key3,key3);
							   }
						   }
					   }
				   }else{
					   nodeLinkRelNew.put(rel,mapold);
				   }
			   }
			   Map<String, Node> weaverNodeNew = graphNew.getWeaverNodeMap();
			   for (String weaverOld:weaverNodeOld.keySet()){
				   if (!weaverNodeNew.containsKey(weaverOld)){
					   weaverNodeNew.put(weaverOld,weaverNodeOld.get(weaverOld));
				   }
			   }
			   this.ajax(response, graphNew);
			   return;
		   }
	   }


		Graph g =new Graph();
		String key=  node.getEntityId()+".json";
		if (Constants.JSONFILEMAP.containsKey(key)){
			String jsontest = Constants.JSONFILEMAP.get(key);
			Graph graphNew = JSON.parseObject(jsontest, Graph.class);
			this.ajax(response, graphNew);
			return;
		}


		this.ajax(response, g);

	}


	private String ajax(HttpServletResponse response, Object obj) throws Exception {
		JsonUtils.responseJson(response, obj);
		return null;
	}



}
