import cn.reghao.jutil.jdk.converter.ByteConverter;
import cn.reghao.jutil.jdk.converter.ByteType;
import cn.reghao.oss.api.dto.ServerInfo;
import cn.reghao.oss.sdk.OssClient;
import cn.reghao.tnb.file.app.zdisk.model.vo.FileTree;
import cn.reghao.oss.api.constant.ObjectType;
import net.sourceforge.tess4j.Tesseract;
import org.junit.jupiter.api.Test;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class DiskUnitTest {
    void walkDir(Path path) throws IOException {
        Files.walkFileTree(path, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                File dir1 = dir.toFile();
                process(dir1);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                File file1 = file.toFile();
                process(file1);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }

    List<FileTree> fileTreeList = new ArrayList<>();
    private void process(File file) {
        String basePath = dir.getAbsolutePath();
        String absolutePath = file.getAbsolutePath();
        String path = absolutePath.replace(basePath, "");
        String pid = file.getParent().replace(basePath, "");
        if (path.equals("")) {
            pid = "0";
        } else if (pid.equals("")) {
            pid = "/";
        }

        int fileType = ObjectType.Image.getCode();
        if (file.isDirectory()) {
            fileType = ObjectType.Dir.getCode();
        }

        String fileId = absolutePath.replace(basePath, "");
        FileTree fileTree = new FileTree(pid, fileId, fileType);
        fileTreeList.add(fileTree);
    }

    String baseDir = "/home/reghao/Downloads/";
    File dir = new File(baseDir);

    Map<String, List<FileTree>> getTreeMap() throws IOException {
        Path path = Paths.get(dir.getAbsolutePath());
        walkDir(path);

        Map<String, List<FileTree>> pidGroup = fileTreeList.stream()
                .collect(Collectors.groupingBy(FileTree::getPid));
        return new TreeMap<>(pidGroup);

    }

    public void treeTest() throws Exception {
        /*Map<String, List<FileTree>> pidTreeMap = getTreeMap();
        pidTreeMap.forEach((key, list) -> {
            String pid = key;
            for (FileTree fileTree : list) {
                String fileId = fileTree.getFileId();
            }
        });
        System.out.println("main-thread goto sleep...");*/
    }

    @Test
    public void ocrTest() throws Exception {
//        Tesseract tess = new Tesseract();
//        // 字库位置
//        tess.setDatapath("/home/reghao/Downloads/tess");
//        // 中英文混合
//        tess.setLanguage("eng+chi_sim");
        //tess.setLanguage("chi_sim");
//        tess.setLanguage("eng");

//        String imagePath = "/home/reghao/Downloads/1620438.png";
//        String result = tess.doOCR(new File(imagePath));
//        System.out.println(result);

        ByteConverter byteConverter = new ByteConverter();
        long value = 1623581261432L;
        value = 945039537733L;

        String str = byteConverter.convert(ByteType.Bytes, value);
        System.out.println(str);
    }

    @Test
    public void ossTest() throws Exception {
        String endpoint = "http://localhost:8010";
        String ak = "PmvonhHI";
        String sk = "28aQEK7wJ6U5m7E1u7";
        OssClient ossClient = new OssClient(endpoint, ak, sk);

        int channelCode = 101;
        ServerInfo serverInfo = ossClient.getServerInfo(channelCode);
        String uploadUrl = serverInfo.getOssUrl();
        String uploadToken = serverInfo.getToken();

        String baseDir = "/home/reghao/Downloads/1";
        //client.startWalk(baseDir);

        /*uploadUrl = "http://localhost:8020";
        String filePath = "/home/reghao/Downloads/abc.mp4";
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.printf("%s not exist\n", filePath);
            return;
        }
        client.upload(uploadUrl, uploadToken, Path.of(filePath));*/

        /*String objectId1 = "3c0403add7ec4127bf66cfdbb4a8ddf1";
        String signedUrl = uploader.getSignedUrl(objectId1);
        System.out.printf("signed url -> %s\n", signedUrl);*/
    }
}
