package com.zhangmeng.file.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zhengmeng
 * @version 1.0
 * @date 2021年1月7日11:10:12
 */
@Configuration
@ConfigurationProperties(prefix = "file")
public class FileConfig implements ApplicationListener<WebServerInitializedEvent> {

    private int serverPort;

    @Value("${file.type}")
    private Type type;

    @NestedConfigurationProperty
    private Path path;

    @NestedConfigurationProperty
    private Url url;

    public static class Url {

        private String localhost;
        private String aliyun;
        private String vmware;

        public String getLocalhost() {
            return localhost;
        }

        public void setLocalhost(String localhost) {
            this.localhost = localhost;
        }

        public String getAliyun() {
            return aliyun;
        }

        public void setAliyun(String aliyun) {
            this.aliyun = aliyun;
        }

        public String getVmware() {
            return vmware;
        }

        public void setVmware(String vmware) {
            this.vmware = vmware;
        }
    }

    public static class Path {

        private String localhost;

        private String aliyun;

        public String getLocalhost() {
            return localhost;
        }

        public void setLocalhost(String localhost) {
            this.localhost = localhost;
        }

        public String getAliyun() {
            return aliyun;
        }

        public void setAliyun(String aliyun) {
            this.aliyun = aliyun;
        }
    }

    /**
     * 类型
     */
    public enum Type {
        /**
         * 本地
         */
        localhost("本地"),

        /**
         * 阿里云
         */
        aliyun("阿里云"),


        /**
         * 虚拟机
         */
        vmware("虚拟机");

        /**
         * 描述
         */
        private String description;

        private Type(String description) {
            this.description = description;
        }

        public String getDesc() {
            return description;
        }

        public void setDesc(String description) {
            this.description = description;
        }
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Path getPath() {
        return path;
    }

    public void setPath(Path path) {
        this.path = path;
    }

    public Url getUrl() {
        return url;
    }

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

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
}
