package com.ym.mec.biz.dal.enums;

import org.apache.commons.lang3.StringUtils;

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

/**
 * 优惠券明细类型
 *
 * @author hgw
 * Created by 2021-12-29
 */
public enum CouponDetailTypeEnum {

    //商品-其他类型优惠券
    OTHER("OTHER", "其他"),
    //乐器
    MUSICAL("MUSICAL", "乐器"),
    //乐保
    MAINTENANCE("MAINTENANCE", "乐保服务"),
    //辅件
    ACCESSORIES("ACCESSORIES", "辅件"),
    //教材
    TEACHING("TEACHING", "教谱"),
    //网管课
    PRACTICE("PRACTICE", "网管课"),
    //声部课 = 单技课 SINGLE、集训单技课 TRAINING_SINGLE
    SINGLE("SINGLE", "声部课"),
    //合奏课 = 合奏课 MIX、集训合奏课 TRAINING_MIX ,
    MIX("MIX", "合奏课"),
    //VIP = vip课 VIP +乐理课 THEORY_COURSE
    VIP("VIP", "VIP"),
    //会员开通，团练宝   CLOUD_TEACHER  CLOUD_TEACHER_PLUS
    MEMBER("MEMBER", "云教练"),
    //全类都可以使用
    FULLCOUPON("FULLCOUPON", "全类券");

    private String code;

    private String msg;

    CouponDetailTypeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    //根据传入code找到对应的券类型，如果不存在则返回 全类型
    public static String of(String code) {
        String check = check(code);
        return StringUtils.isBlank(check) ? FULLCOUPON.getCode() : check;
    }

    //如果不存在则返回 null
    public static String ofNull(String code) {
        return check(code);
    }

    private static String check(String code) {
        CouponDetailTypeEnum detailTypeEnum = Arrays.stream(CouponDetailTypeEnum.values())
                .filter(e -> e.code.equalsIgnoreCase(code))
                .findFirst()
                .orElse(null);
        if (Objects.nonNull(detailTypeEnum)) {
            return detailTypeEnum.getCode();
        }
        switch (code) {
            case "INSTRUMENT":
                return MUSICAL.getCode();
            case "TRAINING_SINGLE":
                return SINGLE.getCode();
            case "THEORY_COURSE":
                return VIP.getCode();
            case "TRAINING_MIX":
                return MIX.getCode();
            case "CLOUD_TEACHER":
            case "CLOUD_TEACHER_PLUS":
                return MEMBER.getCode();
            default:
                return null;
        }
    }

    public static String[] getAllowType(CouponDetailTypeEnum... type) {
        List<String> resultList = new ArrayList<>();
        resultList.add(FULLCOUPON.getCode());
        if (Objects.nonNull(type)) {
            List<String> collect = Arrays.stream(type)
                    .map(CouponDetailTypeEnum::getCode)
                    .distinct()
                    .collect(Collectors.toList());
            resultList.addAll(collect);
        }
        return resultList.toArray(new String[0]);
    }

}
