package kr.gmtc.gicoms.pg.tss;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import kr.gmtc.tss.util.XmlParseUtil;
import kr.gmtc.tss.util.vo.NodeValue;

public class XmlTest {

	public static void main(String[] args) {
		

		Map<String,Object> rootXml=XmlParseUtil.parseXml("./data/marine_area.xml");
		
		double d = 26.99999994172804*100000;
		
		
		 d=Math.round(d);
		System.out.println((int)d);
		
//		NodeValue rootNode=XmlParseUtil.getNodeValue(rootXml, "/kml/Document/Folder");
//		rootNode.getChildMap();
		List<NodeValue> list=XmlParseUtil.getNodeValueList(rootXml, "/kml/Document/Folder/Placemark");
		
		Map<String,String> marinAreaMap = new HashMap<String, String>(); 
		
		Set<Integer> lonList = new HashSet<Integer>();
		Set<Integer> latList = new HashSet<Integer>();
		
		
		
		for (int i = 0; i < list.size(); i++) {
			NodeValue placemark=list.get(i);
			List<NodeValue> simpleDataList = XmlParseUtil.getNodeValueList(placemark.getChildMap(), "/ExtendedData/SchemaData/SimpleData");
			String no1="";
			String no2="";
			String no3="";
			
			
			for (int j = 0; j < simpleDataList.size(); j++) {
				NodeValue simpleData=simpleDataList.get(j);
				String name=simpleData.getAttrMapValue("name");
				if("대해구도번호".equals(name)) {
					no1=simpleData.getText();
				}else if("소해구도 부번".equals(name)) {
					no2=simpleData.getText();
				}else if("소해구도번호".equals(name)) {
					no3=simpleData.getText();
				}
			}
			String boundary=""+XmlParseUtil.getNodeData(placemark.getChildMap(), "/Polygon/outerBoundaryIs/LinearRing/coordinates", 1);
			
			String key=parseBoundary(boundary, lonList, latList);
			
			if(!key.isEmpty()) {
				//key : minLon_maxLat
				//value : 해구도번호
				marinAreaMap.put(key, no1+"_"+no2+"_"+no3);
			}else {
				System.err.println("key make error!!!!!!");
			}
			
			
//			XmlParseUtil.getNodeData(nodeMap, nodePath, category)
//			System.out.println(no1+"_"+no2+"_"+no3);
			boundary=boundary.replaceAll(",", "&").replaceAll(" ", ",").replaceAll("&", " ");
//			System.out.println(boundary);
//			System.out.println("select '"+no1+"_"+no2+"_"+no3+"' as no, st_geomfromtext('POLYGON(("+boundary+"))',4326) as geom union all ");
//			Arrays.search
//			System.out.println("insert into test_marine_no values ( '"+no1+"_"+no2+"_"+no3+"' , st_geomfromtext('POLYGON(("+boundary+"))',4326) );");
			System.out.println("insert into tm_zone values ( '"+no1+"_"+no2+"' , st_geomfromtext('POLYGON(("+boundary+"))',4326) );");
		}

		Integer[] lonArr =  lonList.toArray(new Integer[lonList.size()]);
		Integer[] latArr =  latList.toArray(new Integer[latList.size()]);
		Arrays.sort(lonArr);
		Arrays.sort(latArr);
		
		int a=Arrays.binarySearch(latArr, (int)(34.3*100000));
		int b=Arrays.binarySearch(lonArr, (int)(125.4*100000));
		
		System.out.println(a);
		System.out.println(b);
		

		a=Arrays.binarySearch(latArr, (int)(34*100000));
		b=Arrays.binarySearch(lonArr, (int)(125*100000));
		
		System.out.println(a);
		System.out.println(b);
		
		
		System.out.println("asdf");
		
//		ObjectMapper xmlMapper = new XmlMapper(); 
//		
//		Map<String,Object> map = new HashMap<String, Object>();
//		
//		File file = new File("./data/marine_area.xml");
//		if(!file.exists()) {
//			System.out.println("no file!");
//			return;
//		}
//		try {
//			map = xmlMapper.readValue(file, Map.class);
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//		}
//		System.out.println("asdf");
//		((Map<String,Object>)map.get("Document")).get("Folder");
	}
	
	public static String parseBoundary(String boundaryStr,Set<Integer> lonList,Set<Integer> latList) {
		String key="";
		String [] arr = boundaryStr.split(" ");
		double [] lonArr = new double[arr.length];
		double [] latArr = new double[arr.length];
		
		for (int i = 0; i < arr.length; i++) {
			String[] temp = arr[i].split(",");
			lonArr[i]=Double.parseDouble(temp[0]);
			latArr[i]=Double.parseDouble(temp[1]);
		}
		Arrays.sort(lonArr);
		Arrays.sort(latArr);

		double lon=lonArr[0]*100000;
		lon = Math.round(lon);

		double lat=latArr[0]*100000;
		lat = Math.round(lat);
		
		int lonInt = (int)lon;
		int latInt = (int)lat;
		
		lonList.add(lonInt);
		latList.add(latInt);
		
		
		key=lonInt+"_"+latInt;
		
		return key;
	}
	
	
	
}
