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


class Trjconv(GromacsCommand):
    """
    Wrapper for the GROMACS ``trjconv`` trajectory conversion command.

    This class converts a structure or trajectory file to PDB format
    using a provided run input file (``.tpr``). By default, the first
    index group is selected via standard input.

    :param structure:
        Input structure or trajectory file.
    :type structure: pathlib.Path
    :param tpr:
        Run input file used for atom and topology information.
    :type tpr: pathlib.Path
    :param output_pdb:
        Output structure file in PDB format.
    :type output_pdb: pathlib.Path
    :param kwargs:
        Additional keyword arguments forwarded to
        :class:`GromacsCommand`.
    :type kwargs: dict
    """

    def __init__(
        self,
        structure: Path,
        tpr: Path,
        output_pdb: Path,
        **kwargs,
    ):
        self.structure = structure
        self.tpr = tpr
        self.output_pdb = output_pdb
        super().__init__(stdin="0\n", **kwargs)

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

        The command converts the input structure or trajectory to
        PDB format using topology information from the ``.tpr`` file.

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

        return [
            self.gmx_bin, "trjconv",
            "-s", str(self.tpr),
            "-f", str(self.structure),
            "-o", str(self.output_pdb)
        ]

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

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

        return [self.output_pdb]
