/*
 * 文 件 名:  AmapWeatherConfig
 * 版    权:
 * 描    述:  <描述>
 * 修 改 人:  learshaw
 * 修改时间:  2021/9/22
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.data.config;

import com.huashe.common.data.model.BaseWeather;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 高德天气配置
 * <功能详细描述>
 *
 * @author learshaw
 * @version [版本号, 2021/9/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Configuration
@ConfigurationProperties(prefix = "weather.juhe")
public class JuheWeatherConfig extends BaseWeather {
    /**
     * 访问地址
     * <br/>https://lbs.amap.com/api/webservice/guide/api/weatherinfo
     */
    private String url;

    /**
     * 高德访问
     */
    private String key;

    /**
     * 天气映射
     */
    private Map<String, String> weatherMapper;


    @PostConstruct
    public void init() {
        this.weatherMapper = resetCacheMap(weatherList);
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getKey() {
        return key;
    }

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

    public String getWeatherTypeByName(String name) {
        String ret = null;

        for (Map.Entry<String, String> entry : weatherMapper.entrySet()) {
            String key = entry.getKey();

            if (StringUtils.equals(name, key) || StringUtils.contains(name, key)) {
                ret = entry.getValue();
                break;
            }
        }

        return ret;
    }

    public Map<String, String> resetCacheMap(List<String> list) {
        Map<String, String> cacheMap = new HashMap<>();

        for (String str : list) {
            String[] array = StringUtils.split(str, ":");

            String[] subKeys = StringUtils.split(array[1], ",");

            for (String subKey : subKeys) {
                cacheMap.put(subKey, array[0]);
            }
        }

        return cacheMap;
    }
}