from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Qt, QPoint, QRectF
from PySide6.QtGui import QPainter, QColor, QPen, QFont, QPolygon

class CircularGauge(QWidget):
    def __init__(self, title="SPEED", min_val=0, max_val=120, unit="km/h", color="#00E5FF"):
        super().__init__()
        self.value = 0
        self.target_value = 0 # 목표 속도 (빨간 바늘 등)
        self.min_val = min_val
        self.max_val = max_val
        self.title = title
        self.unit = unit
        self.accent_color = QColor(color)
        self.setMinimumSize(200, 200)

    def set_value(self, val, target=None):
        self.value = max(self.min_val, min(val, self.max_val))
        if target is not None:
            self.target_value = max(self.min_val, min(target, self.max_val))
        self.update()

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        
        w, h = self.width(), self.height()
        side = min(w, h)
        painter.translate(w/2, h/2)
        painter.scale(side/200.0, side/200.0)

        # 1. 배경
        painter.setPen(QPen(QColor("#222"), 4))
        painter.drawEllipse(-90, -90, 180, 180)

        # 2. 눈금 (Scale)
        start_angle = 135
        span_angle = 270
        
        painter.setPen(QPen(Qt.white, 2))
        painter.setFont(QFont("Arial", 8))
        
        step = (self.max_val - self.min_val) / 10
        for i in range(11):
            val = self.min_val + i * step
            angle = start_angle + (i * (span_angle / 10))
            
            painter.save()
            painter.rotate(angle)
            painter.drawLine(80, 0, 90, 0) # 눈금
            painter.restore()
            
            # 숫자 (삼각함수로 위치 계산 생략하고 간략히 처리하거나 추가 구현)
        
        # 3. 메인 바늘 (Value)
        val_pct = (self.value - self.min_val) / (self.max_val - self.min_val)
        angle = start_angle + (val_pct * span_angle)
        
        painter.save()
        painter.rotate(angle)
        painter.setBrush(self.accent_color)
        painter.setPen(Qt.NoPen)
        painter.drawConvexPolygon([QPoint(0, -3), QPoint(0, 3), QPoint(85, 0)])
        painter.restore()
        
        # 4. 텍스트 표시
        painter.setPen(self.accent_color)
        painter.setFont(QFont("Arial", 16, QFont.Bold))
        painter.drawText(QRectF(-50, 20, 100, 30), Qt.AlignCenter, f"{self.value:.1f}")
        
        painter.setPen(Qt.gray)
        painter.setFont(QFont("Arial", 9))
        painter.drawText(QRectF(-50, 50, 100, 20), Qt.AlignCenter, self.unit)
        painter.drawText(QRectF(-50, -60, 100, 20), Qt.AlignCenter, self.title)