import pytest

from sangaboard import Sangaboard
from sangaboard.extensible_serial_instrument import DummySerialDevice

class DummySangaboardPort(DummySerialDevice):
    """Create a dummy port, with the minimal responses to work.
    """

    def __init__(self, firmware_version="Sangaboard Firmware v1.0.0"):
        self.pwm_channels_num = 2
        self.pwm_frequency = 64000
        self.pwm_vals = [0.0,0.0,0.0]
        self.cc_val = 0.0
        self.register_response("version", f"{firmware_version}\r\n")
        self.register_response("list_modules", "Illumination: default\r\n--END--\r\n")
        self.register_response("board", "Sangaboard v0.5\r\n")
        self.register_response("led_channels", self.pwm_channels)
        self.register_response("led_frequency\\?? ?([0-9?]+)", self.led_frequency)
        self.register_response("led_pwm\\?? ?([0-9?])? ?([0-9.]+)?", self.led_pwm)
        self.register_response("led_cc\\?? ?([0-9.?]+)?", self.led_cc)

    def pwm_channels(self, groups):
        return f"CC:1 PWM:{self.pwm_channels_num}\r\n"

    def led_frequency(self, groups):
        if groups[0] is not None and groups[0] != "?" and groups[0] != "":
            self.pwm_frequency = int(groups[0])

        return f"PWM Frequency:{self.pwm_frequency}Hz\r\n"

    def led_pwm(self, groups):
        if groups[0] is not None and groups[0] != "?" and groups[0] != "":
            index = int(groups[0])
            value = float(groups[1])
            self.pwm_vals[index] = value
            return f"PWM:{index}:{value}\r\n"

        res = "PWM:"
        for i in range(self.pwm_channels_num):
            res += f"{self.pwm_vals[i]};"
        res += "\r\n"
        return res

    def led_cc(self, groups):
        print(groups)
        if groups[0] is not None and groups[0] != "?" and groups[0] != "":
            self.cc_val = float(groups[0])
        return f"CC LED:{self.cc_val}\r\n"

def test_led_frequency():
    port = DummySangaboardPort()
    sb = Sangaboard(port)
    assert sb.illumination is not None
    assert sb.illumination.pwm_frequency == 64000
    sb.illumination.pwm_frequency = 100000
    assert sb.illumination.pwm_frequency == 100000

def test_channel_num():
    port = DummySangaboardPort()
    sb = Sangaboard(port)
    assert sb.illumination is not None
    assert sb.illumination.channels == [1, 2]
    port.pwm_channels_num = 1
    assert sb.illumination.channels == [1, 1]
    port.pwm_channels_num = 2
    assert sb.illumination.channels == [1, 2]

def test_pwm():
    port = DummySangaboardPort()
    sb = Sangaboard(port)
    assert sb.illumination is not None
    assert sb.illumination.get_pwm_led(0) == 0
    assert sb.illumination.get_pwm_led(1) == 0
    sb.illumination.set_pwm_led(0, 0.5)
    assert sb.illumination.get_pwm_led(0) == 0.5
    assert sb.illumination.get_pwm_led(1) == 0
    sb.illumination.set_pwm_led(1, 0.2)
    assert sb.illumination.get_pwm_led(0) == 0.5
    assert sb.illumination.get_pwm_led(1) == 0.2
    sb.illumination.set_pwm_led(0, 0)
    sb.illumination.set_pwm_led(1, 0)
    assert sb.illumination.get_pwm_led(0) == 0
    assert sb.illumination.get_pwm_led(1) == 0


def test_cc():
    port = DummySangaboardPort()
    sb = Sangaboard(port)
    assert sb.illumination is not None
    assert sb.illumination.cc_led == 0.0
    sb.illumination.cc_led = 0.4
    assert sb.illumination.cc_led == 0.4
    sb.illumination.cc_led = 0.99
    assert sb.illumination.cc_led == 0.99
    sb.illumination.cc_led = 0.0
    assert sb.illumination.cc_led == 0.0