package com.erp.warehouse.client;

import com.erp.warehouse.entity.Warehouse;
import com.erp.warehouse.client.jst.JstClient;
import com.erp.warehouse.client.wdt.WdtClient;
import com.erp.warehouse.client.qimen.QimenClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 云仓客户端工厂 - 根据仓库类型创建对应的客户端
 */
@Component
public class CloudWmsClientFactory {

    @Autowired
    private JstClient jstClient;

    @Autowired
    private WdtClient wdtClient;

    @Autowired
    private QimenClient qimenClient;

    /**
     * 根据仓库创建云仓客户端
     * @param warehouse 仓库实体
     * @return 对应的云仓客户端
     */
    public CloudWmsClient createClient(Warehouse warehouse) {
        if (warehouse == null || !"cloud".equals(warehouse.getType())) {
            throw new IllegalArgumentException("无效的云仓配置");
        }

        String cloudSystem = warehouse.getCloudSystem();
        CloudWmsConfig config = buildConfig(warehouse);

        return switch (cloudSystem.toLowerCase()) {
            case "jst" -> jstClient.withConfig(config);
            case "wdt" -> wdtClient.withConfig(config);
            case "qimen" -> qimenClient.withConfig(config);
            default -> throw new IllegalArgumentException("不支持的云仓系统: " + cloudSystem);
        };
    }

    /**
     * 根据云仓系统类型创建客户端
     */
    public CloudWmsClient createClient(String cloudSystem, CloudWmsConfig config) {
        return switch (cloudSystem.toLowerCase()) {
            case "jst" -> jstClient.withConfig(config);
            case "wdt" -> wdtClient.withConfig(config);
            case "qimen" -> qimenClient.withConfig(config);
            default -> throw new IllegalArgumentException("不支持的云仓系统: " + cloudSystem);
        };
    }

    private CloudWmsConfig buildConfig(Warehouse warehouse) {
        return CloudWmsConfig.builder()
                .appKey(warehouse.getAppKey())
                .appSecret(warehouse.getAppSecret())
                .apiUrl(warehouse.getApiUrl())
                .ownerCode(warehouse.getOwnerCode())
                .cloudCode(warehouse.getCloudCode())
                .build();
    }

    /**
     * 云仓客户端配置
     */
    public static class CloudWmsConfig {
        private String appKey;
        private String appSecret;
        private String apiUrl;
        private String ownerCode;
        private String cloudCode;

        public CloudWmsConfig() {}
        public CloudWmsConfig(String appKey, String appSecret, String apiUrl, String ownerCode, String cloudCode) {
            this.appKey = appKey;
            this.appSecret = appSecret;
            this.apiUrl = apiUrl;
            this.ownerCode = ownerCode;
            this.cloudCode = cloudCode;
        }
        public String getAppKey() { return appKey; }
        public void setAppKey(String appKey) { this.appKey = appKey; }
        public String getAppSecret() { return appSecret; }
        public void setAppSecret(String appSecret) { this.appSecret = appSecret; }
        public String getApiUrl() { return apiUrl; }
        public void setApiUrl(String apiUrl) { this.apiUrl = apiUrl; }
        public String getOwnerCode() { return ownerCode; }
        public void setOwnerCode(String ownerCode) { this.ownerCode = ownerCode; }
        public String getCloudCode() { return cloudCode; }
        public void setCloudCode(String cloudCode) { this.cloudCode = cloudCode; }

        public static CloudWmsConfigBuilder builder() { return new CloudWmsConfigBuilder(); }
        public static class CloudWmsConfigBuilder {
            private String appKey;
            private String appSecret;
            private String apiUrl;
            private String ownerCode;
            private String cloudCode;
            public CloudWmsConfigBuilder appKey(String appKey) { this.appKey = appKey; return this; }
            public CloudWmsConfigBuilder appSecret(String appSecret) { this.appSecret = appSecret; return this; }
            public CloudWmsConfigBuilder apiUrl(String apiUrl) { this.apiUrl = apiUrl; return this; }
            public CloudWmsConfigBuilder ownerCode(String ownerCode) { this.ownerCode = ownerCode; return this; }
            public CloudWmsConfigBuilder cloudCode(String cloudCode) { this.cloudCode = cloudCode; return this; }
            public CloudWmsConfig build() { return new CloudWmsConfig(appKey, appSecret, apiUrl, ownerCode, cloudCode); }
        }
    }
}
