import cn.reghao.oss.store.util.FileType;
import cn.reghao.jutil.media.FFmpegWrapper;
import cn.reghao.jutil.media.MediaQuality;
import cn.reghao.jutil.media.MediaResolution;
import cn.reghao.jutil.media.model.AudioProps;
import cn.reghao.jutil.media.model.MediaProps;
import cn.reghao.jutil.media.model.VideoProps;
import cn.reghao.jutil.jdk.security.DigestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author reghao
 * @date 2023-03-22 16:19:12
 */
@Slf4j
public class LocalFileTest {
    public void walkDir(Path path) throws IOException {
        Files.walkFileTree(path, new FileVisitor<>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String absolutePath = file.toString();
                process(absolutePath);
                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;
            }
        });
    }

    private void process(String absolutePath) {
        String mediaType = FileType.getMediaType(absolutePath);
        if (mediaType.startsWith("text")) {
            FileUtils.deleteQuietly(new File(absolutePath));
        }
    }

    private void getMediaInfo(String absolutePath) {
        MediaProps mediaProps = FFmpegWrapper.getMediaProps(absolutePath);
        if (mediaProps == null) {
            log.error("{} 没有媒体信息", absolutePath);
            return;
        }

        AudioProps audioProps = mediaProps.getAudioProps();
        if (audioProps == null) {
            log.error("{} 没有音频信息", absolutePath);
            return;
        }

        VideoProps videoProps = mediaProps.getVideoProps();
        if (videoProps == null) {
            log.error("{} 没有视频信息", absolutePath);
            return;
        }

        String audioCodec = audioProps.getCodecName();
        String videoCodec = videoProps.getCodecName();
        int width = videoProps.getCodedWidth().intValue();
        int height = videoProps.getCodedHeight().intValue();
        if ("aac".equals(audioCodec) && "h264".equals(videoCodec)) {
            MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
        } else {
            log.error("{} 不是 aac&h264", absolutePath);
        }
    }

    Map<String, String> map = new HashMap<>();
    List<String> list = new ArrayList<>();
    private void unique(String absolutePath) {
        try {
            String sha256sum = DigestUtil.sha256sum(absolutePath);
            String filePath = map.get(sha256sum);
            if (filePath == null) {
                map.put(sha256sum, filePath);
            } else {
                log.info("{} 已存在", filePath);
                list.add(filePath);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test22() throws IOException {
        String baseDir = "";
        Path path = Path.of(baseDir);
        walkDir(path);

        /*String filePath = "";
        String filePath1 = "";
        String mediaType = FileType.getMediaType(filePath);
        MediaProps mediaProps = FFmpegWrapper.getMediaProps(filePath);
        FFmpegWrapper.convertAudio(filePath, filePath1);*/
    }
}
