package com.its.wthr.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.util.Properties;

@Slf4j
@Getter
@Setter
@ToString
@Configuration
@ConfigurationProperties(prefix = "application.atmp")
public class AtmpConfig {

    private boolean enabled = false;
    private String crontab;
    private String apiKey;
    private String apiVer;
    private int startHour = 0;
    private int endHour = 23;

    @PostConstruct
    private void init() {
        //loadApiKey();
        log.info("{}", this);
    }

    public boolean loadApiKey() {
        try {
            String workingDir = System.getProperty("user.dir");
            FileInputStream in = null;
            try {
                in = new FileInputStream(workingDir + "/conf/apiKey.properties");
            }
            catch(Exception e) {
                log.error("{}.loadApiKey: Exception: {}", this.getClass().getSimpleName(), e.toString());
                return false;
            }

            Properties props = new Properties();
            props.load(in);
            in.close();

            String apiKey = props.getProperty("atmp-key",  "x").trim();
            if (!"x".equals(apiKey)) {
                log.info("ATMP Key: {}", apiKey);
                this.apiKey = apiKey;
            }
        }
        catch(Exception e) {
            log.error("{}.loadApiKey: Exception: {}", this.getClass().getSimpleName(), e.toString());
            return false;
        }
        return true;
    }
}
