package com.jxy.marketall.utils;

import com.github.pagehelper.util.StringUtil;
import com.jxy.common.core.exception.GlobalException;
import com.jxy.common.core.utils.StringUtils;
import com.jxy.marketall.enums.CoinType;
import com.jxy.marketall.enums.DerivativeType;
import com.jxy.marketall.enums.DistributionType;
import com.jxy.marketall.enums.MarketType;
import com.jxy.marketall.vo.*;

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

/**
 * @Description 请求Vo转其他Vo
 * @Date 2022/5/27 11:33
 * @Author 杜懿
 */
public class EntityDtoUtils {

    public static CoinVo toCoinVo(RequestVo requestVo){

        CoinVo coinVo = new CoinVo();

        coinVo.setCoin(requestVo.getRadius());
        coinVo.setStart(requestVo.getStart());
        coinVo.setEnd(requestVo.getEnd());

        return coinVo;
    }

    public static CoinPriceVo toCoinPriceVo(RequestVo requestVo){

        CoinPriceVo coinPriceVo = new CoinPriceVo();

        /** 从radius中取出参数 */
        if (StringUtils.isEmpty(requestVo.getRadius()) || requestVo.getRadius()==""){
            return coinPriceVo;
        }
        List<String> list = Stream.of(requestVo.getRadius().split(",")).map(radiu -> String.valueOf(radiu)).collect(Collectors.toList());

        String coin = null;
        String ohlc = null;
        for (int i=0; i<list.size(); i++){
            String entry = list.get(i).toUpperCase();
            if(EnumUtils.isInclude(CoinType.class,entry)){
                coin = entry;
            }
            if("OHLC".equals(entry)){
                ohlc = entry;
            }
        }

        /** ohlc查询必须正确传时间 */
        if(ohlc != null && (StringUtils.isEmpty(requestVo.getStart()) || StringUtils.isEmpty(requestVo.getEnd()))){
            throw new GlobalException("传递参数异常，查询与OHLCPrice对比的价格必须同时传递起止时间！");
        }

        coinPriceVo.setCoin(coin);
        coinPriceVo.setOhlc(ohlc);
        coinPriceVo.setStart(requestVo.getStart());
        coinPriceVo.setEnd(requestVo.getEnd());


        return coinPriceVo;
    }

    public static QueryVo toQueryVo(RequestVo requestVo){

        QueryVo queryVo = new QueryVo();

        queryVo.setStart(requestVo.getStart());
        queryVo.setEnd(requestVo.getEnd());

        return queryVo;
    }

    public static TypeVo toTypeVo(RequestVo requestVo){

        TypeVo typeVo = new TypeVo();

        typeVo.setType(requestVo.getRadius());
        typeVo.setStart(requestVo.getStart());
        typeVo.setEnd(requestVo.getEnd());

        return typeVo;

    }

    public static CoinByTypeVo toCoinByMarketTypeVo(RequestVo requestVo){

        CoinByTypeVo coinByTypeVo = new CoinByTypeVo();

        /** 从radius中取出参数 */
        List<String> list = Stream.of(requestVo.getRadius().split(",")).map(radiu -> String.valueOf(radiu)).collect(Collectors.toList());

        String type = null;
        String coin = null;
        for (int i=0; i<list.size(); i++){
            if(EnumUtils.isInclude(MarketType.class,list.get(i))){
                type = list.get(i);
            }
            if(EnumUtils.isInclude(CoinType.class,list.get(i))){
                coin = list.get(i);
            }
        }
        /** 赋值完之后查看是否有数据 如果还是为空 说明用户可能没传参 */
        if(type == null || coin == null){
            throw new GlobalException("type和coin都必须正常传值才能请求该接口！");
        }

        coinByTypeVo.setCoin(coin);
        coinByTypeVo.setType(type);
        coinByTypeVo.setStart(requestVo.getStart());
        coinByTypeVo.setEnd(requestVo.getEnd());

        return coinByTypeVo;

    }

    public static CoinByTypeVo toCoinByDistributionTypeVo(RequestVo requestVo){

        CoinByTypeVo coinByTypeVo = new CoinByTypeVo();

        /** 从radius中取出参数 */
        List<String> list = Stream.of(requestVo.getRadius().split(",")).map(radiu -> String.valueOf(radiu)).collect(Collectors.toList());

        String type = null;
        String coin = null;
        for (int i=0; i<list.size(); i++){
            if(EnumUtils.isInclude(DistributionType.class,list.get(i))){
                type = list.get(i);
            }
            if(EnumUtils.isInclude(CoinType.class,list.get(i))){
                coin = list.get(i);
            }
        }
        /** 赋值完之后查看是否有数据 如果还是为空 说明用户可能没传参 */
        if(type == null || coin == null){
            throw new GlobalException("type和coin都必须正常传值才能请求该接口！");
        }

        coinByTypeVo.setCoin(coin);
        coinByTypeVo.setType(type);
        coinByTypeVo.setStart(requestVo.getStart());
        coinByTypeVo.setEnd(requestVo.getEnd());

        return coinByTypeVo;

    }

    public static CoinByTypeVo toCoinByDerivativeTypeVo(RequestVo requestVo){

        CoinByTypeVo coinByTypeVo = new CoinByTypeVo();

        /** 从radius中取出参数 */
        List<String> list = Stream.of(requestVo.getRadius().split(",")).map(radiu -> String.valueOf(radiu)).collect(Collectors.toList());

        String type = null;
        String coin = null;
        for (int i=0; i<list.size(); i++){
            if(EnumUtils.isInclude(DerivativeType.class,list.get(i))){
                type = list.get(i);
            }
            if(EnumUtils.isInclude(CoinType.class,list.get(i))){
                coin = list.get(i);
            }
        }
        /** 赋值完之后查看是否有数据 如果还是为空 说明用户可能没传参 */
        if(type == null || coin == null){
            throw new GlobalException("type和coin都必须正常传值才能请求该接口！");
        }

        coinByTypeVo.setCoin(coin);
        coinByTypeVo.setType(type);

        return coinByTypeVo;

    }

}
