/*
 * @Author: Kane
 * @Date: 2022-04-22 10:53:49
 * @LastEditors: Kane
 * @LastEditTime: 2022-04-26 22:33:14
 * @FilePath: \DisasterWarning\src\main\java\AppMain.java
 * @Description: 和风天气预警推送厦门太保公众号主程序！
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
import com.cpic.xim.config.City;
import com.cpic.xim.notify.disaster.QWeatherDisasterWarning;
import com.cpic.xim.notify.disaster.WeatherDisasterWarningGrabber;
import com.cpic.xim.config.WeatherDisasterNotifyConfig;

import java.io.IOException;
import java.util.Vector;
import java.util.logging.*;


public class AppMain
{
    private final static String LOG_FILE_PATH = "./app.log";

    public static void main( String[] args )
    {
        String json;
        WeatherDisasterNotifyConfig config = null;
        QWeatherDisasterWarning warning = null;
        Logger logger = null;

        // 配置logger
        try
        {
            setRootLogger();

            logger = Logger.getLogger( "com.cpicxim" );
        }
        catch ( IOException error )
        {
            System.out.println( "配置logger失败，原因：" + error.getMessage() );

            return;
        }


        // 读取配置
        try
        {
            config = WeatherDisasterNotifyConfig.getConfig();
        }
        catch ( IOException error )
        {
            System.out.println( "读取配置文件失败！" );
            System.out.println( error.getMessage() );

            logger.log( Level.SEVERE, "读取配置文件失败：{0}", error.getMessage() );

            return;
        }

        Vector<City> cities = config.getCities();
        String queryURL = config.getQueryUrl();
        String userKey = config.getKey();

        // 遍历所有城市，查询是否有警报，有则推送。
        for ( City city : cities )
        {
            try
            {


                json = WeatherDisasterWarningGrabber.getWeatherDisasterWarningJSON( queryURL,
                        userKey, city.getCityCode() );
                warning = WeatherDisasterWarningGrabber.convertWeatherDisasterWarning( json );

                logger.log( Level.INFO, "查询{0}天气预警，结果：{1}。", new Object[]
                { city.getCityName(), json} );

                // 判断是否有警报
                if ( warning.getWarning().isEmpty() == true)
                {
                    continue;
                }

                logger.log( Level.INFO, "查询{0}天气预警，发送日志。", new Object[]
                { city.getCityName()} );

                // sendMessage.sendWeatherDisasterWarning( config.getWechatOfficalAccountURL(),
                // warning );
            }
            catch ( IOException error )
            {
                System.out.println( "查询" + city.getCityName() + "出现异常！" );
                System.out.println( error.getMessage() );

                logger.log( Level.SEVERE, "查询 {0} 出现异常:{1}。", new Object[]
                { city.getCityName(), error.getMessage()} );

                continue;
            }
        }
    }

    /**
     * 设置JUL的logger。
     */
    private static void setRootLogger() throws IOException
    {
        Logger rootLogger = Logger.getLogger( "com.cpicxim" );
        ConsoleHandler consoleHandler = new ConsoleHandler();
        FileHandler fileHandler = new FileHandler( LOG_FILE_PATH );
        SimpleFormatter formatter = new SimpleFormatter();

        consoleHandler.setFormatter( formatter );
        fileHandler.setFormatter( formatter );
        rootLogger.addHandler( consoleHandler );
        rootLogger.addHandler( fileHandler );

        rootLogger.setUseParentHandlers( false );
        rootLogger.setLevel( Level.ALL );
    }
}
