package top.r3944realms.docchecktoolrefactored.test;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.RepeatedTest;
import top.r3944realms.docchecktoolrefactored.core.DuplicateFinder;
import top.r3944realms.docchecktoolrefactored.core.FileHashCalculator;
import top.r3944realms.docchecktoolrefactored.io.scanner.FileScanner;
import top.r3944realms.docchecktoolrefactored.io.scanner.RobustParallelScanner;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class DuplicateFinderPerformanceTest {

    private static final String TEST_PATH = "D:/测试数据/JPG";
    private FileScanner scanner;
    private FileHashCalculator hashCalculator;

    @BeforeEach
    void setUp() {
        scanner = new RobustParallelScanner(20);
        hashCalculator = FileHashCalculator.defaultInstance();
    }

    @RepeatedTest(5)
    void compareFinderPerformance() throws IOException {
        Path rootDir = Paths.get(TEST_PATH);
        // Test finder WITHOUT pre-counting
        long startWithoutPrecount = System.nanoTime();
        DuplicateFinder finderWithoutPrecount = new DuplicateFinder(scanner, hashCalculator, false);
        finderWithoutPrecount.findDuplicates(rootDir);
        long durationWithoutPrecount = System.nanoTime() - startWithoutPrecount;

        // Test finder WITH pre-counting
        long startWithPrecount = System.nanoTime();
        DuplicateFinder finderWithPrecount = new DuplicateFinder(scanner, hashCalculator, true);
        finderWithPrecount.findDuplicates(rootDir);
        long durationWithPrecount = System.nanoTime() - startWithPrecount;

        // Convert to milliseconds
        long msWithout = TimeUnit.NANOSECONDS.toMillis(durationWithoutPrecount);
        long msWith = TimeUnit.NANOSECONDS.toMillis(durationWithPrecount);

        System.out.println("Without pre-counting: " + msWithout + " ms");
        System.out.println("With pre-counting: " + msWith + " ms");

//        // Assert that pre-counting provides benefit
//        assertTrue(msWith < msWithout * 1.2,
//                "Pre-counting version should not be more than 20% slower");
    }

    @Test
    void verifySameResults() throws IOException {
        DuplicateFinder finder1 = new DuplicateFinder(scanner, hashCalculator, false);
        var result1 = finder1.findDuplicates(Paths.get(TEST_PATH));

        DuplicateFinder finder2 = new DuplicateFinder(scanner, hashCalculator, true);
        var result2 = finder2.findDuplicates(Paths.get(TEST_PATH));

//        // Verify both methods find the same duplicates
//        assertTrue(result1.containsAll(result2) && result2.containsAll(result1),
//                "Both methods should find the same duplicate files");
    }
}