from pathlib import Path
from viennaptm.gromacs.gromacs_command import GromacsCommand


class Grompp(GromacsCommand):
    """
    Wrapper for the GROMACS ``grompp`` preprocessor command.

    This command preprocesses a molecular dynamics parameter file,
    structure, and topology to generate a binary run input file
    (``.tpr``) for subsequent simulation steps.

    Common warnings are tolerated by allowing a fixed maximum number
    of warnings during preprocessing.

    :param mdp:
        Path to the molecular dynamics parameter (MDP) file.
    :type mdp: pathlib.Path
    :param structure:
        Path to the input structure file (e.g. ``.gro``).
    :type structure: pathlib.Path
    :param topology:
        Path to the topology file (e.g. ``.top``).
    :type topology: pathlib.Path
    :param tpr:
        Path to the output binary run input file (``.tpr``).
    :type tpr: pathlib.Path
    :param kwargs:
        Additional keyword arguments forwarded to
        :class:`GromacsCommand`.
    :type kwargs: dict
    """

    def __init__(
        self,
        mdp: Path,
        structure: Path,
        topology: Path,
        tpr: Path,
        **kwargs
    ):
        self.mdp = mdp
        self.structure = structure
        self.topology = topology
        self.tpr = tpr
        super().__init__(**kwargs)

    def build_gromacs_cmd(self):
        """
        Construct the ``gmx grompp`` command invocation.

        This method assembles the command-line arguments required to
        preprocess input files and generate a ``.tpr`` file.

        :return:
            Command-line argument list suitable for execution.
        :rtype: list[str]
        """

        return [
            self.gmx_bin, "grompp",
            "-f", str(self.mdp),
            "-c", str(self.structure),
            "-p", str(self.topology),
            "-o", str(self.tpr),
            "-maxwarn", "50"
        ]

    def expected_outputs(self):
        """
        Declare the files expected to be generated by this command.

        :return:
            List containing the generated ``.tpr`` file path.
        :rtype: list[pathlib.Path]
        """

        return [self.tpr]
