from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QFrame, QGraphicsDropShadowEffect
from PySide6.QtCore import Qt, QPoint, QTimer
from PySide6.QtGui import QFont, QColor
from i18n import t
from palworld_aio import constants
class PlayerHoverOverlay(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('playerHoverOverlay')
        self.setWindowFlags(Qt.ToolTip | Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setAttribute(Qt.WA_ShowWithoutActivating)
        self._setup_ui()
        self._hide_timer = QTimer(self)
        self._hide_timer.setSingleShot(True)
        self._hide_timer.timeout.connect(self._do_hide)
    def _setup_ui(self):
        self.container = QFrame(self)
        self.container.setObjectName('hoverOverlayContainer')
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.container)
        inner_layout = QVBoxLayout(self.container)
        inner_layout.setContentsMargins(12, 10, 12, 10)
        inner_layout.setSpacing(6)
        self.name_label = QLabel()
        self.name_label.setObjectName('hoverNameLabel')
        self.name_label.setFont(QFont(constants.FONT_FAMILY, 11, QFont.Bold))
        inner_layout.addWidget(self.name_label)
        self.uid_label = QLabel()
        self.uid_label.setObjectName('hoverDetailLabel')
        self.uid_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.uid_label)
        self.level_label = QLabel()
        self.level_label.setObjectName('hoverDetailLabel')
        self.level_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.level_label)
        self.guild_label = QLabel()
        self.guild_label.setObjectName('hoverDetailLabel')
        self.guild_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.guild_label)
        self.pals_label = QLabel()
        self.pals_label.setObjectName('hoverDetailLabel')
        self.pals_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.pals_label)
        self.last_seen_label = QLabel()
        self.last_seen_label.setObjectName('hoverDetailLabel')
        self.last_seen_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.last_seen_label)
        self.coords_label = QLabel()
        self.coords_label.setObjectName('hoverDetailLabel')
        self.coords_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.coords_label)
        shadow = QGraphicsDropShadowEffect(self)
        shadow.setBlurRadius(15)
        shadow.setOffset(2, 2)
        shadow.setColor(QColor(0, 0, 0, 100))
        self.container.setGraphicsEffect(shadow)
        self.container.setStyleSheet('\n            QFrame#hoverOverlayContainer {\n                background: rgba(18,20,24,0.95);\n                border: 1px solid rgba(0, 200, 120, 0.5);\n                border-radius: 8px;\n            }\n            QLabel#hoverNameLabel {\n                color: #00C878;\n            }\n            QLabel#hoverDetailLabel {\n                color: #a0aec0;\n            }\n        ')
    def show_for_player(self, player_data: dict, global_pos: QPoint):
        self._hide_timer.stop()
        player_name = player_data.get('player_name', 'Unknown')
        player_uid = player_data.get('player_uid', '')
        player_level = player_data.get('level', '?')
        last_seen = player_data.get('last_seen', 'Unknown')
        pal_count = player_data.get('pal_count', 0)
        guild_name = player_data.get('guild_name', '')
        coords = player_data.get('coords', (0, 0))
        self.name_label.setText(player_name)
        self.name_label.show()
        self.uid_label.setText(f"{(t('player.hover.uid') if t else 'UID:')} {player_uid}")
        self.uid_label.show()
        self.level_label.setText(f"{(t('player.hover.level') if t else 'Level:')} {player_level}")
        self.level_label.show()
        if guild_name:
            self.guild_label.setText(f"{(t('player.hover.guild') if t else 'Guild:')} {guild_name}")
            self.guild_label.show()
        else:
            self.guild_label.hide()
        self.pals_label.setText(f"{(t('player.hover.pals') if t else 'Pals:')} {pal_count}")
        self.pals_label.show()
        self.last_seen_label.setText(f"{(t('player.hover.last_seen') if t else 'Last Seen:')} {last_seen}")
        self.last_seen_label.show()
        self.coords_label.setText(f"{(t('player.hover.location') if t else 'Location:')} X:{int(coords[0])},Y:{int(coords[1])}")
        self.coords_label.show()
        self.adjustSize()
        offset_x = 20
        offset_y = -self.height() // 2
        new_pos = QPoint(global_pos.x() + offset_x, global_pos.y() + offset_y)
        self.move(new_pos)
        self.show()
        self.raise_()
    def hide_overlay(self):
        self._hide_timer.start(50)
    def _do_hide(self):
        self.hide()
    def cancel_hide(self):
        self._hide_timer.stop()