package com.chinaweal.aiccs.config;

import com.chinaweal.aiccs.aiccs.business.rules.init.InitRules;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 通用配置
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021/1/26 14:14
 **/
@Component
public class CommonConfig {

    private static String uploadDrive;

    private static String uploadPath;

    @Value("${rules.dir}")
    private String ruleDir;

    @Value("${storage.path.uploadDrive}")
    public void setUploadDrive(String uploadDrive) {
        CommonConfig.uploadDrive = uploadDrive;
    }

    @Value("${storage.path.uploadPath}")
    public void setUploadPath(String uploadPath) {
        CommonConfig.uploadPath = uploadPath;
    }

    /**
     * 获取上传目录
     *
     * @return 上传目录
     * @author lroyia
     * @since 2021年1月26日 14:50:14
     */
    public static String getUploadPath() {
        return getUploadPathByDate();
    }

    public static String getUploadPathByDate() {
        LocalDate now = LocalDate.now();
        String dateStr = now.format(DateTimeFormatter.ofPattern("dd"));
        String monthStr = now.format(DateTimeFormatter.ofPattern("MM"));
        String yearStr = now.format(DateTimeFormatter.ofPattern("yyyy"));
        String datePath = File.separator + yearStr + File.separator + monthStr + File.separator + dateStr;
        return uploadDrive + uploadPath + datePath;
    }

    public static String getUploadPathByDate(LocalDate now) {
        String dateStr = now.format(DateTimeFormatter.ofPattern("dd"));
        String monthStr = now.format(DateTimeFormatter.ofPattern("MM"));
        String yearStr = now.format(DateTimeFormatter.ofPattern("yyyy"));
        String datePath = File.separator + yearStr + File.separator + monthStr + File.separator + dateStr;
        return uploadDrive + uploadPath + datePath;
    }

    /**
     * 获取涉嫌冒用上传目录
     *
     * @return 上传目录
     * @author lroyia
     * @since 2021年1月26日 14:50:14
     */
    public static String getFraudRegPath() {
        return getUploadPathByDate() + File.separator + "fraudReg";
    }

    /**
     * 获取信用修复上传目录
     *
     * @return 上传目录
     * @author lroyia
     * @since 2021年1月26日 14:50:14
     */
    public static String getRepairPath() {
        return getUploadPath() + File.separator + "repair";
    }

    /**
     * 获取行政处罚修复上传目录
     *
     * @return 上传目录
     * @author ymc
     * @since 2023年4月11日 11:05
     */
    public static String getRemPunishmentPath() {
        return getUploadPath() + File.separator + "remPunishment";
    }

    /**
     * 获取文书上传目录
     *
     * @return 上传目录
     * @author ymc
     * @since 2024年9月26日 14:42
     */
    public static String getWritPath() {
        return getUploadPath() + File.separator + "write";
    }


    @Bean
    public InitRules initRules() {
        return new InitRules(ruleDir);
    }

    @Bean
    public RestTemplate restTemplate() {
        try {
            // 信任所有证书
            SSLContext sslContext = SSLContexts.custom()
                    .loadTrustMaterial(null, (chain, authType) -> true)
                    .build();
            // 禁用主机名验证
            CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLContext(sslContext)
                    .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                    .build();
            // 配置RestTemplate
            HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
            return new RestTemplate(factory);
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
            throw new RuntimeException(e);
        }

    }
}
