package com.rehome.mqttclienttemperature.controller;

import com.rehome.mqttclienttemperature.entity.WebServiceCity;
import com.rehome.mqttclienttemperature.entity.WebServiceProvince;
import com.rehome.mqttclienttemperature.service.TemperatureService;
import com.rehome.mqttclienttemperature.service.WebServiceWeatherService;
import com.rehome.mqttclienttemperature.soap.*;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.axiom.om.OMElement;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


@Api(tags = "天气预报接口")
@Controller
public class WeatherController {

	@Resource
	private WebServiceWeatherService webServiceWeatherService;
	
	@ApiOperation("获取城市天气预报")
	@RequestMapping(value="/getWeatherbyCityName/{cityname}",method = RequestMethod.GET)
	@ResponseBody
	public String[] getWeatherbyCityName(@PathVariable String cityname) throws Exception{
		WeatherWebServiceStub sub = new WeatherWebServiceStub();
		GetWeatherbyCityName weather = new GetWeatherbyCityName();
		weather.setTheCityName(cityname);
		GetWeatherbyCityNameResponse wps = sub.getWeatherbyCityName(weather);
		String[] wuhanweather = wps.localGetWeatherbyCityNameResult.localString;
		for (String wuhan : wuhanweather ) {
			System.out.println(wuhan);
		}
		return wuhanweather;
	}
	
	@ApiOperation("获取支持的城市列表")
	@RequestMapping(value="/getCitysbyProvince/{province}",method = RequestMethod.GET)
	@ResponseBody
	public String[] getCitysbyProvince(@PathVariable String province) throws Exception{
		WeatherWebServiceStub sub = new WeatherWebServiceStub();
		GetSupportCity city = new GetSupportCity();
		city.setByProvinceName(province);
		GetSupportCityResponse rsp = sub.getSupportCity(city);
		String[] data = rsp.localGetSupportCityResult.getString();
		for (String d : data) {
			System.out.println(d);
		}
		return data;
	}	
	
	@ApiOperation("获取所有支持的洲、省份和城市列表")
	@RequestMapping(value="/getSupportList",method = RequestMethod.GET)
	@ResponseBody
	public List<String> getSupportList() throws Exception{
		List<String> list = new ArrayList<>();
		WeatherWebServiceStub sub = new WeatherWebServiceStub();
		GetSupportDataSet set = new GetSupportDataSet();
		GetSupportDataSetResponse setpsp = sub.getSupportDataSet(set);
		GetSupportDataSetResult_type0 a = setpsp.getGetSupportDataSetResult();
		OMElement root = a.localExtraElement;
		Iterator iterator_layer1 = root.getChildElements();
		while (iterator_layer1.hasNext())
		{
			OMElement result = (OMElement) iterator_layer1.next();
			Iterator innerItr_layer2 = result.getChildElements();
			while (innerItr_layer2.hasNext())
			{
				OMElement result_2 = (OMElement) innerItr_layer2.next();
 
				Iterator innerItr_layer3 = result.getChildElements();
				{
 
					while (innerItr_layer3.hasNext())
					{
 
						OMElement result_l3 = (OMElement) innerItr_layer3.next();
						String s2 = result_l3.getLocalName();
						Iterator ss = result_l3.getChildElements();
						while (ss.hasNext()){
							OMElement result_l4 = (OMElement)ss.next();
							System.out.println(result_l4.getText());
							list.add(result_l4.getText());
						}
						
					}
				}
 
			}
		}
		return list;
	}

	public String[] getWeatherByCityCode(String cityCode) throws Exception{
		WeatherWebServiceStub sub = new WeatherWebServiceStub();
		GetWeatherbyCityName weather = new GetWeatherbyCityName();
		weather.setTheCityName(cityCode);
		GetWeatherbyCityNameResponse wps = sub.getWeatherbyCityName(weather);
		String[] wuhanweather = wps.localGetWeatherbyCityNameResult.localString;
		for (String wuhan : wuhanweather ) {
			System.out.println(wuhan);
		}
		return wuhanweather;
	}

	public List<String> getCitListByProvince(String province) throws Exception{
		List<String> list = new ArrayList<>();
		WeatherWebServiceStub sub = new WeatherWebServiceStub();
		GetSupportCity city = new GetSupportCity();
		city.setByProvinceName(province);
		GetSupportCityResponse rsp = sub.getSupportCity(city);
		String[] data = rsp.localGetSupportCityResult.getString();
		for (String d : data) {
			System.out.println(d);
		}
		list.addAll(Arrays.asList(data));
		return list;
	}

	@ApiOperation("获得本天气预报Web Services支持的洲、国内外省份和城市信息")
	@RequestMapping(value="/getSupportProvince",method = RequestMethod.GET)
	@ResponseBody
	public List<String> getSupportProvince(){
		List<String> list = new ArrayList<>();
		try {

			WeatherWebServiceStub sub = new WeatherWebServiceStub();
			GetSupportProvince set = new GetSupportProvince();
			GetSupportProvinceResponse setpsp = sub.getSupportProvince(set);
			ArrayOfString a = setpsp.getGetSupportProvinceResult();
			String[] resultArray = a.getString();
			Gson gson = new Gson();
			System.out.println(gson.toJson(resultArray));
			list.addAll(Arrays.asList(resultArray));

			Date now = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
			SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			String nowDate = sdf.format(now);
			String nowHour = sdfHour.format(now);
			String dataMinute = sdfMinute.format(now);
			String dataSecond = sdfSecond.format(now);

			for (String provinceName:list) {
				WebServiceProvince provinceDb=webServiceWeatherService.findProvinceByProvinceName(provinceName);
				if(provinceDb==null){
					WebServiceProvince province = new WebServiceProvince();
					province.setProvinceName(provinceName);
					province.setCreateDate(now);
					province.setDataHour(nowHour);
					province.setDataMinute(dataMinute);
					province.setDataSecond(dataSecond);
					webServiceWeatherService.saveWebServiceProvince(province);
				}
			}

			for (String provinceName:list) {
				List<String> cityList = getCitListByProvince(provinceName);
				for (String cityStr:cityList) {
					String[] city= cityStr.split(" ");// 分割字符串
					if(city.length>0){
						System.out.println(gson.toJson(city));
						String cityNameCn = city[0];
						String cityCodeTemp = city[city.length-1];
						String cityCode = cityCodeTemp.replace("(","").replace(")","");
						String cityNameEn = "";

						if(city.length==2){
							cityNameEn = "";
						}else if(city.length==3){
							cityNameEn = city[1];
						}else if(city.length==4){
							cityNameEn = city[1]+" "+city[2];
						}else if(city.length==5){
							cityNameEn = city[1]+" "+city[2]+" "+city[3];
						}else if(city.length==6){
							cityNameEn = city[1]+" "+city[2]+" "+city[3]+" "+city[4];
						}
						System.out.println(cityNameEn);
						System.out.println(cityCode);

						WebServiceCity cityDb = webServiceWeatherService.findByCode(cityCode);
						if(cityDb==null){
							WebServiceCity citySave = new WebServiceCity();
							citySave.setCityName(cityNameCn);
							citySave.setCityNameEn(cityNameEn);
							citySave.setCode(cityCode);
							citySave.setCreateDate(now);
							citySave.setDataHour(nowHour);
							citySave.setDataMinute(dataMinute);
							citySave.setDataSecond(dataSecond);
							webServiceWeatherService.saveWebServiceCity(citySave);
						}else{
							System.out.println(cityNameCn);
							System.out.println(cityNameEn);
							System.out.println(cityCode);
							System.out.println("城市已存在");
						}
					}
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return list;
	}
}
