from ColorPicker_5 import *
#from gpiozero import RGBLED
import serial
from time import sleep

# Initialize serial communication with the Arduino
try:
    arduino = serial.Serial('COM9', 9600, timeout=1)
    sleep(2)  # Wait for the connection to establish
except:
    print("Couldn't find lamp")
    arduino=None

def send_color_to_arduino(self):
    command = f'C {self.r_send} {self.g_send} {self.b_send}\n'
    print(command)
    arduino.write(command.encode())

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self,*args, **kwargs)
        self.setupUi(self)
        #My code
        self.dial_r.setMinimum(0)
        self.dial_r.setMaximum(255)
        self.dial_g.setMinimum(0)
        self.dial_g.setMaximum(255)
        self.dial_b.setMinimum(0)
        self.dial_b.setMaximum(255)

        self.set_values_color()
        self.set_values_brighness()
        
        self.dial_r.valueChanged.connect(self.choose_red)
        self.dial_g.valueChanged.connect(self.choose_green)
        self.dial_b.valueChanged.connect(self.choose_blue)
        #self.slider_br.valueChanged.connect(self.choose_brighness)
        self.Colo_R.triggered.connect(self.set_values_color)
        self.Brightness_R.triggered.connect(self.set_values_brighness)

        self.off.clicked.connect(self.set_values_color)
        self.white.clicked.connect(self.set_values_white)
    
    def set_values_white(self):
        self.dial_r.setValue(255)
        self.lcdNumber_R.display(255)
        self.dial_g.setValue(255)
        self.lcdNumber_g.display(255)
        self.dial_b.setValue(255)
        self.lcdNumber_b.display(255)
        self.choose_color()
        
    def set_values_off(self):
        self.dial_r.setValue(0)
        self.lcdNumber_R.display(0)
        self.dial_g.setValue(0)
        self.lcdNumber_g.display(0)
        self.dial_b.setValue(0)
        self.lcdNumber_b.display(0)
        self.choose_color()

    def set_values_color(self):
        self.dial_r.setValue(0)
        self.lcdNumber_R.display(0)
        self.dial_g.setValue(0)
        self.lcdNumber_g.display(0)
        self.dial_b.setValue(0)
        self.lcdNumber_b.display(0)
        self.choose_color()
    
    def set_values_brighness(self):
        self.choose_color()
   
    def choose_red(self):
        red = self.dial_r.value()
        self.lcdNumber_R.display(red)
        self.choose_color()

    def choose_green(self):
        green = self.dial_g.value()
        self.lcdNumber_g.display(green)
        self.choose_color()
        
    def choose_blue(self):
        blue = self.dial_b.value()
        self.lcdNumber_b.display(blue)
        self.choose_color()
    
    def choose_brighness(self):
        brighness = 100
        self.choose_color()

    def choose_color(self):
        red = self.dial_r.value()
        green = self.dial_g.value()
        blue = self.dial_b.value()
        brighness = 100
        red = int(red * (brighness / 100))
        green = int(green * (brighness / 100))
        blue = int(blue * (brighness / 100))
        self.r_send=red
        self.g_send=green
        self.b_send=blue
        send_color_to_arduino(self)

        self.setStyleSheet(f"background-color: rgb({red}, {green}, {blue});")

        if (brighness <= 50) or (red < 125 and green < 125 and blue < 125):
            self.label_r.setStyleSheet("color: rgb(255, 255, 255)")
            self.label_g.setStyleSheet("color: rgb(255, 255, 255)")
            self.label_b.setStyleSheet("color: rgb(255, 255, 255)")
            self.label_tt.setStyleSheet("color: rgb(255, 255, 255)")
            self.white.setStyleSheet("color: rgb(255, 255, 255)")
            self.off.setStyleSheet("color: rgb(255, 255, 255)")

        else:
            self.label_r.setStyleSheet("color: rgb(0, 0, 0)")
            self.label_g.setStyleSheet("color: rgb(0, 0, 0)")
            self.label_b.setStyleSheet("color: rgb(0, 0, 0)")
            self.label_tt.setStyleSheet("color: rgb(0, 0, 0)")
            self.white.setStyleSheet("color: rgb(0, 0, 0)")

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    Window = MainWindow()
    Window.show()
    app.exec()