# srcs/views/device_connection_popup.py
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QListWidget, QListWidgetItem
from PyQt5.QtSvg import QSvgWidget
from PyQt5.QtCore import Qt
from ..config import UXUI_ASSETS, SECONDARY_COLOR, BUTTON_STYLE
import os

class DeviceConnectionPopup(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.init_ui()

    def init_ui(self):
        # 設定彈出視窗的大小和樣式
        self.setFixedSize(int(self.parent.width() * 0.67), int(self.parent.height() * 0.67))
        self.setStyleSheet(f"""
            QWidget {{
                background-color: {SECONDARY_COLOR};
                border-radius: 20px;
                padding: 20px;
            }}
        """)

        layout = QVBoxLayout(self)

        # 標題列
        title_layout = QHBoxLayout()
        title_layout.setAlignment(Qt.AlignCenter)

        # 容器來放置圖示和標籤
        title_container = QWidget()
        container_layout = QHBoxLayout(title_container)
        container_layout.setSpacing(10)

        # 添加圖示
        device_icon = QSvgWidget(os.path.join(UXUI_ASSETS, "Assets_svg/ic_window_device.svg"))
        device_icon.setFixedSize(35, 35)
        container_layout.addWidget(device_icon)

        # 添加標籤
        popup_label = QLabel("Device Connection")
        popup_label.setStyleSheet("color: white; font-size: 25px;")
        container_layout.addWidget(popup_label)

        # 設置容器的對齊方式
        container_layout.setAlignment(Qt.AlignCenter)

        # 將容器添加到標題布局中
        title_layout.addWidget(title_container)
        layout.addLayout(title_layout)

        # 設備列表
        self.device_list_widget = QListWidget(self)
        layout.addWidget(self.device_list_widget)

        # 按鈕區域
        button_layout = QHBoxLayout()

        refresh_button = QPushButton("Refresh")
        refresh_button.clicked.connect(self.parent.refresh_devices)
        refresh_button.setStyleSheet(BUTTON_STYLE)
        button_layout.addWidget(refresh_button)

        done_button = QPushButton("Done")
        done_button.setStyleSheet(BUTTON_STYLE)
        done_button.clicked.connect(self.parent.hide_device_popup)
        button_layout.addWidget(done_button)

        layout.addLayout(button_layout)
