"""
v2: 统一到逻辑宽度 430px 后对比。
H5 截图 1290px (DPR 3) → 缩放到 430px
小程序截图 645px (DPR 1.5) → 缩放到 430px
然后裁剪到相同高度对比。
"""
import sys
from pathlib import Path
from PIL import Image

SCREENSHOTS_DIR = Path(__file__).resolve().parents[2] / "docs" / "h5_ui" / "screenshots"
LOGICAL_WIDTH = 430

def resize_to_width(src: Path, dst: Path, target_width: int) -> dict:
    img = Image.open(src)
    if img.width == target_width:
        img.save(dst)
        return {"width": img.width, "height": img.height, "scaled": False}
    ratio = target_width / img.width
    new_height = int(img.height * ratio)
    resized = img.resize((target_width, new_height), Image.LANCZOS)
    resized.save(dst)
    return {"width": target_width, "height": new_height, "scaled": True, "ratio": ratio}

def main():
    h5_path = SCREENSHOTS_DIR / "board-finance.png"
    mp_path = SCREENSHOTS_DIR / "mp-board-finance.png"

    for p in [h5_path, mp_path]:
        if not p.exists():
            print(f"❌ 文件不存在: {p}")
            sys.exit(1)

    h5_img = Image.open(h5_path)
    mp_img = Image.open(mp_path)
    print(f"H5 原始: {h5_img.width}×{h5_img.height} (DPR 3, 逻辑 {h5_img.width/3:.0f}×{h5_img.height/3:.0f})")
    print(f"MP 原始: {mp_img.width}×{mp_img.height} (DPR 1.5, 逻辑 {mp_img.width/1.5:.0f}×{mp_img.height/1.5:.0f})")

    # 方案: 都缩放到 1290px 宽（H5 原始宽度），保持 H5 不变
    # MP 需要 ×2 缩放
    TARGET_W = 1290

    h5_resized = SCREENSHOTS_DIR / "cmp-h5.png"
    mp_resized = SCREENSHOTS_DIR / "cmp-mp.png"

    h5_r = resize_to_width(h5_path, h5_resized, TARGET_W)
    mp_r = resize_to_width(mp_path, mp_resized, TARGET_W)
    print(f"\nH5 缩放后: {h5_r['width']}×{h5_r['height']}")
    print(f"MP 缩放后: {mp_r['width']}×{mp_r['height']}")

    # MP 逻辑高度 = 1128/1.5 = 752px → 物理高度 752×3 = 2256px (在 DPR3 下)
    # H5 前 2256px 对应逻辑前 752px
    # 所以裁剪 H5 到 2256px 高度，MP 已经是 2256px
    mp_logical_h = mp_img.height / 1.5  # 752
    h5_crop_h = int(mp_logical_h * 3)   # 2256 (DPR 3 下的物理像素)

    print(f"\nMP 逻辑高度: {mp_logical_h:.0f}px")
    print(f"H5 裁剪到: {h5_crop_h}px (对应逻辑 {mp_logical_h:.0f}px)")

    # 裁剪 H5
    h5_full = Image.open(h5_resized)
    h5_cropped = h5_full.crop((0, 0, TARGET_W, h5_crop_h))
    h5_cropped.save(h5_resized)

    # MP 已经是 2256px 高，确认
    mp_full = Image.open(mp_resized)
    if mp_full.height != h5_crop_h:
        print(f"⚠️ MP 高度 {mp_full.height} != H5 裁剪高度 {h5_crop_h}，裁剪 MP")
        mp_cropped = mp_full.crop((0, 0, TARGET_W, min(mp_full.height, h5_crop_h)))
        mp_cropped.save(mp_resized)
        # 也裁剪 H5 到相同高度
        final_h = min(mp_full.height, h5_crop_h)
        if h5_crop_h > final_h:
            h5_cropped2 = Image.open(h5_resized).crop((0, 0, TARGET_W, final_h))
            h5_cropped2.save(h5_resized)

    final_h5 = Image.open(h5_resized)
    final_mp = Image.open(mp_resized)
    print(f"\n最终对比尺寸:")
    print(f"  H5: {final_h5.width}×{final_h5.height}")
    print(f"  MP: {final_mp.width}×{final_mp.height}")
    print(f"\n✅ 可用 image-compare 对比:")
    print(f"  图1: {h5_resized}")
    print(f"  图2: {mp_resized}")

if __name__ == "__main__":
    main()
