package com.izouma.nineth.service;

import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.TokenHistory;
import com.izouma.nineth.domain.*;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.*;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

class AssetServiceTest extends ApplicationTests {
    @Autowired
    private OrderRepo         orderRepo;
    @Autowired
    private BlindBoxItemRepo  blindBoxItemRepo;
    @Autowired
    private AssetService      assetService;
    @Autowired
    private CollectionRepo    collectionRepo;
    @Autowired
    private UserRepo          userRepo;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private AssetMintService  assetMintService;
    @Autowired
    private AssetRepo         assetRepo;
    @Autowired
    private NFTService        nftService;
    @Autowired
    private TokenHistoryRepo  tokenHistoryRepo;

    @Test
    void createAsset() {
        BlindBoxItem item = blindBoxItemRepo.findById(1860L).get();
        Order order = orderRepo.findById(1922L).get();
    }

    @Test
    void cancelConsignment() {
        assetService.cancelConsignment(1209L);
    }

    @Test
    void testCancelConsignment() {
    }

    @Test
    public void testCreateAsset() {
        Order order = orderRepo.findById(4618L).get();
        Collection collection = collectionRepo.findById(order.getCollectionId()).get();
        BlindBoxItem blindBoxItem = blindBoxItemRepo.findById(4250L).get();
        User user = userRepo.findById(order.getUserId()).get();
        assetService.createAsset(blindBoxItem, user, order.getId(), order.getPrice(), "出售", 1);
    }

    @Test
    public void mint() {
        Asset asset = assetRepo.findById(4622L).get();
        assetMintService.mint(asset.getId());
    }

    @Test
    public void setHistory() {
        assetService.setHistory();
    }

    @Test
    public void cancelCon() {
        for (Asset asset : assetRepo.findNoAccount()) {
            try {
                assetService.cancelConsignment(asset.getId());
            } catch (Exception e) {

            }
        }
    }

    @Test
    public void fixNoHistory() {
        List<Asset> assets = assetRepo.findByNoHistory();

        for (Asset asset : assets) {
            if (StringUtils.isNotBlank(asset.getTokenId())) {
                User user = userRepo.findById(asset.getUserId()).get();
                TokenHistory tokenHistory = tokenHistoryRepo.save(TokenHistory.builder()
                        .tokenId(asset.getTokenId())
                        .fromUser(asset.getMinter())
                        .fromUserId(asset.getMinterId())
                        .fromAvatar(asset.getMinterAvatar())
                        .toUser(user.getNickname())
                        .toUserId(user.getId())
                        .toAvatar(user.getAvatar())
                        .operation("出售")
                        .price(asset.getPrice())
                        .tokenId(asset.getTokenId())
                        .build());
                tokenHistory.setCreatedAt(asset.getCreatedAt());
                tokenHistoryRepo.save(tokenHistory);
            }

        }
        System.out.println(assets);
    }


    @Test
    public void fixHistoryPrice() {
        List<TokenHistory> list = tokenHistoryRepo.findByOperationAndPriceNull("转让");
        System.out.println(list.size());
        StringBuilder builder = new StringBuilder();
        list.parallelStream().forEach(tokenHistory -> {
            List<Asset> assets = assetRepo.findByTokenIdAndCreatedAtBetween(tokenHistory.getTokenId(), tokenHistory.getCreatedAt()
                    .minusSeconds(10), tokenHistory.getCreatedAt().plusSeconds(1));
            if (assets.size() != 1) {
                throw new BusinessException("");
            }
            Order order = orderRepo.findById(assets.get(0).getOrderId())
                    .orElseThrow(new BusinessException(assets.get(0).getId() + "订单没找到"));
            builder.append(order.getId()).append(",").append(order.getPrice()).append("\n");
        });
        System.out.println(builder);
    }

}