/**
 * @author 王炜
 * @version 1.0
 */
package com.cpic.xim.notify.disaster;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.zip.GZIPInputStream;

/**
 *
 */
public class WeatherDisasterWarningGrabber
{
    private final static String QUERY_URL = "https://devapi.qweather.com/v7/warning/now?";
    private final static String USER_KEY  = "fe9fa8eeeb6f4301a92541eed565dd15";

    /***
     * 从和风天气获取天气警报json字符串
     * @param cityCode 城市或区域代码
     * @return 返回警报的json字符串
     */
    public static String getWeatherDisasterWarningJSON( String queryURL,
                                                        String userKey,
                                                        String cityCode )
    {
        //拼接url字符串
        String json       = "";
        String requestURL = queryURL + "key=" + userKey + "&location=" + cityCode;

        //链接用
        HttpURLConnection connection     = null;
        URL               url            = null;
        InputStream       inputStream    = null;
        BufferedReader    bufferedReader = null;
        StringBuilder     buffer         = new StringBuilder();

        try
        {
            url        = new URL( requestURL );
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod( "GET" );
            connection.setConnectTimeout( 15000 );
            connection.setReadTimeout( 60000 );
            connection.connect();

            //如果responseCode为200，说明访问成功！
            if ( connection.getResponseCode() == 200 )
            {
                //注意，和风使用了gzip压缩响应体
                inputStream    = new GZIPInputStream( connection.getInputStream() );
                bufferedReader = new BufferedReader( new InputStreamReader( inputStream, "UTF-8" ) );

                //读出数据
                String temp = bufferedReader.readLine();

                while ( temp != null )
                {
                    buffer.append( temp );

                    temp = bufferedReader.readLine();
                }

                json = buffer.toString();
            }
        }
        catch ( MalformedURLException error )
        {
            error.printStackTrace();
        }
        catch ( IOException error )
        {
            System.out.println( "读取失败！" );
        }
        finally
        {
            if ( bufferedReader != null )
            {
                try
                {
                    bufferedReader.close();
                }
                catch ( IOException error )
                {
                    error.printStackTrace();
                }
            }

            if ( inputStream != null )
            {
                try
                {
                    inputStream.close();
                }
                catch ( IOException error )
                {
                    error.printStackTrace();
                }
            }

            if ( connection != null )
            {
                connection.disconnect();
            }
        }

        return json;
    }

    /**
     * 将天气警告的json字符串转换成java对象。
     * @param json json字符串
     * @return  返回 QWeatherDisasterWarning 对象。
     * @throws IOException
     */
    public static QWeatherDisasterWarning convertWeatherDisasterWarning( String json )
            throws IOException
    {
        ObjectMapper mapper = new ObjectMapper();
        QWeatherDisasterWarning warning = mapper.readValue( json, QWeatherDisasterWarning.class );

        return warning;
    }
}
