"""Tests for ISO channel code parsing."""

import pytest

from impakt.channel.code import ChannelCode, parse_channel_code


class TestChannelCodeParsing:
    """Test the 16-character ISO channel code parser."""

    def test_parse_full_code(self):
        code = ChannelCode.parse("11HEAD0000ACXA")
        assert code.is_valid
        assert code.test_object == "11"
        assert code.main_location == "HEAD"
        assert code.fine_location == "0000"
        assert code.measurement == "AC"
        assert code.direction == "X"
        assert code.sense == "A"

    def test_parse_with_filter_class(self):
        code = ChannelCode.parse("11HEAD0000ACXA60")
        assert code.is_valid
        assert code.filter_class == "60"

    def test_parse_neck_channel(self):
        code = ChannelCode.parse("11NECKUP00FOZA")
        assert code.test_object == "11"
        assert code.main_location == "NECK"
        assert code.fine_location == "UP00"
        assert code.measurement == "FO"
        assert code.direction == "Z"

    def test_parse_femur_channel(self):
        code = ChannelCode.parse("11FEMRLE00FOZA")
        assert code.main_location == "FEMR"
        assert code.fine_location == "LE00"
        assert code.measurement == "FO"

    def test_invalid_short_code(self):
        code = ChannelCode.parse("SHORT")
        assert not code.is_valid

    def test_group_key(self):
        cx = ChannelCode.parse("11HEAD0000ACXA")
        cy = ChannelCode.parse("11HEAD0000ACYA")
        cz = ChannelCode.parse("11HEAD0000ACZA")
        assert cx.group_key() == cy.group_key() == cz.group_key()

    def test_group_key_differs_for_different_locations(self):
        head = ChannelCode.parse("11HEAD0000ACXA")
        chest = ChannelCode.parse("11CHST0000ACXA")
        assert head.group_key() != chest.group_key()

    def test_is_component(self):
        assert ChannelCode.parse("11HEAD0000ACXA").is_component()
        assert ChannelCode.parse("11HEAD0000ACYA").is_component()
        assert ChannelCode.parse("11HEAD0000ACZA").is_component()
        assert not ChannelCode.parse("11HEAD0000ACRA").is_resultant() is False

    def test_is_resultant(self):
        assert ChannelCode.parse("11HEAD0000ACRA").is_resultant()

    def test_description(self):
        code = ChannelCode.parse("11HEAD0000ACXA")
        desc = code.description
        assert "Head" in desc
        assert "Acceleration" in desc
        assert "X" in desc

    def test_short_label(self):
        code = ChannelCode.parse("11HEAD0000ACXA")
        label = code.short_label
        assert "Head" in label
        assert "Accel" in label
        assert "X" in label

    def test_measurement_unit(self):
        code = ChannelCode.parse("11HEAD0000ACXA")
        assert code.measurement_unit == "m/s²"

    def test_matches_wildcard(self):
        code = ChannelCode.parse("11HEAD0000ACXA")
        assert code.matches("11HEAD0000AC*")
        assert code.matches("11HEAD*")
        assert code.matches("*ACXA")
        assert not code.matches("12HEAD*")

    def test_matches_set_notation(self):
        code = ChannelCode.parse("11HEAD0000ACXA")
        assert code.matches("11HEAD0000AC{X,Y,Z}A")
        code_y = ChannelCode.parse("11HEAD0000ACYA")
        assert code_y.matches("11HEAD0000AC{X,Y,Z}A")

    def test_convenience_function(self):
        code = parse_channel_code("11HEAD0000ACXA")
        assert code.is_valid
        assert code.test_object == "11"
