# ui/panels/xray_panel.py
from PyQt6.QtWidgets import QGroupBox, QVBoxLayout, QHBoxLayout, QLabel, QComboBox, QGridLayout, QCheckBox, QDoubleSpinBox, QPushButton
from core.constants import WAVE_MAP

class XRaySourcePanel(QGroupBox):
    """X線源とゴーストピーク予測の設定を担当するパネル"""
    def __init__(self, parent=None):
        super().__init__("X線源 / ゴーストピーク予測", parent)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout()
        
        # --- 主波長選択 ---
        main_layout = QHBoxLayout()
        main_layout.addWidget(QLabel("主波長:"))
        self.combo_main_wave = QComboBox()
        # core.constants から取得したキーを追加
        self.combo_main_wave.addItems(["CuKα1 (1.5406 Å)", "CuKα 平均 (1.5418 Å)"])
        main_layout.addWidget(self.combo_main_wave, stretch=1)
        layout.addLayout(main_layout)
        
        layout.addWidget(QLabel("副波長(ゴースト)の予測線を表示:"))
        
        # --- ゴーストピークのチェックボックス (2x2グリッド) ---
        grid = QGridLayout()
        self.chk_ka2 = QCheckBox("CuKα2 (Orange)")
        self.chk_kb = QCheckBox("CuKβ (Green)")
        self.chk_wl1 = QCheckBox("W Lα1 (Magenta)")
        self.chk_wl2 = QCheckBox("W Lα2 (Purple)")
        
        grid.addWidget(self.chk_ka2, 0, 0)
        grid.addWidget(self.chk_kb, 0, 1)
        grid.addWidget(self.chk_wl1, 1, 0)
        grid.addWidget(self.chk_wl2, 1, 1)
        layout.addLayout(grid)

        # --- カスタム波長 ---
        custom_layout = QHBoxLayout()
        self.chk_custom_wave = QCheckBox("カスタム波長(Å):")
        self.spin_custom_wave = QDoubleSpinBox()
        self.spin_custom_wave.setDecimals(5)
        self.spin_custom_wave.setRange(0.1, 5.0)
        self.spin_custom_wave.setSingleStep(0.01)
        self.spin_custom_wave.setValue(1.54060)
        
        custom_layout.addWidget(self.chk_custom_wave)
        custom_layout.addWidget(self.spin_custom_wave)
        custom_layout.addStretch(1)
        layout.addLayout(custom_layout)

        # --- 高次反射ハイライトボタン ---
        self.btn_higher_order = QPushButton("ホバー中ピークの高次反射 (x2, x3...) をハイライト")
        self.btn_higher_order.setCheckable(True)
        layout.addWidget(self.btn_higher_order)
        
        self.setLayout(layout)