package com.liquidnet.service.galaxy.router.strategy;

import com.liquidnet.common.exception.LiquidnetServiceException;
import com.liquidnet.commons.lang.util.StringUtil;
import com.liquidnet.service.galaxy.constant.GalaxyErrorEnum;
import com.liquidnet.service.galaxy.router.strategy.config.GalaxyConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AnJiabin <anjiabin@zhengzai.tv>
 * @version V1.0
 * @Description: TODO
 * @class: GalaxyRouterStrategyContext
 * @Package com.liquidnet.service.galaxy.router.strategy
 * @Copyright: LightNet @ Copyright (c) 2021
 * @date 2022/3/8 11:26
 */
@Component
public class GalaxyRouterStrategyContext {
    @Autowired
    private GalaxyConfig galaxyConfig;

    private final Map<String, IGalaxyRouterStrategyArtwork> artworkHandlerMap = new HashMap<>();
    private final Map<String, IGalaxyRouterStrategyUser> userHandlerMap = new HashMap<>();
    private final Map<String, IGalaxyRouterStrategyPublish> publishHandlerMap = new HashMap<>();
    private final Map<String, IGalaxyRouterStrategyTrade> tradeHandlerMap = new HashMap<>();
    private final Map<String, IGalaxyRouterStrategyTradeQuery> tradeQueryHandlerMap = new HashMap<>();

    public IGalaxyRouterStrategyArtwork getArtworkStrategy(String type) {
        if(!this.isActived(type)){
            throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        }
        IGalaxyRouterStrategyArtwork obj = artworkHandlerMap.get(type);
        if(StringUtil.isNull(obj)) throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        return obj;
    }

    public void putArtworkStrategy(String code, IGalaxyRouterStrategyArtwork strategy) {
        artworkHandlerMap.put(code, strategy);
    }

    public IGalaxyRouterStrategyUser getUserStrategy(String type) {
        if(!this.isActived(type)){
            throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        }
        IGalaxyRouterStrategyUser obj = userHandlerMap.get(type);
        if(StringUtil.isNull(obj)) throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        return obj;
    }

    public void putUserStrategy(String code, IGalaxyRouterStrategyUser strategy) {
        userHandlerMap.put(code, strategy);
    }

    public IGalaxyRouterStrategyPublish getPublishStrategy(String type) {
        if(!this.isActived(type)){
            throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        }
        IGalaxyRouterStrategyPublish obj = publishHandlerMap.get(type);
        if(StringUtil.isNull(obj)) throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        return obj;
    }

    public void putPublishStrategy(String code, IGalaxyRouterStrategyPublish strategy) {
        publishHandlerMap.put(code, strategy);
    }

    public IGalaxyRouterStrategyTrade getTradeStrategy(String type) {
        if(!this.isActived(type)){
            throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        }
        IGalaxyRouterStrategyTrade obj = tradeHandlerMap.get(type);
        if(StringUtil.isNull(obj)) throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        return obj;
    }

    public void putTradeStrategy(String code, IGalaxyRouterStrategyTrade strategy) {
        tradeHandlerMap.put(code, strategy);
    }

    public IGalaxyRouterStrategyTradeQuery getTradeQueryStrategy(String type) {
        if(!this.isActived(type)){
            throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        }
        IGalaxyRouterStrategyTradeQuery obj = tradeQueryHandlerMap.get(type);
        if(StringUtil.isNull(obj)) throw new LiquidnetServiceException(GalaxyErrorEnum.ROUTER_NOT_EXIST.getCode(), GalaxyErrorEnum.ROUTER_NOT_EXIST.getMessage());
        return obj;
    }

    public void putTradeQueryStrategy(String code, IGalaxyRouterStrategyTradeQuery strategy) {
        tradeQueryHandlerMap.put(code, strategy);
    }

    private boolean isActived(String routerType){
        List<String> routerList = galaxyConfig.getRouterList();
        boolean rs = false;
        if(StringUtil.isNotNull(routerList)){
            for(int i=0;i<routerList.size();i++){
                if(routerType.equalsIgnoreCase(routerList.get(i))){
                    rs = true;
                }
            }
        }
        return rs;
    }
}
