package com.ycwl.basic.image.pipeline.stages;

import com.ycwl.basic.image.pipeline.core.PhotoProcessContext;
import com.ycwl.basic.pipeline.core.StageResult;
import com.ycwl.basic.image.pipeline.enums.ImageType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;

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

/**
 * RestoreOrientationStage 单元测试
 */
class RestoreOrientationStageTest {

    @Test
    void testGetName() {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        assertEquals("RestoreOrientationStage", stage.getName());
    }

    @Test
    void testShouldExecute_NormalPhotoAndRotationApplied() {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        PhotoProcessContext context = PhotoProcessContext.builder()
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setRotationApplied(true);

        assertTrue(stage.shouldExecute(context));
    }

    @Test
    void testShouldExecute_NormalPhotoButNoRotationApplied_ShouldSkip() {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        PhotoProcessContext context = PhotoProcessContext.builder()
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setRotationApplied(false);

        assertFalse(stage.shouldExecute(context));
    }

    @Test
    void testShouldExecute_Puzzle_ShouldSkip() {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        PhotoProcessContext context = PhotoProcessContext.builder()
                .originalUrl("https://example.com/puzzle.png")
                .scenicId(123L)
                .imageType(ImageType.PUZZLE)
                .build();

        context.setRotationApplied(true);

        assertFalse(stage.shouldExecute(context));
    }

    @Test
    void testExecute_NoCurrentFile_ShouldFail() {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        PhotoProcessContext context = PhotoProcessContext.builder()
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setRotationApplied(true);

        StageResult result = stage.execute(context);

        assertTrue(result.isFailed());
        assertEquals("当前文件不存在", result.getMessage());
    }

    @Test
    void testExecute_Restore90Rotation(@TempDir Path tempDir) throws Exception {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        File testFile = createTestImage(tempDir, "test.jpg", 1920, 1080);

        PhotoProcessContext context = PhotoProcessContext.builder()
                .processId("test-restore-90")
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setOriginalFile(testFile);
        context.setRotationApplied(true);
        context.setImageRotation(90); // 原始旋转了90度,恢复需要旋转270度

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertEquals("已恢复原方向", result.getMessage());
    }

    @Test
    void testExecute_Restore270Rotation(@TempDir Path tempDir) throws Exception {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        File testFile = createTestImage(tempDir, "test.jpg", 1920, 1080);

        PhotoProcessContext context = PhotoProcessContext.builder()
                .processId("test-restore-270")
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setOriginalFile(testFile);
        context.setRotationApplied(true);
        context.setImageRotation(270); // 原始旋转了270度,恢复需要旋转90度

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertEquals("已恢复原方向", result.getMessage());
    }

    @Test
    void testExecute_Restore180Rotation(@TempDir Path tempDir) throws Exception {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        File testFile = createTestImage(tempDir, "test.jpg", 1920, 1080);

        PhotoProcessContext context = PhotoProcessContext.builder()
                .processId("test-restore-180")
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setOriginalFile(testFile);
        context.setRotationApplied(true);
        context.setImageRotation(180); // 原始旋转了180度,恢复需要旋转180度

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertEquals("已恢复原方向", result.getMessage());
    }

    @Test
    void testExecute_NoRotationInfo_ShouldSkip(@TempDir Path tempDir) throws Exception {
        RestoreOrientationStage stage = new RestoreOrientationStage();

        File testFile = createTestImage(tempDir, "test.jpg", 1920, 1080);

        PhotoProcessContext context = PhotoProcessContext.builder()
                .processId("test-no-rotation-info")
                .originalUrl("https://example.com/test.jpg")
                .scenicId(123L)
                .imageType(ImageType.NORMAL_PHOTO)
                .build();

        context.setOriginalFile(testFile);
        context.setRotationApplied(true);
        context.setImageRotation(null); // 无旋转信息

        StageResult result = stage.execute(context);

        assertTrue(result.isSkipped());
        assertEquals("无旋转信息", result.getMessage());
    }

    /**
     * 创建测试用的图片文件
     */
    private File createTestImage(Path tempDir, String filename, int width, int height) throws Exception {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        File file = tempDir.resolve(filename).toFile();
        ImageIO.write(image, "jpg", file);
        return file;
    }
}
