package com.m2pool.pool.strategy;

import com.m2pool.pool.dto.BlockInfoDto;
import com.m2pool.pool.dto.BlockTimeAndNumberDto;
import com.m2pool.pool.mapper.PoolMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import static com.m2pool.pool.enums.PoolCalParamConfig.getCoinCount;

/**
 * 旧版
 * 用于获取矿池实际报块一段时间内总数
 * 可能存在无效区块
 */
@Component
public class DetailedBlockDataV1Strategy implements PoolBlockDataStrategy {

    @Resource
    private PoolMapper poolMapper;


    @Override
    public BlockTimeAndNumberDto getTotalBlocks(String coin,Date start, Date end,List<BlockInfoDto> blockPerDays ) {

        if (blockPerDays.isEmpty()){
            return BlockTimeAndNumberDto.builder().dateLength(BigDecimal.ZERO).height(0).build();
        }
        //每日出块数固定的出块数
        BigDecimal nexaCount = getCoinCount(coin);
        long diffInMillis = end.getTime() - start.getTime();
        BigDecimal dateLength2 = BigDecimal.valueOf(diffInMillis).divide(BigDecimal.valueOf(86400000), 2, RoundingMode.HALF_UP);
        //获取时间段内全网的实际报块数
        int height = dateLength2.multiply(nexaCount).intValue();
        //天数不足时，以实际天数计算
        //Date poolStart = blockPerDays.get(blockPerDays.size() - 1).getDate();
        //if(poolStart.after(start)){
        //  dateLength2 = dateLength2
        //          .subtract(BigDecimal.valueOf(poolStart.getTime() - start.getTime())
        //          .divide(BigDecimal.valueOf(86400000), 2, RoundingMode.HALF_UP));
        //  height = dateLength2.multiply(nexaCount).intValue();
        //}
        return BlockTimeAndNumberDto.builder().dateLength(dateLength2).height(height).build();
    }
}
