package com.haha.common.utils;

import com.haha.common.constant.CommonConstants;
import com.haha.common.constant.DeviceConstants;
import com.haha.common.constant.OrderConstants;
import com.haha.common.constant.SyncConstants;
import com.haha.common.vo.StatusLabel;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * 实体标签填充工具类
 * 用于统一处理实体的状态标签填充逻辑
 */
public class EntityLabelUtils {

    /**
     * 状态映射配置
     */
    private static final Map<String, Map<Integer, StatusLabel>> STATUS_MAPPING = new HashMap<>();
    
    static {
        // 通用状态映射（启用/禁用）
        Map<Integer, StatusLabel> commonStatus = new HashMap<>();
        commonStatus.put(CommonConstants.STATUS_ENABLED, StatusLabel.success("启用"));
        commonStatus.put(CommonConstants.STATUS_DISABLED, StatusLabel.danger("禁用"));
        STATUS_MAPPING.put("common", commonStatus);
        
        // 设备状态映射
        Map<Integer, StatusLabel> deviceStatus = new HashMap<>();
        deviceStatus.put(DeviceConstants.STATUS_ONLINE, StatusLabel.success("正常"));
        deviceStatus.put(DeviceConstants.STATUS_FROZEN, StatusLabel.warning("冻结"));
        deviceStatus.put(DeviceConstants.STATUS_OFFLINE, StatusLabel.info("离线"));
        deviceStatus.put(DeviceConstants.STATUS_MAINTENANCE, StatusLabel.danger("维护中"));
        STATUS_MAPPING.put("device", deviceStatus);
        
        // 同步状态映射
        Map<Integer, StatusLabel> syncStatus = new HashMap<>();
        syncStatus.put(SyncConstants.STATUS_NOT_SYNCED, StatusLabel.info("未同步"));
        syncStatus.put(SyncConstants.STATUS_SYNCING, StatusLabel.warning("同步中"));
        syncStatus.put(SyncConstants.STATUS_SYNCED, StatusLabel.success("已同步"));
        syncStatus.put(SyncConstants.STATUS_SYNC_FAILED, StatusLabel.danger("同步失败"));
        STATUS_MAPPING.put("sync", syncStatus);
        
        // 订单状态映射
        Map<Integer, StatusLabel> orderStatus = new HashMap<>();
        orderStatus.put(OrderConstants.STATUS_CANCELLED, StatusLabel.danger("已取消"));
        orderStatus.put(OrderConstants.STATUS_PENDING_PAYMENT, StatusLabel.warning("待支付"));
        orderStatus.put(OrderConstants.STATUS_COMPLETED, StatusLabel.success("已完成"));
        orderStatus.put(OrderConstants.STATUS_CLOSED, StatusLabel.info("已关闭"));
        STATUS_MAPPING.put("order", orderStatus);
    }
    
    /**
     * 获取状态标签
     *
     * @param type   状态类型（common/device/sync/order）
     * @param status 状态值
     * @return 状态标签
     */
    public static StatusLabel getStatusLabel(String type, Integer status) {
        if (status == null) {
            return StatusLabel.info("未知");
        }
        Map<Integer, StatusLabel> mapping = STATUS_MAPPING.get(type);
        if (mapping == null) {
            return StatusLabel.info("未知");
        }
        return mapping.getOrDefault(status, StatusLabel.info("未知"));
    }
    
    /**
     * 获取支付状态标签（字符串类型）
     *
     * @param payStatus 支付状态码
     * @return 状态标签
     */
    public static StatusLabel getPayStatusLabel(String payStatus) {
        if (payStatus == null) {
            return StatusLabel.info("未知");
        }
        return switch (payStatus) {
            case OrderConstants.PAY_STATUS_UNPAID -> StatusLabel.warning("未支付");
            case OrderConstants.PAY_STATUS_PAID -> StatusLabel.success("已支付");
            case OrderConstants.PAY_STATUS_PARTIAL_REFUND -> StatusLabel.warning("部分退款");
            case OrderConstants.PAY_STATUS_REFUND -> StatusLabel.danger("全额退款");
            default -> StatusLabel.info("未知");
        };
    }
    
    /**
     * 填充实体的状态标签
     *
     * @param entity         实体对象
     * @param statusGetter   状态获取函数
     * @param labelSetter    标签设置函数
     * @param colorSetter    颜色设置函数
     * @param statusType     状态类型
     * @param <T>            实体类型
     */
    public static <T> void fillStatusLabel(T entity,
                                           Function<T, Integer> statusGetter,
                                           BiConsumer<T, String> labelSetter,
                                           BiConsumer<T, String> colorSetter,
                                           String statusType) {
        if (entity == null) {
            return;
        }
        Integer status = statusGetter.apply(entity);
        StatusLabel label = getStatusLabel(statusType, status);
        labelSetter.accept(entity, label.getLabel());
        colorSetter.accept(entity, label.getColor());
    }
    
    /**
     * 注册自定义状态映射
     *
     * @param type    状态类型
     * @param mapping 状态映射
     */
    public static void registerStatusMapping(String type, Map<Integer, StatusLabel> mapping) {
        STATUS_MAPPING.put(type, mapping);
    }
}
