""" Lemontropia Suite - Simple Healing Selector Quick healing tool selection for cost configuration. """ import logging from decimal import Decimal from PyQt6.QtWidgets import ( QDialog, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QLineEdit, QListWidget, QListWidgetItem, QMessageBox, QFormLayout, QGroupBox ) from PyQt6.QtCore import Qt, pyqtSignal from core.nexus_full_api import get_nexus_api logger = logging.getLogger(__name__) class HealingSelectorDialog(QDialog): """Simple dialog to select healing tool for cost tracking.""" def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle("Select Healing Tool") self.setMinimumSize(500, 400) self.selected_healing = None self._healing_tools = [] self._setup_ui() self._load_healing_tools() def _setup_ui(self): """Setup simple UI.""" layout = QVBoxLayout(self) layout.setSpacing(10) # Search search_layout = QHBoxLayout() search_layout.addWidget(QLabel("Search:")) self.search_edit = QLineEdit() self.search_edit.setPlaceholderText("Type healing tool name...") self.search_edit.textChanged.connect(self._on_search) search_layout.addWidget(self.search_edit) layout.addLayout(search_layout) # Healing list self.healing_list = QListWidget() self.healing_list.itemClicked.connect(self._on_select) self.healing_list.itemDoubleClicked.connect(self._on_double_click) layout.addWidget(self.healing_list) # Preview preview_group = QGroupBox("Selected Healing Tool") preview_layout = QFormLayout(preview_group) self.preview_name = QLabel("None") preview_layout.addRow("Name:", self.preview_name) self.preview_heal = QLabel("-") preview_layout.addRow("Heal Amount:", self.preview_heal) self.preview_decay = QLabel("-") preview_layout.addRow("Decay:", self.preview_decay) self.preview_economy = QLabel("-") preview_layout.addRow("Economy:", self.preview_economy) self.preview_cost = QLabel("-") self.preview_cost.setStyleSheet("font-weight: bold; color: #7FFF7F;") preview_layout.addRow("Cost/Heal:", self.preview_cost) layout.addWidget(preview_group) # Buttons button_layout = QHBoxLayout() button_layout.addStretch() self.ok_btn = QPushButton("Select") self.ok_btn.clicked.connect(self.accept) self.ok_btn.setEnabled(False) self.ok_btn.setStyleSheet(""" QPushButton { background-color: #2E7D32; color: white; padding: 8px 16px; font-weight: bold; } QPushButton:disabled { background-color: #333; color: #666; } """) button_layout.addWidget(self.ok_btn) cancel_btn = QPushButton("Cancel") cancel_btn.clicked.connect(self.reject) button_layout.addWidget(cancel_btn) layout.addLayout(button_layout) def _load_healing_tools(self): """Load healing tools from API.""" try: nexus = get_nexus_api() # Get both medical tools and chips tools = nexus.get_all_healing_tools() chips = nexus.get_all_healing_chips() self._healing_tools = tools + chips # Sort by name self._healing_tools.sort(key=lambda h: h.name.lower()) self._populate_list(self._healing_tools) except Exception as e: logger.error(f"Failed to load healing tools: {e}") QMessageBox.warning(self, "Error", f"Failed to load healing tools: {e}") def _populate_list(self, healing_tools): """Populate list with healing tools.""" self.healing_list.clear() for tool in healing_tools: # Get decay and heal amount decay_pec = Decimal(str(getattr(tool, 'decay', 0))) heal_min = getattr(tool, 'heal_min', 0) heal_max = getattr(tool, 'heal_max', 0) heal_avg = (Decimal(str(heal_min)) + Decimal(str(heal_max))) / Decimal("2") cost_per_heal = decay_pec / Decimal("100") item = QListWidgetItem(f"{tool.name} (💚 {heal_avg:.0f} HP)") item.setData(Qt.ItemDataRole.UserRole, tool) # Tooltip tooltip_parts = [ f"Heal: {heal_min}-{heal_max} HP", f"Decay: {decay_pec} PEC", ] if hasattr(tool, 'economy') and tool.economy: tooltip_parts.append(f"Economy: {tool.economy} HP/PEC") tooltip_parts.append(f"Cost/Heal: {cost_per_heal:.4f} PED") item.setToolTip("\n".join(tooltip_parts)) self.healing_list.addItem(item) def _on_search(self, text): """Filter healing tools by search text.""" if not text: self._populate_list(self._healing_tools) return text_lower = text.lower() filtered = [h for h in self._healing_tools if text_lower in h.name.lower()] self._populate_list(filtered) def _on_select(self, item): """Update preview when healing tool selected.""" tool = item.data(Qt.ItemDataRole.UserRole) if not tool: return self.selected_healing = tool # Get values decay_pec = Decimal(str(getattr(tool, 'decay', 0))) heal_min = getattr(tool, 'heal_min', 0) heal_max = getattr(tool, 'heal_max', 0) heal_avg = (Decimal(str(heal_min)) + Decimal(str(heal_max))) / Decimal("2") cost_per_heal = decay_pec / Decimal("100") # Update preview self.preview_name.setText(tool.name) self.preview_heal.setText(f"{heal_min}-{heal_max} HP (avg {heal_avg:.0f})") self.preview_decay.setText(f"{decay_pec} PEC") economy_text = "-" if hasattr(tool, 'economy') and tool.economy: economy_text = f"{tool.economy} HP/PEC" self.preview_economy.setText(economy_text) self.preview_cost.setText(f"{cost_per_heal:.4f} PED") self.ok_btn.setEnabled(True) def _on_double_click(self, item): """Double-click to select immediately.""" self._on_select(item) self.accept() def get_selected_healing(self): """Get the selected healing tool as a dict for the simplified system.""" if not self.selected_healing: return None decay_pec = Decimal(str(getattr(self.selected_healing, 'decay', 0))) heal_min = getattr(self.selected_healing, 'heal_min', 0) heal_max = getattr(self.selected_healing, 'heal_max', 0) heal_avg = (Decimal(str(heal_min)) + Decimal(str(heal_max))) / Decimal("2") return { 'name': self.selected_healing.name, 'api_id': getattr(self.selected_healing, 'id', None), 'decay_pec': decay_pec, 'heal_amount': heal_avg, }