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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * v2版
 * 用于获取矿池实际报块一段时间内总数
 * 解决了无效区块数的问题
 */
@Component
public class DetailedBlockDataV2Strategy implements PoolBlockDataStrategy {

    @Override
    public BlockTimeAndNumberDto getTotalBlocks(String coin,Date start, Date end, List<BlockInfoDto> blockPerDays) {
        List<BlockInfoDto> collect = blockPerDays.stream()
                .filter(e -> (e.getDate().before(end)) || e.getDate().equals(end))
                .filter(e -> (e.getDate().after(start) || e.getDate().equals(start)))
                .collect(Collectors.toList());
        if (collect.isEmpty()){
            return BlockTimeAndNumberDto.builder().dateLength(BigDecimal.ZERO).height(0).build();
        }
        if (collect.size() == 1){
            return BlockTimeAndNumberDto.builder().dateLength(BigDecimal.ZERO).height(collect.get(0).getHeight()).build();
        }
        return BlockTimeAndNumberDto.builder()
                .height(collect.get(0).getHeight()-collect.get(collect.size() - 1).getHeight()).build();

    }
}
