package com.izouma.nineth.service;

import com.izouma.nineth.ApplicationTests;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Scanner;

public class NFTServiceTest extends ApplicationTests {
    @Autowired
    private NFTService nftService;

    @Test
    public void createAccount() {
        nftService.createAccount(RandomStringUtils.randomAlphabetic(8));
    }

    @Test
    public void createToken() throws Exception {
        for (int i = 0; i < 100; i++) {
            nftService.createToken("nine-space-official");
        }
    }

    @Test
    public void testCreateToken() {
    }

    @Test
    public void transferToken() throws Exception {
        nftService.transferToken("000000000000000000000000000000000000000000000000000000000000009d",
                "9th_BHlKkGWw", "9th_test");
    }

    @Test
    public void setApprovalForAll() throws Exception {
        nftService.setApprovalForAll("9th_test");
    }

    @Test
    public void deployContract() throws Exception {
        ProcessBuilder builder = new ProcessBuilder("/Users/drew/Projects/Java/9th/src/main/contract/mychain_solc", "--bin", "9th.sol");
        builder.directory(new File("/Users/drew/Projects/Java/9th/src/main/contract").getAbsoluteFile());

        Process process = builder.start();

        Scanner s = new Scanner(process.getInputStream());
        StringBuilder text = new StringBuilder();
        while (s.hasNextLine()) {
            text.append(s.nextLine());
            text.append("\n");
        }
        s.close();

        int result = process.waitFor();

        String[] lines = text.toString().split("\n");
        int idx = 0;
        for (int i = 0; i < lines.length; i++) {
            if ("======= 9th.sol:ERC721PresetMinterPauserAutoId =======".equals(lines[i])) {
                idx = i;
                break;
            }
        }
        System.out.println(lines[idx]);
        System.out.println(lines[idx + 2]);


        nftService.deployContract("nine_space_test" + System.currentTimeMillis(), lines[idx + 2]);
    }
}