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 com.ycwl.basic.model.Crop;
import com.ycwl.basic.utils.ImageUtils;
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.*;

/**
 * ImageOrientationStage 单元测试
 */
class ImageOrientationStageTest {

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

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

    @Test
    void testShouldExecute_NormalPhoto() {
        ImageOrientationStage stage = new ImageOrientationStage();

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

        assertTrue(stage.shouldExecute(context));
    }

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

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

        assertFalse(stage.shouldExecute(context));
    }

    @Test
    void testShouldExecute_MobileUpload_ShouldSkip() {
        ImageOrientationStage stage = new ImageOrientationStage();

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

        assertFalse(stage.shouldExecute(context));
    }

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

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

        StageResult result = stage.execute(context);

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

    /**
     * 测试综合判断：物理横图 + rotation=0 → 横图
     */
    @Test
    void testExecute_LandscapeImage_NoRotation(@TempDir Path tempDir) throws Exception {
        ImageOrientationStage stage = new ImageOrientationStage();

        // 创建物理横图 (1920x1080)
        File landscapeFile = createTestImage(tempDir, "landscape.jpg", 1920, 1080);

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

        context.setOriginalFile(landscapeFile);

        // 无 rotation 信息
        Crop crop = new Crop();
        crop.setRotation(0);
        context.setCrop(crop);

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertTrue(context.isLandscape(), "应该判定为横图");
    }

    /**
     * 测试综合判断：物理横图 + rotation=90 → 竖图
     * 场景：一张横向分辨率的图片，但内容是竖着的（需要旋转90度才能正确显示）
     */
    @Test
    void testExecute_LandscapeImage_Rotation90_ShouldBePortrait(@TempDir Path tempDir) throws Exception {
        ImageOrientationStage stage = new ImageOrientationStage();

        // 创建物理横图 (1920x1080)，但内容实际是竖着的
        File landscapeFile = createTestImage(tempDir, "landscape_rotated.jpg", 1920, 1080);

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

        context.setOriginalFile(landscapeFile);

        // rotation=90 表示需要旋转90度才能正确显示
        Crop crop = new Crop();
        crop.setRotation(90);
        context.setCrop(crop);

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertFalse(context.isLandscape(), "旋转90度后应该是竖图");
    }

    /**
     * 测试综合判断：物理竖图 + rotation=90 → 横图
     * 场景：一张竖向分辨率的图片，但内容是横着的（需要旋转90度才能正确显示）
     */
    @Test
    void testExecute_PortraitImage_Rotation90_ShouldBeLandscape(@TempDir Path tempDir) throws Exception {
        ImageOrientationStage stage = new ImageOrientationStage();

        // 创建物理竖图 (1080x1920)，但内容实际是横着的
        File portraitFile = createTestImage(tempDir, "portrait_rotated.jpg", 1080, 1920);

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

        context.setOriginalFile(portraitFile);

        // rotation=90 表示需要旋转90度才能正确显示
        Crop crop = new Crop();
        crop.setRotation(90);
        context.setCrop(crop);

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertTrue(context.isLandscape(), "旋转90度后应该是横图");
    }

    /**
     * 测试综合判断：物理横图 + rotation=270 → 竖图
     */
    @Test
    void testExecute_LandscapeImage_Rotation270_ShouldBePortrait(@TempDir Path tempDir) throws Exception {
        ImageOrientationStage stage = new ImageOrientationStage();

        File landscapeFile = createTestImage(tempDir, "landscape_270.jpg", 1920, 1080);

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

        context.setOriginalFile(landscapeFile);

        Crop crop = new Crop();
        crop.setRotation(270);
        context.setCrop(crop);

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertFalse(context.isLandscape(), "旋转270度后应该是竖图");
    }

    /**
     * 测试综合判断：物理横图 + rotation=180 → 横图
     */
    @Test
    void testExecute_LandscapeImage_Rotation180_ShouldBeLandscape(@TempDir Path tempDir) throws Exception {
        ImageOrientationStage stage = new ImageOrientationStage();

        File landscapeFile = createTestImage(tempDir, "landscape_180.jpg", 1920, 1080);

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

        context.setOriginalFile(landscapeFile);

        Crop crop = new Crop();
        crop.setRotation(180);
        context.setCrop(crop);

        StageResult result = stage.execute(context);

        assertTrue(result.isSuccess());
        assertTrue(context.isLandscape(), "旋转180度后仍然是横图");
    }

    /**
     * 创建测试用的图片文件
     */
    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;
    }
}
