package cn.iocoder.yudao.module.mes.controller.admin.home;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.framework.util.HttpUtils;
import cn.iocoder.yudao.module.mes.controller.admin.home.weather.Forecast;
import cn.iocoder.yudao.module.mes.controller.admin.home.weather.WeatherInfoGaodeVO;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "登录首页信息")
@RestController
@RequestMapping("/home/info")
@Validated
public class HomeController {



    //中文名	adcode	citycode   呼和浩特市	150100	0471
    //https://restapi.amap.com/v3/weather/weatherInfo?key=1a63897dd11e83ae0be4cc922c07b523&city=150100
    @GetMapping("/weatherInfo")
    @Operation(summary = "获得天气")
    public CommonResult<Forecast> getTask() {
        WeatherInfoGaodeVO gaodeVO =  getWeather();
        Forecast forecast = null;
        if(gaodeVO.getStatus().equals("1") && gaodeVO.getForecasts()!=null && gaodeVO.getForecasts().size()>0){
            forecast = gaodeVO.getForecasts().get(0);
            forecast.setIsEnable(true);
        }
        else {
            forecast = new Forecast();
            forecast.setIsEnable(false);
        }
        return success(forecast);
    }

    public WeatherInfoGaodeVO getWeather() {
        String key = "2f0de30f37da59122eeb774d3a2ba6da";
        String city = "150100";
        String extensions = "all";
        String URL= "https://restapi.amap.com/v3/weather/weatherInfo?key="+ key +"&city="+city+"&extensions="+extensions;
        // 创建 HttpClient 客户端
        CloseableHttpClient httpClient = HttpUtils.createSSLClientDefault();
        CloseableHttpResponse httpResponse = null;
        WeatherInfoGaodeVO gaodeVO = null;
        try {
            HttpGet httpGet = HttpUtils.getHttpGet(URL,new ArrayList<>());
            httpResponse = httpClient.execute(httpGet);
            StatusLine statusLine = httpResponse.getStatusLine();
            if (statusLine.getStatusCode() == 200) {
                String entityStr = EntityUtils.toString(httpResponse.getEntity());
                gaodeVO = JSON.parseObject(entityStr, WeatherInfoGaodeVO.class);
            }
        }
        // 无论如何必须关闭连接
        catch (IOException e) {
            e.printStackTrace();
        } finally {
            HttpUtils.closeHttp(httpClient, httpResponse);
        }
        return gaodeVO;
    }

}