package cn.com.liandisys.ycjt.ggcx.airline.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.liandisys.ycjt.ggcx.airline.vo.AirDynamic;
import cn.com.liandisys.ycjt.ggcx.common.util.MessageUtil;


@Controller
@RequestMapping(value = "/table")
public class DynamicflightController {

	 private Logger logger = LoggerFactory.getLogger(DynamicflightController.class);
	final static String url = "http://221.231.1.162/GettingYNZFlightInfo/GettingFlightInfo";
	
	
	//查询到港航班
    @RequestMapping(value = "initDynamicflightData")
    @ResponseBody
	public  Map<String, Object> DynamicflightList(Model model){
    	List<Map<String, Object>> airoutList = new ArrayList<Map<String, Object>>();
    	List<Map<String, Object>> airinList = new ArrayList<Map<String, Object>>();
		List<AirDynamic> airout = new ArrayList<AirDynamic>();
		List<AirDynamic> airin = new ArrayList<AirDynamic>();
	    Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			airout.addAll(airDynamic(url, "marquee4", "out"));
			airin.addAll(airDynamic(url, "marquee5", "in"));   //抓取进港数据
			for (AirDynamic d : airout) {
				 Map<String, Object> airoutMap = new HashMap<String, Object>();
        		 airoutMap.put("hbh",d.getHbh());
        		 airoutMap.put("sfd",(d.getSfd().toString()).replaceAll("  ", ""));
        		 airoutMap.put("jt",d.getJt());
        		 airoutMap.put("planlg",d.getPlanlg());
        		 airoutMap.put("prelg",d.getPrelg());
        		 airoutMap.put("actuallg",d.getActuallg());
        		 airoutMap.put("status",d.getStatus());
        		 airoutMap.put("color","#75b0dc");
        		 if(d.getStatus().replaceAll("   ", "").equals("起飞"))
        		 { airoutMap.put("color","#00B0F0");  }
        		 if(d.getStatus().replaceAll("   ", "").equals("办票截止"))
        		 { airoutMap.put("color","#F79646");  }
        		 if(d.getStatus().replaceAll("   ", "").equals("登机结束"))
        		 { airoutMap.put("color","red");  }
        		 if(d.getStatus().replaceAll("   ", "").equals("延误"))
        		 { airoutMap.put("color","#F2CA12");  }
        		 if(d.getStatus().replaceAll("   ", "").equals("过站登机"))
        		 { airoutMap.put("color","#00B050");  }
        		 if(d.getStatus().replaceAll("   ", "").equals("正在办票"))
        		 { airoutMap.put("color","#00B050");  }
        		 airoutList.add(airoutMap);
        	 }
			for (AirDynamic d : airin) {
				Map<String, Object> airinMap = new HashMap<String, Object>();
	       		airinMap.put("hbh",d.getHbh());
	       		airinMap.put("sfd",(d.getSfd().toString()).replaceAll(" ", ""));
	       		airinMap.put("jt",d.getJt());
	       		airinMap.put("planlg",d.getPlanlg());
	       		airinMap.put("prelg",d.getPrelg());
	       		airinMap.put("actuallg",d.getActuallg());
	       		airinMap.put("status",d.getStatus());
	       		airinMap.put("color","#000");
	       		if(d.getStatus().replaceAll("   ", "").equals("前方起飞"))
	       		{ airinMap.put("color","#00B0F0");  }
	       		if(d.getStatus().replaceAll("   ", "").equals("到达"))
	       		{ airinMap.put("color","#00B050");  }
	       		if(d.getStatus().replaceAll("   ", "").equals("延误"))
	       		{ airinMap.put("color","#F2CA12");  }
       		    airinList.add(airinMap);
       	 }
			retMap.put("DynamicOut", airoutList);
			retMap.put("DynamicIn", airinList);
			return MessageUtil.buildResponseMap(true, retMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
            return MessageUtil.buildResponseMap(false, MessageUtil.buildSystemErrorMessage(e), retMap);
		}  //抓取出港数据
		
		
	}


public static List<AirDynamic> airDynamic(String url, String htmlId, String type) throws IOException {
	List<AirDynamic> airs = new ArrayList<AirDynamic>();
	Document result = Jsoup.connect(url).get();
	Elements elemct = result.select("#" + htmlId + " ul li");
	for (Element e : elemct) {
		Elements ele = e.select("span");
		AirDynamic air = new AirDynamic();
		air.setType(type);
		for (int i = 0, len = ele.size(); i < len; i++) {
			switch (i) {
			case 0:
				air.setHbh(ele.get(i).text().trim());
				break;
			case 1:
				air.setJt(ele.get(i).text().trim());
				break;
			case 2:
				air.setSfd(ele.get(i).text().trim());
				break;
			case 3:
				air.setPlanlg(ele.get(i).text().trim());
				break;
			case 4:
				air.setPrelg(ele.get(i).text().trim());
				break;
			case 5:
				air.setActuallg(ele.get(i).text().trim());
				break;
			case 6:
				air.setStatus(ele.get(i).text().trim());
				break;
			}
		}
		airs.add(air);
	}

	return airs;
}

}
