import logging

import pytest

from psd_tools.composite.blend import BLEND_FUNC, lighter_color, normal
from .test_composite import check_composite_quality

logger = logging.getLogger(__name__)


def test_lighter_color_descriptor_key() -> None:
    """Regression: b"lighterColor" was previously a typo (b"ligherColor")
    which silently fell back to the normal blend mode for effects/strokes."""
    assert BLEND_FUNC.get(b"lighterColor") is lighter_color
    assert BLEND_FUNC.get(b"lighterColor") is not normal


@pytest.mark.parametrize(
    ("filename",),
    [
        ("blend-modes/normal.psd",),
        ("blend-modes/multiply.psd",),
        ("blend-modes/screen.psd",),
        ("blend-modes/overlay.psd",),
        ("blend-modes/darken.psd",),
        ("blend-modes/lighten.psd",),
        ("blend-modes/color-dodge.psd",),
        ("blend-modes/linear-dodge.psd",),
        ("blend-modes/color-burn.psd",),
        ("blend-modes/linear-burn.psd",),
        ("blend-modes/hard-light.psd",),
        ("blend-modes/soft-light.psd",),
        pytest.param(
            "blend-modes/vivid-light.psd",
            marks=pytest.mark.xfail(reason="vivid light algorithm discrepency"),
        ),
        ("blend-modes/linear-light.psd",),
        ("blend-modes/pin-light.psd",),
        ("blend-modes/difference.psd",),
        ("blend-modes/exclusion.psd",),
        ("blend-modes/subtract.psd",),
        ("blend-modes/hard-mix.psd",),
        ("blend-modes/saturation.psd",),
        ("blend-modes/divide.psd",),
        ("blend-modes/hue.psd",),
        ("blend-modes/color.psd",),
        ("blend-modes/luminosity.psd",),
        ("blend-modes/pass-through.psd",),
        # Total test
        ("blend-modes/rgb-blend-modes.psd",),
        ("blend-modes/gray-blend-modes.psd",),
    ],
)
def test_blend_quality(filename: str) -> None:
    check_composite_quality(filename, threshold=0.01)


@pytest.mark.parametrize(
    ("filename",),
    [
        ("blend-modes/dissolve.psd",),
        ("blend-modes/cmyk-blend-modes.psd",),  # Fix me!
    ],
)
@pytest.mark.xfail
def test_blend_quality_xfail(filename: str) -> None:
    check_composite_quality(filename, threshold=0.01)


@pytest.mark.parametrize(
    "property",
    [
        ("opacity"),
        ("fill_adjustment"),
        ("fill_blendmode"),
        ("clipping_mask_adjustment"),
        ("clipping_mask_blendmode"),
        ("vector_mask"),
        ("blendmodes1"),
        ("blendmodes2"),
        ("blendmodes3"),
    ],
)
def test_passthrough_properties(property) -> None:
    filename = f"passthrough_{property}"
    check_composite_quality(f"{filename}.psd", 0.001, False)
