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 BaseHoverOverlay(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName('baseHoverOverlay')
        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.guild_label = QLabel()
        self.guild_label.setObjectName('hoverGuildLabel')
        self.guild_label.setFont(QFont(constants.FONT_FAMILY, 11, QFont.Bold))
        inner_layout.addWidget(self.guild_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.leader_label = QLabel()
        self.leader_label.setObjectName('hoverDetailLabel')
        self.leader_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.leader_label)
        self.members_label = QLabel()
        self.members_label.setObjectName('hoverDetailLabel')
        self.members_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.members_label)
        self.bases_label = QLabel()
        self.bases_label.setObjectName('hoverDetailLabel')
        self.bases_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.bases_label)
        self.base_id_label = QLabel()
        self.base_id_label.setObjectName('hoverDetailLabel')
        self.base_id_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.base_id_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)
        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.guild_id_label = QLabel()
        self.guild_id_label.setObjectName('hoverDetailLabel')
        self.guild_id_label.setFont(QFont(constants.FONT_FAMILY, 9))
        inner_layout.addWidget(self.guild_id_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(125,211,252,0.3);\n                border-radius: 8px;\n            }\n            QLabel#hoverGuildLabel {\n                color: #7DD3FC;\n            }\n            QLabel#hoverDetailLabel {\n                color: #a0aec0;\n            }\n        ')
    def show_for_base(self, base_data: dict, global_pos: QPoint):
        self._hide_timer.stop()
        guild_name = base_data.get('guild_name', 'Unknown')
        guild_level = base_data.get('guild_level', 1)
        leader_name = base_data.get('leader_name', 'Unknown')
        member_count = base_data.get('member_count', 0)
        total_bases = base_data.get('total_bases', 0)
        base_position = base_data.get('base_position', 1)
        base_id = str(base_data.get('base_id', ''))
        coords = base_data.get('coords', (0, 0))
        self.guild_label.setText(guild_name)
        self.guild_label.show()
        self.level_label.setText(f"{(t('map.info.level') if t else 'Level:')} {guild_level}")
        self.level_label.show()
        self.leader_label.setText(f"{(t('map.info.admin') if t else 'Admin:')} {leader_name}")
        self.leader_label.show()
        self.members_label.setText(f"{(t('map.info.members') if t else 'Members:')} {member_count}")
        self.members_label.show()
        self.bases_label.setText(f"{(t('map.info.base_camps') if t else 'Base Camps:')} {base_position}/{total_bases}")
        self.bases_label.show()
        self.base_id_label.setText(f"{(t('map.info.base_id') if t else 'Base ID:')} {base_id}")
        self.base_id_label.show()
        self.coords_label.setText(f"{(t('map.info.location') if t else 'Location:')} X:{int(coords[0])},Y:{int(coords[1])}")
        self.coords_label.show()
        self.uid_label.hide()
        self.guild_id_label.hide()
        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 show_for_player(self, player_data: dict, global_pos: QPoint):
        self._hide_timer.stop()
        player_name = player_data.get('player_name', 'Unknown')
        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', '')
        guild_id = player_data.get('guild_id', '')
        coords = player_data.get('coords', (0, 0))
        player_uid = player_data.get('player_uid', '')
        self.guild_label.setText(player_name)
        self.guild_label.show()
        self.level_label.setText(f'UID: {player_uid}')
        self.level_label.show()
        self.leader_label.setText(f'Level: {player_level}')
        self.leader_label.show()
        if guild_name:
            self.members_label.setText(f'Guild: {guild_name}')
            self.members_label.show()
        else:
            self.members_label.hide()
        self.bases_label.setText(f'Pals: {pal_count}')
        self.bases_label.show()
        self.base_id_label.setText(f'Last Seen: {last_seen}')
        self.base_id_label.show()
        self.coords_label.setText(f'Location: X:{int(coords[0])},Y:{int(coords[1])}')
        self.coords_label.show()
        self.uid_label.hide()
        self.guild_id_label.hide()
        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()