package com.izouma.nineth.service;

import com.github.kevinsawicki.http.HttpRequest;
import com.izouma.nineth.domain.Asset;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.NFT;
import com.izouma.nineth.dto.NFTAccount;
import com.izouma.nineth.event.CreateAssetEvent;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.AssetRepo;
import com.izouma.nineth.repo.TokenHistoryRepo;
import com.izouma.nineth.repo.UserRepo;
import io.ipfs.api.IPFS;
import io.ipfs.api.MerkleNode;
import io.ipfs.api.NamedStreamable;
import io.ipfs.multihash.Multihash;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
@Slf4j
@AllArgsConstructor
public class AssetMintService {
    private AssetRepo          assetRepo;
    private UserRepo           userRepo;
    private NFTService         nftService;
    private ApplicationContext applicationContext;
    private TokenHistoryRepo   tokenHistoryRepo;
    private Environment        env;

    public void mint(Asset asset, Long historyId) {
        User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
        if (StringUtils.isEmpty(user.getPublicKey())) {
            NFTAccount account = nftService.createAccount(user.getUsername() + "_");
            user.setNftAccount(account.getAccountId());
            user.setKmsId(account.getAccountKmsId());
            user.setPublicKey(account.getPublicKey());
            userRepo.save(user);
        }
        try {
            NFT nft = nftService.createToken(user.getNftAccount());
            if (nft != null) {
                asset.setTokenId(nft.getTokenId());
                asset.setBlockNumber(nft.getBlockNumber());
                asset.setTxHash(nft.getTxHash());
                asset.setGasUsed(nft.getGasUsed());
                if (asset.getIpfsUrl() == null) {
                    asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
                }
                assetRepo.save(asset);

                tokenHistoryRepo.findById(historyId).ifPresent(tokenHistory -> {
                    tokenHistory.setTokenId(nft.getTokenId());
                    tokenHistoryRepo.save(tokenHistory);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
    }

    public String ipfsUpload(String url) {
        try {
            IPFS ipfs = new IPFS("112.74.34.84", 5001);
            HttpRequest request = HttpRequest.get(url);
            File file = File.createTempFile("ipfs", ".tmp");
            request.receive(file);
            NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
            MerkleNode put = ipfs.add(file1).get(0);
            Multihash multihash = ipfs.pin.add(put.hash).get(0);
            log.info("上传ipfs成功 {}", multihash.toBase58());
            return multihash.toBase58();
        } catch (Exception e) {
        }
        return null;
    }
}
