#!/usr/bin/env python
# Turn on using this command line :
# python3.7 path/to/file/light.py on

# Turn off using this command line :
# python3.7 path/to/file/light.py off

# Logger library compatible with multiprocessing
from loguru import logger

# Library to send command over I2C for the light module on the fan
import smbus
import RPi.GPIO
import subprocess  # nosec

# define the bus used to actuate the light module on the fan
bus = smbus.SMBus(1)

DEVICE_ADDRESS = 0x0D
rgb_effect_reg = 0x04
rgb_speed_reg = 0x05
rgb_color_reg = 0x06
rgb_off_reg = 0x07

################################################################################
# LEDs functions
################################################################################


def i2c_update():
    # Update the I2C Bus in order to really update the LEDs new values
    subprocess.Popen("i2cdetect -y 1".split(), stdout=subprocess.PIPE)  # nosec


def setRGB(R, G, B):
    """Update all LED at the same time"""
    try:
        bus.write_byte_data(DEVICE_ADDRESS, 0x00, 0xFF)
        bus.write_byte_data(DEVICE_ADDRESS, 0x01, R & 0xFF)
        bus.write_byte_data(DEVICE_ADDRESS, 0x02, G & 0xFF)
        bus.write_byte_data(DEVICE_ADDRESS, 0x03, B & 0xFF)
        i2c_update()
    except Exception as e:
        logger.exception(f"An Exception has occured in the light library at {e}")


def setRGBOff():
    """Turn off the RGB LED"""
    try:
        bus.write_byte_data(DEVICE_ADDRESS, 0x07, 0x00)
        i2c_update()
    except Exception as e:
        logger.exception(f"An Exception has occured in the light library at {e}")


def setRGBEffect(effect):
    """Choose an effect, 0-4

    0: Water light
    1: Breathing light
    2: Marquee
    3: Rainbow lights
    4: Colorful lights
    """

    if effect >= 0 and effect <= 4:
        try:
            bus.write_byte_data(DEVICE_ADDRESS, rgb_effect_reg, effect & 0xFF)
        except Exception as e:
            logger.exception(f"An Exception has occured in the light library at {e}")


def setRGBSpeed(speed):
    """Set the effect speed, 1-3, 3 being the fastest speed"""
    if speed >= 1 and speed <= 3:
        try:
            bus.write_byte_data(DEVICE_ADDRESS, rgb_speed_reg, speed & 0xFF)
        except Exception as e:
            logger.exception(f"An Exception has occured in the light library at {e}")


def setRGBColor(color):
    """Set the color of the water light and breathing light effect, 0-6

    0: Red
    1: Green (default)
    2: Blue
    3: Yellow
    4: Purple
    5: Cyan
    6: White
    """

    if color >= 0 and color <= 6:
        try:
            bus.write_byte_data(DEVICE_ADDRESS, rgb_color_reg, color & 0xFF)
        except Exception as e:
            logger.exception(f"An Exception has occured in the light library at {e}")


def light(state):
    """Turn the LED on or off"""

    if state == "on":
        RPi.GPIO.output(21, RPi.GPIO.HIGH)
    elif state == "off":
        RPi.GPIO.output(21, RPi.GPIO.LOW)


## Wait message: Green
## Actuate message: White
## Pumping message: Blue
## Pumping 2 message: BLue + Green

# This is called if this script is launched directly
if __name__ == "__main__":
    # TODO This should be a test suite for this library
    import RPi.GPIO as GPIO
    import sys

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(21, GPIO.OUT)

    state = str(sys.argv[1])

    light(state)
