package cn.com.qmth.stmms.ms.commons.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by zhengmin on 2017/2/27.
 */
@Component
@ConfigurationProperties(prefix = "sys.config.imageServer")
public class ImageServerConfig {

    @Resource
    AliYunOssConfig aliYunOssConfig;

    @Resource
    SystemConfig systemConfig;

    private String ip;
    private String port;
    private boolean aliyunOss;
    private String dir;

    public String getIp() {
        try {
            if (ip == null || ip.isEmpty()) {
                ip = InetAddress.getLocalHost().getHostAddress();

            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return (port == null || port.isEmpty()) ? "3000" : port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getImageServer() {
        if (aliyunOss) {
            return aliYunOssConfig.getUrl() + File.separator + this.getDir() + File.separator+ "upload";
        } else {
            return "http://" + this.getIp() + ":" + this.getPort() + File.separator + "upload";
        }
    }


    public String getBaseServer() {
        //如果是图片加密，也需要调用接口处理图片。
        //所以是否上传到阿里云，调用接口相同，然后去接口处理阿里云和本地文件逻辑
        if (aliyunOss) {
//            return aliYunOssConfig.getUrl() + File.separator + this.getDir();
            return "http://" + this.getIp() + ":" + this.getPort();
        } else {
            return "http://" + this.getIp() + ":" + this.getPort();
        }
    }

    public AliYunOssConfig getAliYunOssConfig() {
        return aliYunOssConfig;
    }

    public void setAliYunOssConfig(AliYunOssConfig aliYunOssConfig) {
        this.aliYunOssConfig = aliYunOssConfig;
    }

    public boolean isAliyunOss() {
        return aliyunOss;
    }

    public void setAliyunOss(boolean aliyunOss) {
        this.aliyunOss = aliyunOss;
    }
}
