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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.com.liandisys.ycjt.ggcx.common.util.MessageUtil;
import cn.com.liandisys.ycjt.ggcx.gjcsswz.model.SoapEntity;

@Controller
public class GjsswzController {

    private Logger logger = LoggerFactory.getLogger(GjsswzController.class);

    //    final static String url =
    //            "http://120.195.27.61:28090/AndroidGreen/BusService.asmx/GetBusRealInfoById?username=admin&passWord=123456789abc123456789abc&routeId=9&dir=0&tag=";

    //查询到港航班
    @RequestMapping(value = "getSswzList.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> DynamicflightList(HttpServletRequest request, HttpServletResponse response) {

        String u1 =
                "http://120.195.27.61:28090/AndroidGreen/BusService.asmx/GetBusRealInfoById?username=admin&passWord=123456789abc123456789abc";
        String u2 = "&routeId=";
        String roadId = request.getParameter("roadId");
        String u3 = roadId;
        String u4 = "&dir=0&tag=";
        String url = u1 + u2 + u3 + u4;

        List<Map<String, Object>> busList = new ArrayList<Map<String, Object>>();
        Map<String, Object> retMap = new HashMap<String, Object>();
        try {
            //时间格式转换
            List<SoapEntity> SoapEntityList = new ArrayList<SoapEntity>();
            SoapEntityList = busLine(url);

            for (SoapEntity d : SoapEntityList) {
                Map<String, Object> businfo = new HashMap<String, Object>();

                //坐标纠正的方法
                //                double[] ab = GetBaiduGps.wgs2bd(Double.valueOf(d.getLatitude()), Double.valueOf(d.getLongitude()));

                /*businfo.put("latitude", String.valueOf(ab[0]));
                businfo.put("longitude", String.valueOf(ab[1]));*/
                businfo.put("latitude", d.getLatitude());
                businfo.put("longitude", d.getLongitude());
                busList.add(businfo);
            }
            retMap.put("busList", busList);
            return MessageUtil.buildResponseMap(true, retMap);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            return MessageUtil.buildResponseMap(false, MessageUtil.buildSystemErrorMessage(e), retMap);
        }

    }

    public static List<SoapEntity> busLine(String url) throws IOException {

        List<SoapEntity> busline = new ArrayList<SoapEntity>();
        Document result = Jsoup.connect(url).get();

        String value = result.toString().replace("\n", "").replace("\n", "").replace("\n", "")
                .replace("<string xmlns=\"http://tempuri.org/\">", "").replace("</string>", "")
                .replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
        value = value.substring(2, value.length());

        busline = JSON.parseArray(value, SoapEntity.class);

        return busline;
    }

}
