package com.erp.warehouse.client.jst;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.erp.warehouse.client.CloudWmsClient;
import com.erp.warehouse.client.CloudWmsClientFactory.CloudWmsConfig;
import com.erp.warehouse.dto.PurchaseOrderDTO;
import com.erp.warehouse.dto.SaleOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Duration;
import java.util.*;

/**
 * 聚水潭WMS客户端
 * 文档: https://open.jushuitan.com/
 */
@Slf4j
@Component
public class JstClient implements CloudWmsClient {

    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JstClient.class);
    private static final String CACHE_KEY_PREFIX = "jst_access_token_";
    private static final Duration TOKEN_EXPIRE = Duration.ofHours(2);

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RestTemplate restTemplate;

    private CloudWmsConfig config;

    public JstClient withConfig(CloudWmsConfig config) {
        this.config = config;
        return this;
    }

    @Override
    public Object testConnection() {
        return request("jushuitan.shop.list.get", Map.of("page_size", 10));
    }

    @Override
    public Object getGoodsList(int page, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("page_size", pageSize);
        return request("jushuitan.item.list.get", params);
    }

    @Override
    public Object getGoodsDetail(String itemId) {
        return request("jushuitan.item.get", Map.of("item_id", itemId));
    }

    @Override
    public Object getStockList(String warehouseCode, int page, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("page_size", pageSize);
        params.put("warehouse_code", warehouseCode);
        return request("jushuitan.stock.list.get", params);
    }

    @Override
    public Object queryStock(String skuCode, String warehouseCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("item_code", skuCode);
        if (warehouseCode != null && !warehouseCode.isEmpty()) {
            params.put("warehouse_code", warehouseCode);
        }
        return request("jushuitan.stock.detail.get", params);
    }

    @Override
    public CloudWmsResponse pushPurchaseOrder(PurchaseOrderDTO purchaseOrder) {
        Map<String, Object> order = new HashMap<>();
        order.put("po_order", purchaseOrder.getPoOrderNo());
        order.put("supplier_code", purchaseOrder.getSupplierCode());
        order.put("warehouse_code", purchaseOrder.getWarehouseCode());
        order.put("expected_date", purchaseOrder.getExpectedDate() != null ? 
                purchaseOrder.getExpectedDate().toString() : java.time.LocalDate.now().toString());
        order.put("remark", purchaseOrder.getRemark());
        
        List<Map<String, Object>> details = new ArrayList<>();
        if (purchaseOrder.getItems() != null) {
            for (PurchaseOrderDTO.PurchaseOrderItemDTO item : purchaseOrder.getItems()) {
                Map<String, Object> detail = new HashMap<>();
                detail.put("item_code", item.getSkuCode());
                detail.put("item_name", item.getName());
                detail.put("qty", item.getQuantity());
                detail.put("price", item.getPrice());
                details.add(detail);
            }
        }
        order.put("details", details);

        return requestResponse("jushuitan.purchase.order.create", order);
    }

    @Override
    public CloudWmsResponse confirmPurchaseOrder(String poOrderNo) {
        Map<String, Object> params = Map.of("po_order", poOrderNo);
        return requestResponse("jushuitan.purchase.order.confirm", params);
    }

    @Override
    public CloudWmsResponse pushSaleOrder(SaleOrderDTO saleOrder) {
        Map<String, Object> order = new HashMap<>();
        order.put("shop_order", saleOrder.getOrderNo());
        order.put("warehouse_code", saleOrder.getWarehouseCode());
        order.put("receiver_name", saleOrder.getReceiverName());
        order.put("receiver_phone", saleOrder.getReceiverPhone());
        order.put("receiver_province", saleOrder.getReceiverProvince());
        order.put("receiver_city", saleOrder.getReceiverCity());
        order.put("receiver_district", saleOrder.getReceiverDistrict());
        order.put("receiver_address", saleOrder.getReceiverAddress());
        order.put("logistics_code", saleOrder.getLogisticsCode());
        order.put("logistics_no", saleOrder.getLogisticsNo());
        order.put("remark", saleOrder.getRemark());

        List<Map<String, Object>> details = new ArrayList<>();
        if (saleOrder.getItems() != null) {
            for (SaleOrderDTO.SaleOrderItemDTO item : saleOrder.getItems()) {
                Map<String, Object> detail = new HashMap<>();
                detail.put("item_code", item.getSkuCode());
                detail.put("item_name", item.getName());
                detail.put("qty", item.getQuantity());
                detail.put("price", item.getPrice());
                details.add(detail);
            }
        }
        order.put("details", details);

        return requestResponse("jushuitan.trade.order.create", order);
    }

    @Override
    public Object getLogisticsTrace(String logisticsNo) {
        return request("jushuitan.logistics.trace.get", Map.of("logistics_no", logisticsNo));
    }

    @Override
    public CloudWmsResponse adjustStock(String warehouseCode, String skuCode, int type, int quantity, String reason) {
        Map<String, Object> params = new HashMap<>();
        params.put("warehouse_code", warehouseCode);
        params.put("item_code", skuCode);
        params.put("type", type);
        params.put("qty", quantity);
        params.put("reason", reason);
        return requestResponse("jushuitan.stock.adjust", params);
    }

    @Override
    public Object getWarehouseList() {
        return request("jushuitan.warehouse.list.get", Map.of());
    }

    /**
     * 发送API请求
     */
    private Object request(String method, Map<String, Object> params) {
        String accessToken = getAccessToken();
        
        Map<String, Object> requestParams = new HashMap<>(params);
        requestParams.put("app_key", config.getAppKey());
        requestParams.put("access_token", accessToken);
        requestParams.put("method", method);
        requestParams.put("timestamp", java.time.LocalDateTime.now().format(
                java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        requestParams.put("format", "json");
        requestParams.put("v", "1.0");
        
        // 生成签名
        requestParams.put("sign", generateSign(requestParams));

        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestParams, headers);
            ResponseEntity<String> response = restTemplate.exchange(
                    config.getApiUrl() + "/api",
                    HttpMethod.POST,
                    entity,
                    String.class
            );

            JSONObject result = JSON.parseObject(response.getBody());
            
            if (result == null) {
                return CloudWmsResponse.error(-1, "请求失败: 响应为空");
            }

            Integer code = result.getInteger("code");
            if (code != null && code != 0) {
                log.warn("JST API错误: method={}, code={}, message={}", 
                        method, code, result.getString("message"));
                return CloudWmsResponse.error(code, result.getString("message"));
            }

            return CloudWmsResponse.success(result.get("data"));

        } catch (Exception e) {
            log.error("JST请求异常: method={}, error={}", method, e.getMessage(), e);
            return CloudWmsResponse.error(-1, e.getMessage());
        }
    }

    /**
     * 发送API请求并返回CloudWmsResponse
     */
    private CloudWmsResponse requestResponse(String method, Map<String, Object> params) {
        Object result = request(method, params);
        if (result instanceof CloudWmsResponse) {
            return (CloudWmsResponse) result;
        }
        return CloudWmsResponse.error(-1, "未知错误");
    }

    /**
     * 获取访问令牌
     */
    private String getAccessToken() {
        String cacheKey = CACHE_KEY_PREFIX + md5(config.getAppKey());
        
        String cachedToken = redisTemplate.opsForValue().get(cacheKey);
        if (cachedToken != null && !cachedToken.isEmpty()) {
            return cachedToken;
        }

        try {
            Map<String, Object> params = new HashMap<>();
            params.put("app_key", config.getAppKey());
            params.put("app_secret", config.getAppSecret());
            params.put("grant_type", "silent");

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers);
            ResponseEntity<String> response = restTemplate.exchange(
                    config.getApiUrl() + "/oauth/token",
                    HttpMethod.POST,
                    entity,
                    String.class
            );

            JSONObject result = JSON.parseObject(response.getBody());
            String accessToken = result.getString("access_token");

            if (accessToken != null && !accessToken.isEmpty()) {
                redisTemplate.opsForValue().set(cacheKey, accessToken, TOKEN_EXPIRE);
                return accessToken;
            }

            throw new RuntimeException("获取聚水潭访问令牌失败");

        } catch (Exception e) {
            log.error("获取聚水潭访问令牌异常: {}", e.getMessage(), e);
            throw new RuntimeException("获取聚水潭访问令牌失败: " + e.getMessage());
        }
    }

    /**
     * 生成签名
     */
    private String generateSign(Map<String, Object> params) {
        // 按key排序
        List<String> keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);

        StringBuilder signString = new StringBuilder();
        for (String key : keys) {
            Object value = params.get(key);
            if (value != null && !value.toString().isEmpty() && !"sign".equals(key)) {
                signString.append(key).append(value.toString());
            }
        }
        
        String sign = signString.toString() + config.getAppSecret();
        return md5(sign).toUpperCase();
    }

    private String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("MD5计算失败", e);
        }
    }
}
