import XCTest
import CoreGraphics
import AVFoundation
@testable import 康康

/// 指标速记的局部裁剪几何。
/// 回归用例:屏上「宽而矮」的小框,必须裁出「宽 > 高」的照片 rect。
/// 旧实现用 `metadataOutputRectConverted`(传感器横向坐标)套到竖屏照片 → x/y 轴对调,
/// 把宽框裁成竖窄条(2026-05-31 真机 bug)。本组用例钉住正确的纯几何映射。
final class RegionImageCropperTests: XCTestCase {

    /// 竖屏照片 + 竖屏屏幕 + 宽框 → 裁出的 rect 必须是横向(宽 > 高)。
    func testWideBoxYieldsLandscapeCropRect() {
        let photo = CGSize(width: 3024, height: 4032)   // 竖屏照片(像素)
        let view = CGSize(width: 393, height: 852)       // 竖屏屏幕(点)
        let box = RegionFraming.box(in: view)            // 宽 84% / 矮 160

        let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)

        XCTAssertGreaterThan(rect.width, rect.height,
            "宽框应裁出宽 rect;若 width<height 说明轴对调 bug 复现")
        XCTAssertGreaterThan(rect.width, 0)
        XCTAssertGreaterThan(rect.height, 0)
    }

    /// 裁出的 rect 必须完整落在照片像素范围内(越界夹紧)。
    func testCropRectStaysInsidePhotoBounds() {
        let photo = CGSize(width: 3024, height: 4032)
        let view = CGSize(width: 393, height: 852)
        let box = RegionFraming.box(in: view)

        let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)

        XCTAssertGreaterThanOrEqual(rect.minX, 0)
        XCTAssertGreaterThanOrEqual(rect.minY, 0)
        XCTAssertLessThanOrEqual(rect.maxX, photo.width)
        XCTAssertLessThanOrEqual(rect.maxY, photo.height)
    }

    /// 宽框宽高比应与照片裁剪 rect 宽高比一致(aspect-fill 等比映射,不拉伸)。
    func testCropRectPreservesBoxAspectRatio() {
        let photo = CGSize(width: 3024, height: 4032)
        let view = CGSize(width: 393, height: 852)
        let box = RegionFraming.box(in: view)

        let rect = RegionImageCropper.cropRect(photoPixelSize: photo, box: box, in: view)

        let boxAspect = box.width / box.height
        let rectAspect = rect.width / rect.height
        XCTAssertEqual(rectAspect, boxAspect, accuracy: 0.05,
            "等比映射下,裁剪 rect 宽高比应与屏上小框一致")
    }

    /// 退化输入:零尺寸不应崩溃,返回 .zero。
    func testZeroInputsReturnZero() {
        XCTAssertEqual(
            RegionImageCropper.cropRect(photoPixelSize: .zero,
                                        box: CGRect(x: 0, y: 0, width: 10, height: 10),
                                        in: CGSize(width: 100, height: 200)),
            .zero)
    }

    // MARK: - aspect-FIT(静态图框选)

    /// 静态图以 aspect-fit 显示;宽框应裁出宽 rect 且落在照片范围内。
    func testAspectFitWideBoxYieldsLandscapeInsideBounds() {
        let photo = CGSize(width: 3024, height: 4032)
        let view = CGSize(width: 393, height: 852)
        let fitted = AVMakeRect(aspectRatio: photo, insideRect: CGRect(origin: .zero, size: view))
        // fitted 内的一个宽框
        let box = CGRect(x: fitted.minX + 30, y: fitted.midY - 50, width: fitted.width - 60, height: 100)

        let rect = RegionImageCropper.cropRectAspectFit(photoPixelSize: photo, box: box, imageFrame: fitted)

        XCTAssertGreaterThan(rect.width, rect.height)
        XCTAssertGreaterThanOrEqual(rect.minX, 0)
        XCTAssertGreaterThanOrEqual(rect.minY, 0)
        XCTAssertLessThanOrEqual(rect.maxX, photo.width)
        XCTAssertLessThanOrEqual(rect.maxY, photo.height)
    }

    /// 框正好等于整个显示区 → 裁出整张照片(像素)。
    func testAspectFitFullFrameBoxYieldsFullPhoto() {
        let photo = CGSize(width: 2000, height: 1000)        // 横向照片
        let view = CGSize(width: 400, height: 800)
        let fitted = AVMakeRect(aspectRatio: photo, insideRect: CGRect(origin: .zero, size: view))

        let rect = RegionImageCropper.cropRectAspectFit(photoPixelSize: photo, box: fitted, imageFrame: fitted)

        XCTAssertEqual(rect.minX, 0, accuracy: 1)
        XCTAssertEqual(rect.minY, 0, accuracy: 1)
        XCTAssertEqual(rect.width, photo.width, accuracy: 2)
        XCTAssertEqual(rect.height, photo.height, accuracy: 2)
    }

    /// 等比映射:选框宽高比应与裁出 rect 宽高比一致。
    func testAspectFitPreservesBoxAspectRatio() {
        let photo = CGSize(width: 3024, height: 4032)
        let view = CGSize(width: 393, height: 852)
        let fitted = AVMakeRect(aspectRatio: photo, insideRect: CGRect(origin: .zero, size: view))
        let box = CGRect(x: fitted.minX + 20, y: fitted.minY + 40, width: 180, height: 120)

        let rect = RegionImageCropper.cropRectAspectFit(photoPixelSize: photo, box: box, imageFrame: fitted)

        XCTAssertEqual(rect.width / rect.height, box.width / box.height, accuracy: 0.05)
    }

    func testAspectFitZeroInputsReturnZero() {
        XCTAssertEqual(
            RegionImageCropper.cropRectAspectFit(photoPixelSize: .zero,
                                                 box: CGRect(x: 0, y: 0, width: 10, height: 10),
                                                 imageFrame: CGRect(x: 0, y: 0, width: 100, height: 100)),
            .zero)
    }

}
