package com.yushu.common.config;

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

import java.io.File;
import java.util.List;

/**
 * 文件存储配置
 * 
 * @author yushu
 */
@Component
@ConfigurationProperties(prefix = "file.storage")
public class FileStorageConfig
{
    /** 存储类型 */
    private String type = "local";
    
    /** 本地存储配置 */
    private LocalConfig local = new LocalConfig();
    
    /** OSS配置 */
    private OssConfig oss = new OssConfig();
    
    /** MinIO配置 */
    private MinioConfig minio = new MinioConfig();
    
    public String getType()
    {
        return type;
    }
    
    public void setType(String type)
    {
        this.type = type;
    }
    
    public LocalConfig getLocal()
    {
        return local;
    }
    
    public void setLocal(LocalConfig local)
    {
        this.local = local;
    }
    
    public OssConfig getOss()
    {
        return oss;
    }
    
    public void setOss(OssConfig oss)
    {
        this.oss = oss;
    }
    
    public MinioConfig getMinio()
    {
        return minio;
    }
    
    public void setMinio(MinioConfig minio)
    {
        this.minio = minio;
    }
    
    /**
     * 本地存储配置
     */
    public static class LocalConfig
    {
        /** 基础路径 */
        private String basePath;
        
        /** URL前缀 */
        private String urlPrefix;
        
        /** 最大文件大小（MB） */
        private Integer maxFileSize = 100;
        
        /** 允许的文件扩展名 */
        private List<String> allowedExtensions;
        
        /**
         * 获取规范化的基础路径（自动适配操作系统）
         */
        public String getNormalizedBasePath()
        {
            if (basePath == null || basePath.isEmpty())
            {
                return System.getProperty("user.home") + File.separator + "yushu" + File.separator + "upload";
            }
            
            // 替换路径分隔符以适配当前操作系统
            String normalizedPath = basePath.replace("/", File.separator).replace("\\", File.separator);
            
            // 确保路径存在
            File dir = new File(normalizedPath);
            if (!dir.exists())
            {
                dir.mkdirs();
            }
            
            return normalizedPath;
        }
        
        public String getBasePath()
        {
            return basePath;
        }
        
        public void setBasePath(String basePath)
        {
            this.basePath = basePath;
        }
        
        public String getUrlPrefix()
        {
            return urlPrefix;
        }
        
        public void setUrlPrefix(String urlPrefix)
        {
            this.urlPrefix = urlPrefix;
        }
        
        public Integer getMaxFileSize()
        {
            return maxFileSize;
        }
        
        public void setMaxFileSize(Integer maxFileSize)
        {
            this.maxFileSize = maxFileSize;
        }
        
        public List<String> getAllowedExtensions()
        {
            return allowedExtensions;
        }
        
        public void setAllowedExtensions(List<String> allowedExtensions)
        {
            this.allowedExtensions = allowedExtensions;
        }
    }
    
    /**
     * OSS配置
     */
    public static class OssConfig
    {
        private String endpoint;
        private String accessKeyId;
        private String accessKeySecret;
        private String bucketName;
        
        // Getters and Setters
        public String getEndpoint()
        {
            return endpoint;
        }
        
        public void setEndpoint(String endpoint)
        {
            this.endpoint = endpoint;
        }
        
        public String getAccessKeyId()
        {
            return accessKeyId;
        }
        
        public void setAccessKeyId(String accessKeyId)
        {
            this.accessKeyId = accessKeyId;
        }
        
        public String getAccessKeySecret()
        {
            return accessKeySecret;
        }
        
        public void setAccessKeySecret(String accessKeySecret)
        {
            this.accessKeySecret = accessKeySecret;
        }
        
        public String getBucketName()
        {
            return bucketName;
        }
        
        public void setBucketName(String bucketName)
        {
            this.bucketName = bucketName;
        }
    }
    
    /**
     * MinIO配置
     */
    public static class MinioConfig
    {
        private String endpoint;
        private String accessKey;
        private String secretKey;
        private String bucketName;
        
        // Getters and Setters
        public String getEndpoint()
        {
            return endpoint;
        }
        
        public void setEndpoint(String endpoint)
        {
            this.endpoint = endpoint;
        }
        
        public String getAccessKey()
        {
            return accessKey;
        }
        
        public void setAccessKey(String accessKey)
        {
            this.accessKey = accessKey;
        }
        
        public String getSecretKey()
        {
            return secretKey;
        }
        
        public void setSecretKey(String secretKey)
        {
            this.secretKey = secretKey;
        }
        
        public String getBucketName()
        {
            return bucketName;
        }
        
        public void setBucketName(String bucketName)
        {
            this.bucketName = bucketName;
        }
    }
}
