package com.qmth.teachcloud.common.enums;

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

/**
 * @Description: 选项编号枚举
 * @Author: CaoZixuan
 * @Date: 2024-04-02
 */
public enum OptionsEnum {
    A("A", 1),
    B("B", 2),
    C("C", 3),
    D("D", 4),
    E("E", 5),
    F("F", 6),
    G("G", 7),
    H("H", 8),
    I("I", 9),
    J("J", 10),
    K("K", 11),
    L("L", 12),
    M("M", 13),
    N("N", 14),
    O("O", 15),
    P("P", 16),
    Q("Q", 17),
    R("R", 18),
    S("S", 19),
    T("T", 20),
    U("U", 21),
    V("V", 22),
    W("W", 23),
    X("X", 24),
    Y("Y", 25),
    Z("Z", 26);

    private final String code;

    private final int index;

    OptionsEnum(String code, int index) {
        this.code = code;
        this.index = index;
    }

    public String getCode() {
        return code;
    }

    public int getIndex() {
        return index;
    }

    /**
     * 根据编号查询枚举
     *
     * @param code 编号
     * @return 枚举
     */
    public static OptionsEnum getByCode(String code) {
        OptionsEnum result = null;
        List<OptionsEnum> optionsEnumList = Arrays.stream(OptionsEnum.values()).filter(e -> e.getCode().equals(code))
                .collect(Collectors.toList());

        if (optionsEnumList.size() > 1) {
            throw ExceptionResultEnum.ERROR.exception("code:" + code + "的枚举异常");
        } else if (optionsEnumList.size() == 1) {
            result = optionsEnumList.get(0);
        }
        return result;
    }

    /**
     * 根据索引查询枚举
     *
     * @param index 索引
     * @return 枚举
     */
    public static String getCodeByIndex(int index) {
        String result = "";
        List<OptionsEnum> optionsEnumList = Arrays.stream(OptionsEnum.values()).filter(e -> index == e.getIndex())
                .collect(Collectors.toList());
        int size = optionsEnumList.size();
        if (size > 1) {
            throw ExceptionResultEnum.ERROR.exception("index:" + index + "的编号枚举异常");
        } else if (size == 1) {
            result = optionsEnumList.get(0).getCode();
        }
        return result;
    }
}
