"""
Dedicated histogram dialog for EMDS results.
"""

from __future__ import annotations

from typing import Optional, Sequence

import pandas as pd
from .qt_compat import QDialog, QHBoxLayout, QPushButton, QVBoxLayout

from .histogram_widget import HistogramWidget


class ResultsHistogramDialog(QDialog):
    """Reusable histogram dialog for numeric result analysis."""

    def __init__(
        self,
        dataframe: Optional[pd.DataFrame],
        initial_columns: Optional[Sequence[str]] = None,
        title: str = "Histogram",
        subtitle: str = "",
        parent=None,
    ):
        super().__init__(parent)
        self.dataframe = dataframe.copy() if dataframe is not None else pd.DataFrame()
        self.initial_columns = list(initial_columns or [])
        self.title_text = title
        self.subtitle_text = subtitle

        self.setWindowTitle(self.title_text)
        self.resize(1100, 760)

        self._init_ui()

    def _init_ui(self) -> None:
        """Build the dialog UI."""
        layout = QVBoxLayout()
        self.setLayout(layout)

        self.histogram_widget = HistogramWidget(
            self.dataframe,
            initial_columns=self.initial_columns,
            title=self.title_text,
            subtitle=self.subtitle_text,
            parent=self,
        )
        layout.addWidget(self.histogram_widget, 1)

        button_row = QHBoxLayout()
        layout.addLayout(button_row)
        button_row.addStretch()

        close_button = QPushButton("Close")
        close_button.clicked.connect(self.accept)
        button_row.addWidget(close_button)
