"""
Palette construction.
"""

import numpy as np
import pandas as pd
import pytest
import plotting_utils as plu


def test_builtin_palettes_are_colour_lists():
    for palette in (plu.ten_godisnot, plu.darjeeling, plu.fantastic_fox):
        assert len(palette) > 0


def test_create_palette_from_seaborn_palette(df):
    colors = plu.create_palette(df, "cat", palette="tab10")
    assert set(colors) == set(df["cat"].cat.categories)


def test_create_palette_from_col_list(df):
    cats = list(df["cat"].cat.categories)
    colors = plu.create_palette(df, "cat", col_list=["#FF0000", "#00FF00", "#0000FF"])
    assert set(colors) == set(cats)


def test_create_palette_respects_order(df):
    order = ["c", "b", "a"]
    colors = plu.create_palette(df, "cat", palette="tab10", order=order)
    assert list(colors) == order


def test_create_palette_on_object_dtype(df):
    """Column need not be categorical -- the unique() fallback path."""
    df = df.copy()
    df["cat"] = df["cat"].astype(str)
    colors = plu.create_palette(df, "cat", palette="tab10")
    assert set(colors) == {"a", "b", "c"}


@pytest.mark.parametrize("saturation,lightness", [(0.5, None), (None, 0.5), (0.7, 0.3)])
def test_create_palette_saturation_lightness(df, saturation, lightness):
    colors = plu.create_palette(
        df, "cat", palette="tab10", saturation=saturation, lightness=lightness
    )
    assert len(colors) == 3
