package com.cpic.xim.config;

import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Vector;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

public class WeatherDisasterNotifyConfig
{
    private static final int BUFFER_SIZE = 1024;
    private static final String CONFIG_FILE_CHARSET = "UTF-8";
    private static final String CONFIG_FILE_PATH = "./config.json";
    private static WeatherDisasterNotifyConfig appConfig = null;

    public static WeatherDisasterNotifyConfig getConfig() throws IOException

    {
        if ( appConfig != null)
        {
            return appConfig;
        }

        ObjectMapper mapper = new ObjectMapper();
        FileInputStream configFile = null;
        InputStreamReader in = null;
        StringBuffer json = null;
        char[] buffer = new char[BUFFER_SIZE];

        // 设置json属性
        mapper.setPropertyNamingStrategy( PropertyNamingStrategy.SNAKE_CASE );

        try
        {
            configFile = new FileInputStream( CONFIG_FILE_PATH );
            in = new InputStreamReader( configFile, CONFIG_FILE_CHARSET );
            json = new StringBuffer();

            int length = in.read( buffer );

            while (length != -1)
            {
                json.append( buffer );

                length = in.read( buffer );
            }

            appConfig = mapper.readValue( json.toString(), WeatherDisasterNotifyConfig.class );
        }
        finally
        {
            if ( configFile != null)
            {
                try
                {
                    configFile.close();
                }
                catch ( IOException e )
                {
                    e.printStackTrace();
                }
            }
        }

        return appConfig;
    }

    public WeatherDisasterNotifyConfig()
    {};

    public String getTitle()
    {
        return title;
    }

    public void setTitle( String title )
    {
        this.title = title;
    }

    public String getKey()
    {
        return key;
    }

    public void setKey( String key )
    {
        this.key = key;
    }

    public String getQueryUrl()
    {
        return queryUrl;
    }

    public void setQueryUrl( String queryUrl )
    {
        this.queryUrl = queryUrl;
    }

    public Vector<City> getCities()
    {
        return cities;
    }

    public void setCities( Vector<City> cities )
    {
        this.cities = cities;
    }

    public Vector<CpicxmStuff> getNotifyStuffs()
    {
        return notifyStuffs;
    }

    public void setNotifyStuffs( Vector<CpicxmStuff> notifyStuffs )
    {
        this.notifyStuffs = notifyStuffs;
    }

    public String getWechatOfficalAccountURL()
    {
        return wechatOfficalAccountURL;
    }

    public void setWechatOfficalAccountURL( String wechatOfficalAccountURL )
    {
        this.wechatOfficalAccountURL = wechatOfficalAccountURL;
    }

    private String title;
    private String key;
    private String queryUrl;

    @JsonProperty( "wechat_officalaccount_url")
    private String wechatOfficalAccountURL;
    private Vector<City> cities;
    private Vector<CpicxmStuff> notifyStuffs;
}
