import cn.reghao.tnb.content.api.dto.CamPhoto;
import cn.reghao.tnb.file.app.FileApplication;
import cn.reghao.tnb.file.app.service.EarthService;
import cn.reghao.tnb.file.app.util.JarFileResources;
import cn.reghao.tnb.file.app.util.PhotoExif;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ActiveProfiles;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2024-04-17 07:18:59
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = FileApplication.class)
public class FileTest {
    void walkDir(Path path) throws IOException {
        Files.walkFileTree(path, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                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<CamPhoto> camPhotoList = new ArrayList<>();
    private void process(File file) {
        CamPhoto camPhoto = PhotoExif.getCamPhoto(file.getAbsolutePath());
        if (camPhoto != null && camPhoto.getLatitude() != null) {
            camPhotoList.add(camPhoto);
        }
    }

    String baseDir = "/home/reghao/data/vdisk/320g/photo/";
    String kmlFile = "/home/reghao/Downloads/my_places.kml";
    @Autowired
    EarthService earthService;
    @Test
    public void photoTest() throws Exception {
        walkDir(Path.of(baseDir));
        earthService.getEarthKml(kmlFile, camPhotoList);
        System.out.println();
    }

    // print input stream
    private static void printInputStream(InputStream is) {
        try (InputStreamReader streamReader = new InputStreamReader(is, StandardCharsets.UTF_8);
             BufferedReader reader = new BufferedReader(streamReader)) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getFileFromJar() throws IOException, URISyntaxException {
        ClassLoader classLoader = this.getClass().getClassLoader();
        ClassLoader classLoader1 = Thread.currentThread().getContextClassLoader();
        ClassLoader classLoader2 = FileTest.class.getClassLoader();

        String resourcePath = "templates/index.html";
        URL resource = classLoader.getResource(resourcePath);
        File file = new File(resource.toURI());
        InputStream inputStream = classLoader.getResourceAsStream(resourcePath);

        URL resource1 = classLoader1.getResource(resourcePath);
        URL resource2 = classLoader2.getResource(resourcePath);

        InputStream resourceAsStream = new ClassPathResource(resourcePath).getInputStream();


        /*URL resource = Thread.currentThread().getContextClassLoader().getResource(avatarResource);
        String avatarPath = resource.getPath();
        System.out.println(avatarPath);
        File file = new File(avatarPath);
        byte[] imageBytes = Files.readAllBytes(file.toPath());*/

        JarFileResources app = new JarFileResources();
        // Sample 3 - read all files from a resources folder (jar version)
        try {
            // get paths from src/main/resources/json
            List<Path> result = app.getPathsFromResourceJar("json");
            for (Path path : result) {
                System.out.println("Path : " + path);

                String filePathInJar = path.toString();
                // Windows will returns /json/file1.json, cut the first /
                // the correct path should be json/file1.json
                if (filePathInJar.startsWith("/")) {
                    filePathInJar = filePathInJar.substring(1, filePathInJar.length());
                }

                System.out.println("filePathInJar : " + filePathInJar);
                // read a file from resource folder
                InputStream is = app.getFileFromResourceAsStream(filePathInJar);
                printInputStream(is);
            }
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }
}
