# ui/panels/settings_panel.py
from PyQt6.QtWidgets import (
    QGroupBox, QVBoxLayout, QGridLayout, QHBoxLayout, 
    QLabel, QComboBox, QDoubleSpinBox, QCheckBox, 
    QLineEdit, QPushButton, QFrame
)
from PyQt6.QtCore import Qt

class GraphSettingsPanel(QGroupBox):
    def __init__(self, aspect_ratios, fixed_paper_sizes, parent=None):
        super().__init__("グラフ設定", parent)
        self.init_ui(aspect_ratios, fixed_paper_sizes)

    def init_ui(self, aspect_ratios, fixed_paper_sizes):
        layout = QVBoxLayout()

        # --- Y軸スケール & プロット高さ ---
        scale_layout = QGridLayout()
        scale_layout.addWidget(QLabel("データY軸スケール:"), 0, 0)
        self.combo_data_y_scale = QComboBox()
        self.combo_data_y_scale.addItems(["線形", "平方根", "対数"])
        scale_layout.addWidget(self.combo_data_y_scale, 0, 1)

        scale_layout.addWidget(QLabel("リファレンスYスケール:"), 1, 0)
        self.combo_ref_y_scale = QComboBox()
        self.combo_ref_y_scale.addItems(["線形", "平方根", "対数", "定数"])
        scale_layout.addWidget(self.combo_ref_y_scale, 1, 1)

        scale_layout.addWidget(QLabel("プロット高さ係数:"), 2, 0)
        self.spin_scaling_factor = QDoubleSpinBox()
        self.spin_scaling_factor.setRange(0.1, 1.0)
        self.spin_scaling_factor.setSingleStep(0.05)
        self.spin_scaling_factor.setValue(0.8)
        scale_layout.addWidget(self.spin_scaling_factor, 2, 1)
        layout.addLayout(scale_layout)

        # --- 枠・メモリ太さ ---
        thickness_layout = QHBoxLayout()
        thickness_layout.addWidget(QLabel("枠・目盛の太さ:"))
        self.spin_axes_linewidth = QDoubleSpinBox()
        self.spin_axes_linewidth.setRange(0.1, 5.0)
        self.spin_axes_linewidth.setSingleStep(0.1)
        self.spin_axes_linewidth.setValue(0.8)
        thickness_layout.addWidget(self.spin_axes_linewidth)
        layout.addLayout(thickness_layout)

        # --- 表示要素 (チェックボックス) ---
        visibility_layout = QGridLayout()
        self.check_y_ticks_visible = QCheckBox("Y軸数値")
        self.check_ref_y_value_visible = QCheckBox("Ref Y数値")
        self.check_ref_y_label_visible = QCheckBox("Ref Yラベル")
        self.check_legend_visible = QCheckBox("凡例")
        self.check_replace_non_positive = QCheckBox("1以下のデータを無視して直線で結ぶ")

        # visibility_layout (check_replace_non_positive の上あたりに追加)
        self.check_ref_baseline_visible = QCheckBox("Ref ベースライン")
        self.check_ref_baseline_visible.setChecked(True) # デフォルト表示
        self.check_ref_x_ticks_visible = QCheckBox("Ref X軸目盛")
        self.check_ref_x_ticks_visible.setChecked(True)
        self.check_ref_y_ticks_visible = QCheckBox("Ref Y軸目盛")
        self.check_ref_y_ticks_visible.setChecked(True)

        # visibility_layout (check_ref_x_ticks_visible の横あたりに追加)
        self.check_hide_divider = QCheckBox("中央区切り線を隠す")
        self.check_hide_divider.setChecked(False) # デフォルトは表示

        visibility_layout.addWidget(self.check_y_ticks_visible, 0, 0)
        visibility_layout.addWidget(self.check_ref_y_value_visible, 0, 1)
        visibility_layout.addWidget(self.check_ref_y_label_visible, 0, 2)
        visibility_layout.addWidget(self.check_legend_visible, 0, 3)
        visibility_layout.addWidget(self.check_ref_baseline_visible, 1, 0)
        visibility_layout.addWidget(self.check_ref_x_ticks_visible, 1, 1)
        visibility_layout.addWidget(self.check_ref_y_ticks_visible, 1, 2)
        visibility_layout.addWidget(self.check_hide_divider, 2, 0, 2, 4)
        visibility_layout.addWidget(self.check_replace_non_positive, 4, 0, 2, 4)    
        layout.addLayout(visibility_layout)

        # --- レイアウト設定 ---
        layout.addWidget(self._create_separator())
        layout.addWidget(QLabel("<b>レイアウト</b>"))
        
        self.check_single_plot_mode = QCheckBox("1画面モード (リファレンス非表示)")
        layout.addWidget(self.check_single_plot_mode)

        plot_ratio_layout = QHBoxLayout()
        plot_ratio_layout.addWidget(QLabel("プロット比率 (上):"))
        self.spin_plot_ratio = QDoubleSpinBox()
        self.spin_plot_ratio.setRange(0, 100.0); self.spin_plot_ratio.setSuffix("%")
        self.spin_plot_ratio.setValue(80.0)
        plot_ratio_layout.addWidget(self.spin_plot_ratio)
        layout.addLayout(plot_ratio_layout)

        aspect_layout = QHBoxLayout()
        aspect_layout.addWidget(QLabel("グラフ縦横比:"))
        self.combo_aspect_ratio = QComboBox()
        self.combo_aspect_ratio.addItems(list(aspect_ratios.keys()) + list(fixed_paper_sizes.keys()))
        aspect_layout.addWidget(self.combo_aspect_ratio)
        layout.addLayout(aspect_layout)

        size_layout = QHBoxLayout()
        size_layout.addWidget(QLabel("サイズ(cm):"))
        self.spin_width_cm = QDoubleSpinBox(); self.spin_width_cm.setRange(1, 50)
        self.spin_height_cm = QDoubleSpinBox(); self.spin_height_cm.setRange(1, 50)
        self.btn_apply_size_cm = QPushButton("適用")
        size_layout.addWidget(self.spin_width_cm); size_layout.addWidget(self.spin_height_cm)
        size_layout.addWidget(self.btn_apply_size_cm)
        layout.addLayout(size_layout)

        # --- 軸範囲・ラベル設定 (X/Y) ---
        layout.addWidget(self._create_separator())
        layout.addWidget(QLabel("<b>軸範囲・ラベル</b>"))
        
        # X軸
        x_range_layout = QHBoxLayout()
        x_range_layout.addWidget(QLabel("X範囲:"))
        self.edit_x_min = QLineEdit(); self.edit_x_max = QLineEdit()
        self.btn_apply_x_zoom = QPushButton("適用")
        x_range_layout.addWidget(self.edit_x_min); x_range_layout.addWidget(self.edit_x_max); x_range_layout.addWidget(self.btn_apply_x_zoom)
        layout.addLayout(x_range_layout)

        x_tick_layout = QHBoxLayout()
        x_tick_layout.addWidget(QLabel("X目盛間隔:"))
        self.edit_x_tick_interval = QLineEdit()
        self.btn_apply_x_tick = QPushButton("適用")
        x_tick_layout.addWidget(self.edit_x_tick_interval); x_tick_layout.addWidget(self.btn_apply_x_tick)
        layout.addLayout(x_tick_layout)

        self.edit_x_label = QLineEdit(); self.edit_x_label.setPlaceholderText("X軸ラベル")
        layout.addWidget(self.edit_x_label)
        self.edit_data_y_label = QLineEdit(); self.edit_data_y_label.setPlaceholderText("データY軸ラベル")
        layout.addWidget(self.edit_data_y_label)

        # --- Y軸範囲 (データプロット用) ---
        y_range_layout = QHBoxLayout()
        y_range_layout.addWidget(QLabel("Y範囲:"))
        self.edit_y_min = QLineEdit()
        self.edit_y_max = QLineEdit()
        # プレースホルダ（うっすら見える文字）を設定しておくと親切です
        self.edit_y_min.setPlaceholderText("最小")
        self.edit_y_max.setPlaceholderText("最大")
        
        y_range_layout.addWidget(self.edit_y_min)
        y_range_layout.addWidget(QLabel("–"))
        y_range_layout.addWidget(self.edit_y_max)
        
        self.btn_apply_y_zoom = QPushButton("適用")
        y_range_layout.addWidget(self.btn_apply_y_zoom)
        layout.addLayout(y_range_layout)
        
        # --- Ref Y軸範囲 (リファレンスプロット用) ---
        ref_y_range_layout = QHBoxLayout()
        ref_y_range_layout.addWidget(QLabel("Ref Y範囲:"))
        self.edit_ref_y_min = QLineEdit()
        self.edit_ref_y_max = QLineEdit()
        self.edit_ref_y_min.setPlaceholderText("最小")
        self.edit_ref_y_max.setPlaceholderText("最大")
        
        ref_y_range_layout.addWidget(self.edit_ref_y_min)
        ref_y_range_layout.addWidget(QLabel("–"))
        ref_y_range_layout.addWidget(self.edit_ref_y_max)
        
        self.btn_apply_ref_y_zoom = QPushButton("適用")
        ref_y_range_layout.addWidget(self.btn_apply_ref_y_zoom)
        layout.addLayout(ref_y_range_layout)

        # 最後にズームリセットボタンを追加
        self.btn_reset_zoom = QPushButton("ズームリセット")
        layout.addWidget(self.btn_reset_zoom)

        self.setLayout(layout)
        
    def _create_separator(self):
        line = QFrame()
        line.setFrameShape(QFrame.Shape.HLine); line.setFrameShadow(QFrame.Shadow.Sunken)
        return line