import pytest

import pandas as pd
from plotly import exceptions

from pepmeasure.utils import (
    sanitize_seq,
    get_column_name,
    get_distinct_seq,
    normalize_color,
    extract_related_kwargs,
    convert_exponential_to_suffix,
    get_match_for_seq,
)
from tests.constants import PEPTIDES


def test_sanitize_seq():
    assert "PEPTIDE" == sanitize_seq("pEPtiDe :)")


def test_get_column_name():
    assert "Intensity" == get_column_name(PEPTIDES, "intensity")
    normalized = PEPTIDES.rename(columns={"Intensity": "Normalized intensity"})
    assert "Normalized intensity" == get_column_name(normalized, "intensity")
    with pytest.raises(ValueError) as e:
        get_column_name(PEPTIDES, "test")
    assert "could not be found" in str(e.value)


def test_get_distinct_seq():
    peptides = pd.DataFrame(
        {
            "Sequence": [
                "SRVLNLGPITRK",
                "NDPFANKDDPFYYDWKNLQ",
                "SRVLNLGPITRK",
                "EEGEFEEEAEEEVA",
                "GPPGPPGPPGHPGPQGPPG",
                "GPPGPPGPPGHPGPQGPPG",
            ]
        }
    )
    expected = pd.DataFrame(
        {
            "Sequence": [
                "SRVLNLGPITRK",
                "NDPFANKDDPFYYDWKNLQ",
                "EEGEFEEEAEEEVA",
                "GPPGPPGPPGHPGPQGPPG",
            ]
        }
    ).reset_index(drop=True)
    assert expected.equals(get_distinct_seq(peptides).reset_index(drop=True))


def test_normalize_color():
    assert "rgb(13, 8, 135)" == normalize_color(0.0, 0.0, 100.0, "Plasma")
    assert "rgb(182, 48, 139)" == normalize_color(42.0, 0.0, 100.0, "Plasma")
    with pytest.raises(exceptions.PlotlyError) as e:
        normalize_color(42.0, 0.0, 100.0, "Something")
    assert "not a built-in scale" in str(e.value)


def test_extract_related_kwargs():
    mapping = {
        "param_a": "param_1",
        "param_b": "param_2",
        "param_c": "param_3",
    }
    params = {
        "param_a": "foo",
        "param_c": "foo",
        "param_d": "foo",
    }
    expected = {
        "param_1": "foo",
        "param_3": "foo",
    }
    assert expected == extract_related_kwargs(mapping, params)


def test_convert_exponential_to_suffix():
    assert "10" == convert_exponential_to_suffix(1)
    assert "10k" == convert_exponential_to_suffix(4)
    assert "10M" == convert_exponential_to_suffix(7)
    assert "10B" == convert_exponential_to_suffix(10)


def test_get_match_for_seq():
    data = pd.DataFrame(
        {
            "Sample": ["AD01_C1_INSOLUBLE_01", "CTR01_C1_INSOLUBLE_01"],
            "Protein ID": ["A0A075B6S2", "A0A075B6S2"],
            "Sequence": ["FSGVPDR", "PEPTIDE"],
            "Intensity": [936840.0, "NaN"],
            "PEP": [0.0068633, 0.0056387],
            "GRAVY": [2.0, 1.0],
        }
    )
    seq = "PEPTIDE"
    expected_match = {
        "Sequence": "PEPTIDE",
        "Protein ID": "A0A075B6S2",
        "GRAVY": 1.0,
    }
    assert (1, expected_match) == get_match_for_seq(data, seq)
    assert (0, {}) == get_match_for_seq(data, "PEP")
