
package com.ruoyi.course.util;


import com.ruoyi.course.enums.GradeEnum;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class GradeConverter {
    
    /**
     * 将枚举转换为显示名称（用于UI展示）
     */
    public String toDisplayName(GradeEnum grade) {
        return grade != null ? grade.getDisplayName() : "";
    }
    
    /**
     * 将枚举转换为数据库存储的code
     */
    public Integer toCode(GradeEnum grade) {
        return grade != null ? grade.getCode() : null;
    }
    
    /**
     * 将字符串转换为枚举
     */
    public GradeEnum fromDisplayName(String displayName) {
        if (displayName == null || displayName.trim().isEmpty()) {
            return null;
        }
        return GradeEnum.fromDisplayName(displayName.trim());
    }
    
    /**
     * 将code转换为枚举
     */
    public GradeEnum fromCode(Integer code) {
        if (code == null) {
            return null;
        }
        return GradeEnum.fromCode(code);
    }
    
    /**
     * 将逗号分隔的年级字符串转换为枚举列表
     */
    public List<GradeEnum> parseGradeLevel(String gradeLevel) {
        if (gradeLevel == null || gradeLevel.trim().isEmpty()) {
            return Collections.emptyList();
        }
        
        // 如果是"全部"
        if ("全部".equals(gradeLevel.trim())) {
            return Arrays.asList(GradeEnum.values());
        }
        
        List<GradeEnum> result = new ArrayList<>();
        String[] parts = gradeLevel.split(",");
        
        for (String part : parts) {
            GradeEnum grade = fromDisplayName(part.trim());
            if (grade != null) {
                result.add(grade);
            }
        }
        
        return result;
    }
    
    /**
     * 将逗号分隔的code字符串转换为枚举列表
     */
    public List<GradeEnum> parseGradeLevelFromCodes(String gradeCodes) {
        if (gradeCodes == null || gradeCodes.trim().isEmpty()) {
            return Collections.emptyList();
        }
        
        List<GradeEnum> result = new ArrayList<>();
        String[] parts = gradeCodes.split(",");
        
        for (String part : parts) {
            try {
                Integer code = Integer.parseInt(part.trim());
                GradeEnum grade = fromCode(code);
                if (grade != null) {
                    result.add(grade);
                }
            } catch (NumberFormatException e) {
                // 如果不是数字，尝试作为显示名称处理
                GradeEnum grade = fromDisplayName(part.trim());
                if (grade != null) {
                    result.add(grade);
                }
            }
        }
        
        return result;
    }
    
    /**
     * 将枚举列表转换为逗号分隔的显示名称字符串
     */
    public String toDisplayNameString(List<GradeEnum> grades) {
        if (grades == null || grades.isEmpty()) {
            return "";
        }
        
        return grades.stream()
                .map(this::toDisplayName)
                .collect(Collectors.joining(","));
    }
    
    /**
     * 将枚举列表转换为逗号分隔的code字符串
     */
    public String toCodeString(List<GradeEnum> grades) {
        if (grades == null || grades.isEmpty()) {
            return "";
        }
        
        return grades.stream()
                .map(GradeEnum::getCode)
                .map(String::valueOf)
                .collect(Collectors.joining(","));
    }
}