from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, QFrame, QHBoxLayout, QPushButton, QVBoxLayout, QSizePolicy, QLabel
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtCore import Qt, QUrl, QRectF
from PySide6.QtGui import QPainter, QPen, QFont, QColor
import sys


class RPMWidget(QWidget):
    def __init__(self, rpms=None, max_rpm=6000, parent=None):
        super().__init__(parent)
        self.rpms = rpms if rpms is not None else [1200, 800, 1500, 2200]
        self.max_rpm = max_rpm
        self.setMinimumSize(40, 80) 

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        w = self.width()
        h = self.height()
        radius = min(w, h) / 2 * 0.75 
        centers = [
            (w * 0.25, h * 0.25),
            (w * 0.75, h * 0.25),
            (w * 0.25, h * 0.75),
            (w * 0.75, h * 0.75),
        ]
        colors = ["#ff5555", "#55aaff", "#55dd55", "#ffaa00"]
        pen_bg = QPen(QColor("#dddddd"), max(1, int(radius*0.10)))
        font = QFont("Sans", max(6, int(radius*0.13)))
        painter.setFont(font)
        for i in range(4):
            cx, cy = centers[i]
            rpm = self.rpms[i] if i < len(self.rpms) else 0
            painter.setPen(pen_bg)
            painter.drawArc(QRectF(cx - radius/2, cy - radius/2, radius, radius), 0, 360*16)
            span = -int(max(0, min(1.0, rpm / self.max_rpm)) * 360 * 16)
            pen_fg = QPen(QColor(colors[i]), max(1, int(radius*0.10)))
            painter.setPen(pen_fg)
            painter.drawArc(QRectF(cx - radius/2, cy - radius/2, radius, radius), 90*16, span)
            painter.setPen(QColor("#000000"))
            painter.drawText(QRectF(cx - radius/2, cy - radius/2, radius, radius), Qt.AlignCenter, f"{rpm}\nRPM")
        painter.end()

class TemperatureHumidityWidget(QWidget):
    def __init__(self, temp=22.5, hum=45, parent=None):
        super().__init__(parent)
        self.temp = temp
        self.hum = hum
        layout = QVBoxLayout(self)
        layout.setContentsMargins(8,8,8,8)
        layout.setSpacing(6)

        self.title = QLabel("Environment")
        self.title.setAlignment(Qt.AlignCenter)
        self.title.setFont(QFont("Sans", 10, QFont.Bold))
        layout.addWidget(self.title)

        self.temp_label = QLabel(f"{self.temp} °C")
        self.temp_label.setAlignment(Qt.AlignCenter)
        self.temp_label.setFont(QFont("Sans", 18, QFont.Bold))
        layout.addWidget(self.temp_label)

        self.temp_text = QLabel("Temperature")
        self.temp_text.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.temp_text)

        self.hum_label = QLabel(f"{self.hum} %")
        self.hum_label.setAlignment(Qt.AlignCenter)
        self.hum_label.setFont(QFont("Sans", 18, QFont.Bold))
        layout.addWidget(self.hum_label)

        self.hum_text = QLabel("Humidity")
        self.hum_text.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.hum_text)

        layout.addStretch(1)

    def set_values(self, temp, hum):
        self.temp = temp
        self.hum = hum
        self.temp_label.setText(f"{self.temp} °C")
        self.hum_label.setText(f"{self.hum} %")

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("DashBoard")
        self.resize(1000, 600)
        
        self.mapisvisible = False

        self.actions = {
            1: self.action1,
            2: self.action2,
            3: self.action3,
            4: self.action4,
            5: self.action5,
            6: self.action6,
            7: self.action7,
            8: self.action8,
            9: self.action9,
            10: self.action10
        }

        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        self.main_layout = QHBoxLayout()
        central_widget.setLayout(self.main_layout)

        button_panel = QWidget()

        button_panel.setFixedWidth(60)
        button_layout = QVBoxLayout()
        button_layout.setSpacing(8)
        button_panel.setLayout(button_layout)

        for i in range(1, 11):
            btn = QPushButton(f"Button {i}")
            btn.setFixedWidth(40)
            btn.setFixedHeight(40)
            btn.clicked.connect(lambda checked, x=i: self.button_action(x))
            button_layout.addWidget(btn)

        self.main_layout.addWidget(button_panel)



        right_panel = QWidget()
        right_layout = QVBoxLayout()
        right_layout.setContentsMargins(6, 6, 6, 6)
        right_layout.setSpacing(8)
        right_panel.setLayout(right_layout)


        self.top_row = QWidget()
        top_layout = QHBoxLayout()
        top_layout.setSpacing(8)
        top_layout.setContentsMargins(0, 0, 0, 0)
        self.top_row.setLayout(top_layout)

        self.frame_top1 = QFrame()
        self.frame_top1.setFrameShape(QFrame.Box)
        self.frame_top1.setStyleSheet("background:#ffffff;")
        self.frame_top1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        top_layout.addWidget(self.frame_top1, 1)

        rpm_widget = RPMWidget(rpms=[1500, 2200, 1800, 950], max_rpm=8000)
        rpm_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        frame1_layout = QVBoxLayout(self.frame_top1)
        frame1_layout.setContentsMargins(8, 8, 8, 8)
        frame1_layout.addWidget(rpm_widget)

        self.frame_top2 = QFrame()
        self.frame_top2.setFrameShape(QFrame.Box)
        self.frame_top2.setStyleSheet("background:#f8f8f8;")
        self.frame_top2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        top_layout.addWidget(self.frame_top2, 1)

        right_layout.addWidget(self.top_row, 2)

        self.web_frame = QWebEngineView()
        self.web_frame.setUrl(QUrl("https://store-milo.hubworld.net/uploads/app_691f3a6fabbba/view.html"))
        self.web_frame.hide()
        right_layout.addWidget(self.web_frame, 6)

        self.bottom_row = QWidget()
        bottom_layout = QHBoxLayout()
        bottom_layout.setSpacing(8)
        bottom_layout.setContentsMargins(0, 0, 0, 0)
        self.bottom_row.setLayout(bottom_layout)

        self.bottom_frame1 = QFrame()
        self.bottom_frame1.setFrameShape(QFrame.Box)
        self.bottom_frame1.setStyleSheet("background:#ffffff;")
        self.bottom_frame1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        bottom_layout.addWidget(self.bottom_frame1, 1)

        self.bottom_frame2 = QFrame()
        self.bottom_frame2.setFrameShape(QFrame.Box)
        self.bottom_frame2.setStyleSheet("background:#ffffff;")
        self.bottom_frame2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        bottom_layout.addWidget(self.bottom_frame2, 1)

        self.bottom_frame3 = QFrame()
        self.bottom_frame3.setFrameShape(QFrame.Box)
        self.bottom_frame3.setStyleSheet("background:#ffffff;")
        self.bottom_frame3.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        bottom_layout.addWidget(self.bottom_frame3, 1)

        th_widget = TemperatureHumidityWidget(temp=21.8, hum=52)
        th_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        fr_layout = QVBoxLayout(self.bottom_frame3)
        fr_layout.setContentsMargins(8,8,8,8)
        fr_layout.addWidget(th_widget)

        right_layout.addWidget(self.bottom_row, 2)

        self.main_layout.addWidget(right_panel, stretch=1)

    def button_action(self, num):
        if num in self.actions:
            self.actions[num]()

    def action1(self):
        print("Bouton 1 cliqué!")

    def action2(self):
        self.mapisvisible = not self.mapisvisible   
        if self.mapisvisible:
            self.top_row.hide()
            self.bottom_row.hide()
            self.web_frame.show()
        else:
            self.web_frame.hide()
            self.top_row.show()
            self.bottom_row.show()

    def action3(self):
        print("Bouton 3 cliqué!")

    def action4(self):
        print("Bouton 4 cliqué!")

    def action5(self):
        print("Bouton 5 cliqué!")

    def action6(self):
        print("Bouton 6 cliqué!")

    def action7(self):
        print("Bouton 7 cliqué!")
        
    def action8(self):
        print("Bouton 8 cliqué!")

    def action9(self):
        print("Bouton 9 cliqué!")

    def action10(self):
        print("Bouton 10 cliqué!")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())
    