package com.qaup.collision.common.model.enums;

import com.qaup.collision.common.model.MovingObject.MovingObjectType;

/**
 * 车辆类型转换工具类
 * 负责车辆类型枚举和编码之间的转换
 * 
 * @author Claude Code
 * @version 1.0
 */
public class VehicleTypeConverter {
    
    /**
     * 将VehicleTypeCode转换为ObjectType
     */
    public static MovingObjectType toObjectType(VehicleTypeCode vehicleTypeCode) {
        if (vehicleTypeCode == null) {
            return MovingObjectType.NORMAL_VEHICLE;
        }
        
        switch (vehicleTypeCode.getTopLevelType()) {
            case UV:
                return MovingObjectType.UNMANNED_VEHICLE;
            case SP:
                return MovingObjectType.SPECIAL_VEHICLE;
            case NM:
                return MovingObjectType.NORMAL_VEHICLE;
            default:
                return MovingObjectType.NORMAL_VEHICLE;
        }
    }
    
    /**
     * 从ObjectType转换为VehicleTypeCode（一级分类）
     */
    public static VehicleTypeCode fromObjectType(MovingObjectType objectType) {
        if (objectType == null) {
            return VehicleTypeCode.NM;
        }
        
        switch (objectType) {
            case UNMANNED_VEHICLE:
                return VehicleTypeCode.UV;
            case SPECIAL_VEHICLE:
                return VehicleTypeCode.SP;
            case NORMAL_VEHICLE:
                return VehicleTypeCode.NM;
            case AIRCRAFT:
                return VehicleTypeCode.NM;  // 航空器默认映射为普通车（虽然不会用到）
            default:
                return VehicleTypeCode.NM;
        }
    }
    
    /**
     * 从路径编码字符串转换为ObjectType
     */
    public static MovingObjectType fromTypeCode(String typeCode) {
        if (typeCode == null || typeCode.trim().isEmpty()) {
            return MovingObjectType.NORMAL_VEHICLE;
        }
        
        // 直接根据编码前缀判断
        if (typeCode.startsWith("UV")) {
            return MovingObjectType.UNMANNED_VEHICLE;
        } else if (typeCode.startsWith("SP")) {
            return MovingObjectType.SPECIAL_VEHICLE;
        } else if (typeCode.startsWith("AC")) {
            return MovingObjectType.AIRCRAFT;
        } else {
            return MovingObjectType.NORMAL_VEHICLE;
        }
    }
    
    /**
     * 从数据库类型ID转换（向后兼容）
     * 这个方法需要和QuapDataAdapter配合使用
     */
    public static MovingObjectType fromLegacyTypeId(Long typeId, String typeName) {
        // 基于类型名称进行简单分类
        if (typeName == null || typeName.trim().isEmpty()) {
            return MovingObjectType.NORMAL_VEHICLE;
        }
        
        if (typeName.contains("无人车")) {
            return MovingObjectType.UNMANNED_VEHICLE;
        } else if (typeName.contains("特勤") || typeName.contains("消防") || 
                   typeName.contains("警车") || typeName.contains("救护") || 
                   typeName.contains("引导")) {
            return MovingObjectType.SPECIAL_VEHICLE;
        } else if (typeName.contains("航空") || typeName.contains("飞机")) {
            return MovingObjectType.AIRCRAFT;
        } else {
            // 所有其他类型都归类为普通车辆
            return MovingObjectType.NORMAL_VEHICLE;
        }
    }
}