package com.app.constant;

import com.app.dto.basics.GradeClassDto;

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

public class SchoolConstant {
    public static List<String> getPrimarySchool() {
        return PRIMARY_SCHOOL;
    }

    public static List<String> getMiddleSchool() {
        return MIDDLE_SCHOOL;
    }

    public static List<String> getHighSchool() {
        return HIGH_SCHOOL;
    }

    /* 统一所有命名格式 */
    private static final List<String> GRADE_NAME = new ArrayList<>();
    private static final List<String> PRIMARY_SCHOOL = Stream.of("一年级", "二年级", "三年级", "四年级", "五年级", "六年级").collect(Collectors.toList()); // 小学
    private static final List<String> MIDDLE_SCHOOL = Stream.of("初一", "初二", "初三").collect(Collectors.toList()); // 初中
    private static final List<String> HIGH_SCHOOL = Stream.of("高一", "高二", "高三").collect(Collectors.toList()); // 高中
    private static final List<String> CLASS_NAME_LIST = Stream.of("1班", "2班", "3班", "4班", "5班", "6班", "7班", "8班", "9班", "10班").collect(Collectors.toList()); // 班级

    private static final List<String> GRADE_NUMBER_NAME = new ArrayList<>();
    private static final List<String> PRIMARY_NUMBER_SCHOOL = Stream.of("1", "2", "3", "4", "5", "6").collect(Collectors.toList()); // 小学
    private static final List<String> MIDDLE_NUMBER_SCHOOL = Stream.of("7", "8", "9").collect(Collectors.toList()); // 初中
    private static final List<String> HIGH_NUMBER_SCHOOL = Stream.of("10", "11", "12").collect(Collectors.toList()); // 高中
    private static final List<String> CLASS_NAME_NUMBER_LIST = Stream.of("01", "02", "03", "04", "05", "06", "07", "08", "09", "10").collect(Collectors.toList()); // 班级

    static {
        GRADE_NAME.addAll(PRIMARY_SCHOOL);
        GRADE_NAME.addAll(MIDDLE_SCHOOL);
        GRADE_NAME.addAll(HIGH_SCHOOL);

        GRADE_NUMBER_NAME.addAll(PRIMARY_NUMBER_SCHOOL);
        GRADE_NUMBER_NAME.addAll(MIDDLE_NUMBER_SCHOOL);
        GRADE_NUMBER_NAME.addAll(HIGH_NUMBER_SCHOOL);
    }

    public static String formatGradeName(Integer gradeName) {
        if (gradeName <= 6) {
            return PRIMARY_SCHOOL.get(gradeName - 1);
        } else if (gradeName <= 9) {
            return MIDDLE_SCHOOL.get(gradeName - 1);
        } else if (gradeName <= 12) {
            return HIGH_SCHOOL.get(gradeName - 1);
        }

        return String.valueOf(gradeName);
    }

    public static String formatClassName(Integer glassName) {
        return String.format("%s班", glassName);
    }

    /**
     * 功能描述：格式化年级班级名称
     * 一年级1班 -> 101
     * 初一10班 -> 701
     * 高一10班 -> 1010
     *
     * @param gradeName
     * @param className
     * @return
     */
    public static String formatSchoolClass(String gradeName, String className) {
        String _gradeName = "";
        if (PRIMARY_SCHOOL.indexOf(gradeName) >= 0) {
            _gradeName = String.format("%s", PRIMARY_SCHOOL.indexOf(gradeName) + 1);
        } else if (MIDDLE_SCHOOL.indexOf(gradeName) >= 0) {
            _gradeName = String.format("%s", PRIMARY_SCHOOL.indexOf(gradeName) + 7);
        } else if (HIGH_SCHOOL.indexOf(gradeName) >= 0) {
            _gradeName = String.format("%s", PRIMARY_SCHOOL.indexOf(gradeName) + 10);
        }

        // 0 代表前面补充0
        // 2 代表长度为2
        // d 代表参数为正数型
        String _className = String.format("%02d", CLASS_NAME_LIST.indexOf(className) + 1);

        return String.format("%s%s", _gradeName, _className);
    }

    /**
     * 功能描述：格式化年级班级名称
     * 一年级1班 -> 101
     * 初一10班 -> 701
     * 高一10班 -> 1010
     *
     * @param gradeName
     * @param className
     * @return
     */
    public static String formatSchoolClass(Integer gradeName, Integer className) {
        String _gradeName = "";
        if (PRIMARY_SCHOOL.indexOf(gradeName) >= 0) {
            _gradeName = String.format("%s", PRIMARY_SCHOOL.indexOf(gradeName) + 1);
        } else if (MIDDLE_SCHOOL.indexOf(gradeName) >= 0) {
            _gradeName = String.format("%s", PRIMARY_SCHOOL.indexOf(gradeName) + 7);
        } else if (HIGH_SCHOOL.indexOf(gradeName) >= 0) {
            _gradeName = String.format("%s", PRIMARY_SCHOOL.indexOf(gradeName) + 10);
        }

        // 0 代表前面补充0
        // 2 代表长度为2
        // d 代表参数为正数型
        String _className = String.format("%02d", CLASS_NAME_LIST.indexOf(className) + 1);

        return String.format("%s%s", _gradeName, _className);
    }

    /**
     * 功能描述：根据学校的年级类型获取年级班对象
     *
     * @param gradeYear
     * @return
     */
    public static List<GradeClassDto> getSchoolClass4GradeYear(String gradeYear) {
        /* 根据学校年级定义初始化学校年级班级对象 */
        List<GradeClassDto> list = new ArrayList<>();
        if (gradeYear.indexOf("1") >= 0) {
            list.addAll(SchoolConstant.getSchoolClass(1));
        }
        if (gradeYear.indexOf("2") >= 0) {
            list.addAll(SchoolConstant.getSchoolClass(2));
        }
        if (gradeYear.indexOf("3") >= 0) {
            list.addAll(SchoolConstant.getSchoolClass(3));
        }

        return list;
    }

    /**
     * 功能描述：根据学校年级类型获取年级班对象
     *
     * @param grade 1：小学、2：中学、3：高中
     * @return
     */
    public static List<GradeClassDto> getSchoolClass(Integer grade) {
        List<GradeClassDto> list = new ArrayList<>();
        switch (grade) {
            case 1:
                for (String gradeName : PRIMARY_SCHOOL) {
                    for (String className : CLASS_NAME_LIST) {
                        list.add(new GradeClassDto(gradeName, className));
                    }
                }
                break;
            case 2:
                for (String gradeName : MIDDLE_SCHOOL) {
                    for (String className : CLASS_NAME_LIST) {
                        list.add(new GradeClassDto(gradeName, className));
                    }
                }
                break;
            case 3:
                for (String gradeName : HIGH_SCHOOL) {
                    for (String className : CLASS_NAME_LIST) {
                        list.add(new GradeClassDto(gradeName, className));
                    }
                }
                break;
            default:
        }

        return list;
    }

    /**
     * 功能描述：根据学校年级类型获取毕业年级名称
     * 例如：
     * 1：小学 -> 六年级
     * 2：中学 -> 初三
     * 3：高中 -> 高三
     *
     * @param gradeYear 1：小学、2：中学、3：高中
     * @return
     */
    public static String getSchoolByIsGraduate(String gradeYear) {
        switch (gradeYear) {
            case "1":
                return PRIMARY_SCHOOL.get(PRIMARY_SCHOOL.size() - 1);
            case "2":
                return MIDDLE_SCHOOL.get(MIDDLE_SCHOOL.size() - 1);
            case "3":
                return HIGH_SCHOOL.get(HIGH_SCHOOL.size() - 1);
            default:
                return "";
        }
    }

    /**
     * 功能描述：根据学校年级类型获取待升年级的年级名称
     *
     * @param gradeYear 1：小学、2：中学、3：高中
     * @return
     */
    public static List<String> getSchoolByUpLevel(String gradeYear) {
        switch (gradeYear) {
            case "1":
                return PRIMARY_SCHOOL.subList(0, PRIMARY_SCHOOL.size() - 1);
            case "2":
                return MIDDLE_SCHOOL.subList(0, MIDDLE_SCHOOL.size() - 1);
            case "3":
                return HIGH_SCHOOL.subList(0, HIGH_SCHOOL.size() - 1);
            default:
                return Collections.EMPTY_LIST;
        }
    }

    /**
     * 功能描述：根据学校年级类型获取下一年级名称
     *
     * @param gradeYear 1：小学、2：中学、3：高中
     * @param gradeName 当前年级名称
     * @return
     */
    public static String getSchoolNextGradeName(String gradeYear, String gradeName) {
        switch (gradeYear) {
            case "1":
                return PRIMARY_SCHOOL.get(PRIMARY_SCHOOL.indexOf(gradeName) + 1);
            case "2":
                return MIDDLE_SCHOOL.get(MIDDLE_SCHOOL.indexOf(gradeName) + 1);
            case "3":
                return HIGH_SCHOOL.get(HIGH_SCHOOL.indexOf(gradeName) + 1);
            default:
                return "";
        }
    }

    /**
     * 功能描述：根据学校的年级类型获取年级班对象
     *
     * @param gradeYear
     * @return
     */
    public static List<GradeClassDto> getSchoolClassNumber4GradeYear(String gradeYear) {
        /* 根据学校年级定义初始化学校年级班级对象 */
        List<GradeClassDto> list = new ArrayList<>();
        if (gradeYear.indexOf("1") >= 0) {
            list.addAll(SchoolConstant.getSchoolClassNumber(1));
        }
        if (gradeYear.indexOf("2") >= 0) {
            list.addAll(SchoolConstant.getSchoolClassNumber(2));
        }
        if (gradeYear.indexOf("3") >= 0) {
            list.addAll(SchoolConstant.getSchoolClassNumber(3));
        }

        return list;
    }

    /**
     * 功能描述：根据学校年级类型获取年级班对象
     *
     * @param grade 1：小学、2：中学、3：高中
     * @return
     */
    public static List<GradeClassDto> getSchoolClassNumber(Integer grade) {
        List<GradeClassDto> list = new ArrayList<>();
        switch (grade) {
            case 1:
                for (String gradeName : PRIMARY_NUMBER_SCHOOL) {
                    for (String className : CLASS_NAME_NUMBER_LIST) {
                        list.add(new GradeClassDto(gradeName, className));
                    }
                }
                break;
            case 2:
                for (String gradeName : MIDDLE_NUMBER_SCHOOL) {
                    for (String className : CLASS_NAME_NUMBER_LIST) {
                        list.add(new GradeClassDto(gradeName, className));
                    }
                }
                break;
            case 3:
                for (String gradeName : HIGH_NUMBER_SCHOOL) {
                    for (String className : CLASS_NAME_NUMBER_LIST) {
                        list.add(new GradeClassDto(gradeName, className));
                    }
                }
                break;
            default:
        }

        return list;
    }

}
