package com.sonic.lion.domain.enums;

import com.google.common.collect.Lists;
import com.sonic.common.utils.LocaleUtils;
import com.sonic.common.utils.MessageUtils;
import com.sonic.lion.enums.BizType;
import lombok.Getter;

import java.util.List;


@Getter
public enum BuffClassifyEnum {

    //充值 biz_type = 600 AND in_or_out = 1
    B_CHARGE(BuffAmountTypeEnum.BALANCE, InOrOut.IN, "BUFF_BALANCE_TYPE_CHARGE", Lists.newArrayList(
            BizType.CHARGE
    )),

    //游戏购买，订单赠送 biz_type IN (100, 201) AND in_or_out = 2
    B_PURCHASE(BuffAmountTypeEnum.BALANCE, InOrOut.OUT, "BUFF_BALANCE_TYPE_PURCHASE", Lists.newArrayList(

    )),

    //游戏退款，商家订阅退款，赠送订单退款 biz_type IN (217,400,401, 402) AND in_or_out = 1
    B_REFUND(BuffAmountTypeEnum.BALANCE, InOrOut.IN, "BUFF_BALANCE_TYPE_REFUND", Lists.newArrayList(
            BizType.REFUND
    )),

    //biz_type IN (101, 203, 801, 802, 803, 902) AND in_or_out = 2
    B_TIP(BuffAmountTypeEnum.BALANCE, InOrOut.OUT, "BUFF_BALANCE_TYPE_TIP", Lists.newArrayList(

    )),

    //biz_type IN (219, 800, 901, 225) AND in_or_out = 2
    B_GIFT(BuffAmountTypeEnum.BALANCE, InOrOut.OUT, "BUFF_BALANCE_TYPE_GIFT", Lists.newArrayList(

    )),

    //商户订阅 biz_type = 216 AND in_or_out = 2
    B_SONIC_SUBSCRIPTION(BuffAmountTypeEnum.BALANCE, InOrOut.OUT, "BUFF_BALANCE_TYPE_SONIC_SUBSCRIPTION", Lists.newArrayList(

    )),

    //抽獎 biz_type = 704 AND in_or_out = 2
    B_LUCKY_DRAW(BuffAmountTypeEnum.BALANCE, InOrOut.OUT, "BUFF_BALANCE_TYPE_LUCKY_DRAW", Lists.newArrayList(

    )),

    //商城商品购买 biz_type = 224 AND in_or_out = 2
    B_STORE(BuffAmountTypeEnum.BALANCE, InOrOut.OUT, "BUFF_BALANCE_TYPE_STORE", Lists.newArrayList(

    )),

    //biz_type IN (200, 206, 207, 209, 212, 213, 215) AND in_or_out = 1
    B_SONIC_PRIZE(BuffAmountTypeEnum.BALANCE, InOrOut.IN, "BUFF_BALANCE_TYPE_SONIC_PRIZE", Lists.newArrayList(

    )),

    //biz_type IN (204, 205, 210, 211,   700, 701, 702, 703)
    B_OTHER(BuffAmountTypeEnum.BALANCE, null, "BUFF_BALANCE_TYPE_OTHER", Lists.newArrayList(
            BizType.FROZEN_BALANCE,
            BizType.UNFROZEN_BALANCE
    )),


    //biz_type = 500
    I_WITHDRAW(BuffAmountTypeEnum.INCOME, null, "BUFF_INCOME_TYPE_WITHDRAW", Lists.newArrayList(
            BizType.WITHDRAW
    )),

    //biz_type in (100,201) AND in_or_out = 1
    I_SERVICE_INCOME(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_TYPE_SERVICE_INCOME", Lists.newArrayList(


    )),

    //biz_type IN (400, 401, 402) AND in_or_out = 2
    I_REFUND(BuffAmountTypeEnum.INCOME, InOrOut.OUT, "BUFF_INCOME_TYPE_REFUND", Lists.newArrayList(
            BizType.REFUND
    )),

    //biz_type IN (101, 203, 801, 802, 803, 902) AND in_or_out = 1
    I_TIP_INCOME(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_TYPE_TIP_INCOME", Lists.newArrayList(

    )),

    //biz_type IN (219, 800, 901) AND in_or_out = 1
    I_GIFT_INCOME(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_TYPE_GIFT_INCOME", Lists.newArrayList(

    )),

    //biz_type = 216 AND in_or_out = 1
    I_SUBSCRIPTION_INCOME(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_TYPE_SUBSCRIPTION_INCOME", Lists.newArrayList(

    )),

    //biz_type IN (202, 218) AND in_or_out = 1
    I_SONIC_REWARD(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_TYPE_SONIC_REWARD", Lists.newArrayList(

    )),

    //biz_type IN (208, 214) AND in_or_out = 1
    I_STORE_REWARD(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_TYPE_STORE_REWARD", Lists.newArrayList(

    )),
    //biz_type IN (601, 220) AND in_or_out = 1
    I_OTHER(BuffAmountTypeEnum.INCOME, InOrOut.IN, "BUFF_INCOME_OTHER_INCOME", Lists.newArrayList(

    )),

    ;

    /**
     * 所属大类 是Banlanc 或Income
     */
    private BuffAmountTypeEnum type;

    private String desc;

    /**
     * 所属业务类型
     */
    private List<BizType> bizTypeList;

    /**
     * 收入或支出
     */
    private InOrOut inOrOut;

    BuffClassifyEnum(BuffAmountTypeEnum type, InOrOut inOrOut, String desc, List<BizType> bizTypeList) {
        this.type = type;
        this.desc = desc;
        this.bizTypeList = bizTypeList;
        this.inOrOut = inOrOut;
    }

    public String getDesc() {
        return MessageUtils.getMessage(desc, LocaleUtils.getLocale());
    }

    /**
     * 获取对应金额类型下的归类
     *
     * @param type
     * @return
     */
    public static List<BuffClassifyEnum> getBuffClassifyList(BuffAmountTypeEnum type) {
        List<BuffClassifyEnum> arrayList = Lists.newArrayList();
        for (BuffClassifyEnum value : BuffClassifyEnum.values()) {
            if (value.type.equals(type)) {
                arrayList.add(value);
            }
        }
        return arrayList;
    }

    /**
     * 获取流水所属归类
     *
     * @param bizType
     * @param inOrOut
     * @return
     */
    public static BuffClassifyEnum getBuffClassify(BizType bizType, InOrOut inOrOut) {
        for (BuffClassifyEnum value : BuffClassifyEnum.values()) {
            List<BizType> bizTypeList = value.getBizTypeList();
            if (bizTypeList.contains(bizType) && value.getInOrOut() != null && value.getInOrOut().equals(inOrOut)) {
                return value;
            }
            if (value.equals(B_OTHER) && bizTypeList.contains(bizType)) {
                return value;
            }
            if (bizType.equals(BizType.WITHDRAW) && bizTypeList.contains(BizType.WITHDRAW)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 所有的收入类型列表（不包括打赏的类型）
     */
    private static final List<BuffClassifyEnum> allIncomeBuffClassifyList = Lists.newArrayList(I_SERVICE_INCOME, I_REFUND, I_TIP_INCOME, I_SUBSCRIPTION_INCOME, I_SONIC_REWARD, I_STORE_REWARD, I_OTHER);

    /**
     * 获取所有的营收业务类型（包括退款）
     * @return
     */
    public static List<BizType> getAllIncomeBizType() {
        List<BizType> bizTypeList = Lists.newArrayList();
        for (BuffClassifyEnum buffClassifyEnum : allIncomeBuffClassifyList) {
            bizTypeList.addAll(buffClassifyEnum.getBizTypeList());
        }
        return bizTypeList;
    }

}
