from pathlib import Path

from ..common.master_file_widget import MasterFileWidget
from ..common.tuple_input_widget import TupleInputWidget
from ..common.utils import NoWheelDoubleSpinBox
from .base_parameter_control import BaseParameterControl


class IndexerControlBox(BaseParameterControl):
    def __init__(self, parent=None):
        super().__init__(title="Index Filter Settings", parent=parent)

        self.ringIndexTuple = TupleInputWidget()
        self.latticeFile = MasterFileWidget("Select Lattice File")
        self.latticeFile.setAutoDefault(False)
        self.latticeFile.setNameFilters(
            ["Par Files (*.par)", "CIF Files (*.cif)", "JSON Files (*.json)"]
        )

        self.dsTol = NoWheelDoubleSpinBox()
        self.dsTol.setMinimum(0)
        self.wavelength = NoWheelDoubleSpinBox()
        self.wavelength.setMinimum(0)

        self.settingsLayout.addRow("Select Rings", self.ringIndexTuple)
        self.settingsLayout.addRow("Lattice File", self.latticeFile)
        self.settingsLayout.addRow("Reciprocal Distance Tol.", self.dsTol)
        self.settingsLayout.addRow("Wavelength", self.wavelength)

    def validate(self):
        if not isinstance(float(self.dsTol.value()), float):
            raise FileNotFoundError(f"[{__name__}] ds tolerance not in correct format")
        if not Path(str(self.latticeFile.getText().strip())).exists():
            raise FileNotFoundError(
                f"[{__name__}] lattice file: {str(self.latticeFile.getText().strip())} not found"
            )
