package com.izouma.nineth.service;

import com.alibaba.excel.EasyExcel;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.TokenHistory;
import com.izouma.nineth.domain.AirDrop;
import com.izouma.nineth.domain.Asset;
import com.izouma.nineth.domain.DropTarget;
import com.izouma.nineth.dto.AirDropExcelDTO;
import com.izouma.nineth.enums.AirDropType;
import com.izouma.nineth.enums.AssetStatus;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.AirDropRepo;
import com.izouma.nineth.repo.AssetRepo;
import com.izouma.nineth.repo.TokenHistoryRepo;
import com.izouma.nineth.utils.excel.UploadDataListener;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AirDropServiceTest extends ApplicationTests {
    @Autowired
    AirDropService    airDropService;
    @Autowired
    AirDropRepo       airDropRepo;
    @Autowired
    AssetRepo         assetRepo;
    @Autowired
    TokenHistoryRepo  tokenHistoryRepo;
    @Autowired
    CollectionService collectionService;

    @Test
    public void drop() {
        airDropService.drop(4235490L, 4273750L, 498,
                LocalDateTime.of(2022, 4, 13, 23, 01));
    }

    @Test
    public void excel() {
        File file = new File("/Users/qiufangchao/Desktop/RaexDrop.xlsx");
        UploadDataListener<AirDropExcelDTO> listener = new UploadDataListener<>();
        List<AirDropExcelDTO> dtos = EasyExcel.read(file, AirDropExcelDTO.class, listener)
                .sheet()
                .doReadSync();
//        System.out.println(dtos);

        dtos.forEach(dto -> {

        });
    }

    @Test
    public void cancel() {
        AirDrop airDrop = airDropRepo.findById(7607018L).orElseThrow(new BusinessException(""));
        List<Asset> resultA = new ArrayList<>();
        List<TokenHistory> resultT = new ArrayList<>();
        int total = 0;
        for (DropTarget target : airDrop.getTargets()) {
            List<Asset> assets = assetRepo.findAllByUserIdAndCollectionIdAndStatus(target.getUserId(), airDrop.getCollectionId(), AssetStatus.NORMAL);
            assets.sort((a, b) -> b.getId().compareTo(a.getId()));
            if (target.getNum() > assets.size()) {
                throw new BusinessException(target.getUserId() + ":" + target.getNum() + ":" + assets.size());
            }
            for (int i = 0; i < target.getNum(); i++) {
                Asset asset = assets.get(i);
                resultA.add(asset);
                TokenHistory tokens = tokenHistoryRepo.findByToUserIdAndTokenId(target.getUserId(), asset.getTokenId())
                        .orElseThrow(new BusinessException(""));
                resultT.add(tokens);
                total++;
            }

        }

//        System.out.println(total);
//        System.out.println(airDrop.getTargets().stream().mapToInt(DropTarget::getNum).sum());
//
//        assetRepo.deleteAll(resultA);
//        tokenHistoryRepo.deleteAll(resultT);
//
//        collectionService.increaseStock(airDrop.getCollectionId(), total);
//        collectionService.decreaseSale(airDrop.getCollectionId(), total);
    }

    @Test
    public void create() {
        DropTarget target = new DropTarget(9972L, "18205083565", "Fairy Li", 1);
        AirDrop drop = AirDrop.builder()
                .auto(true)
                .collectionId(8024802L)
                .name("测试自动空投")
                .userIds(Collections.singletonList(9972L))
                .targets(Collections.singletonList(target))
                .type(AirDropType.asset)
                .build();

        airDropService.create(drop);
    }

}