package com.smppw.utils;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.NumberUtil;
import com.smppw.common.pojo.IStrategy;
import com.smppw.common.pojo.NewPublicSubStrategy;
import com.smppw.common.pojo.NewSubStrategy;
import com.smppw.common.pojo.PublicSubStrategyFullName;
import com.smppw.common.pojo.enums.strategy.Strategy;
import com.smppw.common.pojo.enums.strategy.ThirdStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author Rain
 * @date 2022/12/16 10:17
 * @description
 */
public class StrategyHandleUtils {

    private static final List<IStrategy> lists = ListUtil.list(false);

    private static final List<IStrategy> PUBLIC_SUB_STRATEGY_LIST = ListUtil.list(false);

    static {
        lists.addAll(ListUtil.toList(Strategy.values()));
        lists.addAll(ListUtil.toList(NewSubStrategy.values()));
        lists.addAll(ListUtil.toList(NewPublicSubStrategy.values()));
        lists.addAll(ListUtil.toList(ThirdStrategy.values()));

        PUBLIC_SUB_STRATEGY_LIST.addAll(ListUtil.toList(PublicSubStrategyFullName.values()));
    }

    public static IStrategy getStrategyByOriginName(String name) {
        for (IStrategy item : lists) {
            if (StringUtils.isNotEmpty(name)) {
                if (name.equalsIgnoreCase(item.getStrategyOriginName())) {
                    return item;
                }
            }
        }
        return null;
    }

    public static IStrategy getStrategyById(Integer id) {
        for (IStrategy item : lists) {
            if (id != null) {
                if (id.equals(item.getStrategyId())) {
                    return item;
                }
            }
        }
        return null;
    }

    public static IStrategy getPublicSubStrategyById(Integer id) {
        for (IStrategy item : PUBLIC_SUB_STRATEGY_LIST) {
            if (id != null) {
                if (id.equals(item.getStrategyId())) {
                    return item;
                }
            }
        }
        return null;
    }

    public static IStrategy getStrategy(String strategyKey) {
        IStrategy strategy;
        if (NumberUtil.isNumber(strategyKey)) {
            strategy = StrategyHandleUtils.getStrategyById(Integer.parseInt(strategyKey));
        } else {
            strategy = StrategyHandleUtils.getStrategyByOriginName(strategyKey);
        }
        if (strategy == null) {
            strategy = Strategy.All;
        }
        return strategy;
    }
}
