# Path: acpype/topol.py
# class MolTopol(AbstractTopol):
#     """
#     Class to write topologies and parameters files for several applications.
# 
#     https://ambermd.org/FileFormats.php
# 
#     Parser, take information in AC xyz and top files and convert to objects.
# 
#     Args:
#         acFileXyz
#         acFileTop
# 
#     Returns:
#         molTopol obj or None
#     """
# 
#     def __init__(
#         self,
#         acTopolObj=None,
#         acFileXyz=None,
#         acFileTop=None,
#         debug=False,
#         basename=None,
#         verbose=True,
#         gmx4=False,
#         merge=False,
#         direct=False,
#         is_sorted=False,
#         chiral=False,
#         amb2gmx=False,
#         level=20,
#     ):
#         super().__init__()
#         self.amb2gmx = amb2gmx
#         self.chiral = chiral
#         self.allhdg = False
#         self.debug = debug
#         self.level = level
#         self.gmx4 = gmx4
#         self.merge = merge
#         self.direct = direct
#         self.sorted = is_sorted
#         self.verbose = verbose
#         self.inputFile = acFileTop
# 
#         if not self.verbose:
#             level = 100
#         if self.debug:
#             level = 10
#         self.level = level
# 
#         if acTopolObj:
#             if not acFileXyz:
#                 acFileXyz = acTopolObj.acXyzFileName
#             if not acFileTop:
#                 acFileTop = acTopolObj.acTopFileName
#             self._parent = acTopolObj
#             self.allhdg = self._parent.allhdg
#             self.debug = self._parent.debug
#             self.inputFile = self._parent.inputFile
#         elif not self.amb2gmx:
#             self.amb2gmx = True
#         if not os.path.exists(acFileXyz) or not os.path.exists(acFileTop):
#             self.printError(f"Files '{acFileXyz}' and/or '{acFileTop}' don't exist")
#             self.printError("molTopol object won't be created")
# 
#         self.xyzFileData = open(acFileXyz).readlines()
#         self.topFileData = [x for x in open(acFileTop).readlines() if not x.startswith("%COMMENT")]
#         self.topo14Data = Topology_14()
#         self.topo14Data.read_amber_topology("".join(self.topFileData))
#         self.printDebug("prmtop and inpcrd files loaded")
# 
#         self.getResidueLabel()
#         if len(self.residueLabel) > 1:
#             self.baseName = basename or os.path.splitext(os.path.basename(acFileTop))[0]  # 'solute'
#         else:
#             self.baseName = basename or self.residueLabel[0]  # 3 caps letters
#         if acTopolObj:
#             self.baseName = basename or acTopolObj.baseName
#         self.printDebug("basename defined = '%s'" % self.baseName)
# 
#         self.getAtoms()
# 
#         self.getBonds()
# 
#         self.getAngles()
# 
#         self.getDihedrals()
# 
#         if self.amb2gmx:
#             self.rootDir = os.path.abspath(".")
#             self.homeDir = f"{self.baseName}.amb2gmx"
#             self.makeDir()
#         else:
#             self.getChirals()
# 
#         # Sort atoms for gromacs output. # JDC
#         if self.sorted:
#             self.printMess("Sorting atoms for gromacs ordering.\n")
#             self.sortAtomsForGromacs()


import pytest
from acpype.topol import MolTopol
)
def test_merge(janitor, merge, gaff, n_at, acoef, bcoef, msg):
    molecule = MolTopol(acFileTop=f"ComplexG{gaff}.prmtop", acFileXyz=f"ComplexG{gaff}.inpcrd", debug=True, merge=merge)
    molecule.writeGromacsTopolFiles()
    assert molecule
    assert len(molecule.atomTypesGromacs) == n_at
    assert molecule.atomTypesGromacs[31].ACOEF == acoef
    assert molecule.atomTypesGromacs[31].BCOEF == bcoef
    assert molecule.atomTypesGromacs[31].__repr__() == msg
    janitor.append(molecule.absHomeDir)


@pytest.mark.parametrize(
    ("mol", "n1", "n2", "n3", "n4", "n5", "msg"),
    [("ILDN", 24931, 12230, 577, 0, 47, "<AtomType=C6>"), ("Base", 24931, 12044, 577, 0, 43, "<AtomType=HS>")],
)
def test_ildn(janitor, mol, n1, n2, n3, n4, n5, msg):
    molecule = MolTopol(acFileTop=f"{mol}.prmtop", acFileXyz=f"{mol}.inpcrd", debug=True)
    molecule.writeGromacsTopolFiles()
    assert molecule
    assert len(molecule.atoms) == n1
    assert len(molecule.properDihedrals) == n2
    assert len(molecule.improperDihedrals) == n3
    assert molecule.totalCharge == n4
    assert len(molecule.atomTypes) == n5
    assert molecule.atomTypes[23].__repr__() == msg
    janitor.append(molecule.absHomeDir)


def test_ildn_gmx4_fail(janitor):

# Path: acpype/topol.py
# class MolTopol(AbstractTopol):
#     """
#     Class to write topologies and parameters files for several applications.
# 
#     https://ambermd.org/FileFormats.php
# 
#     Parser, take information in AC xyz and top files and convert to objects.
# 
#     Args:
#         acFileXyz
#         acFileTop
# 
#     Returns:
#         molTopol obj or None
#     """
# 
#     def __init__(
#         self,
#         acTopolObj=None,
#         acFileXyz=None,
#         acFileTop=None,
#         debug=False,
#         basename=None,
#         verbose=True,
#         gmx4=False,
#         merge=False,
#         direct=False,
#         is_sorted=False,
#         chiral=False,
#         amb2gmx=False,
#         level=20,
#     ):
#         super().__init__()
#         self.amb2gmx = amb2gmx
#         self.chiral = chiral
#         self.allhdg = False
#         self.debug = debug
#         self.level = level
#         self.gmx4 = gmx4
#         self.merge = merge
#         self.direct = direct
#         self.sorted = is_sorted
#         self.verbose = verbose
#         self.inputFile = acFileTop
# 
#         if not self.verbose:
#             level = 100
#         if self.debug:
#             level = 10
#         self.level = level
# 
#         if acTopolObj:
#             if not acFileXyz:
#                 acFileXyz = acTopolObj.acXyzFileName
#             if not acFileTop:
#                 acFileTop = acTopolObj.acTopFileName
#             self._parent = acTopolObj
#             self.allhdg = self._parent.allhdg
#             self.debug = self._parent.debug
#             self.inputFile = self._parent.inputFile
#         elif not self.amb2gmx:
#             self.amb2gmx = True
#         if not os.path.exists(acFileXyz) or not os.path.exists(acFileTop):
#             self.printError(f"Files '{acFileXyz}' and/or '{acFileTop}' don't exist")
#             self.printError("molTopol object won't be created")
# 
#         self.xyzFileData = open(acFileXyz).readlines()
#         self.topFileData = [x for x in open(acFileTop).readlines() if not x.startswith("%COMMENT")]
#         self.topo14Data = Topology_14()
#         self.topo14Data.read_amber_topology("".join(self.topFileData))
#         self.printDebug("prmtop and inpcrd files loaded")
# 
#         self.getResidueLabel()
#         if len(self.residueLabel) > 1:
#             self.baseName = basename or os.path.splitext(os.path.basename(acFileTop))[0]  # 'solute'
#         else:
#             self.baseName = basename or self.residueLabel[0]  # 3 caps letters
#         if acTopolObj:
#             self.baseName = basename or acTopolObj.baseName
#         self.printDebug("basename defined = '%s'" % self.baseName)
# 
#         self.getAtoms()
# 
#         self.getBonds()
# 
#         self.getAngles()
# 
#         self.getDihedrals()
# 
#         if self.amb2gmx:
#             self.rootDir = os.path.abspath(".")
#             self.homeDir = f"{self.baseName}.amb2gmx"
#             self.makeDir()
#         else:
#             self.getChirals()
# 
#         # Sort atoms for gromacs output. # JDC
#         if self.sorted:
#             self.printMess("Sorting atoms for gromacs ordering.\n")
#             self.sortAtomsForGromacs()


# Path: tests/test_amb2gmx.py
import pytest
from acpype.topol import MolTopol

)
def test_merge(janitor, merge, gaff, n_at, acoef, bcoef, msg):
    molecule = MolTopol(acFileTop=f"ComplexG{gaff}.prmtop", acFileXyz=f"ComplexG{gaff}.inpcrd", debug=True, merge=merge)
    molecule.writeGromacsTopolFiles()
    assert molecule
    assert len(molecule.atomTypesGromacs) == n_at
    assert molecule.atomTypesGromacs[31].ACOEF == acoef
    assert molecule.atomTypesGromacs[31].BCOEF == bcoef
    assert molecule.atomTypesGromacs[31].__repr__() == msg
    janitor.append(molecule.absHomeDir)


@pytest.mark.parametrize(
    ("mol", "n1", "n2", "n3", "n4", "n5", "msg"),
    [("ILDN", 24931, 12230, 577, 0, 47, "<AtomType=C6>"), ("Base", 24931, 12044, 577, 0, 43, "<AtomType=HS>")],
)
def test_ildn(janitor, mol, n1, n2, n3, n4, n5, msg):
    molecule = MolTopol(acFileTop=f"{mol}.prmtop", acFileXyz=f"{mol}.inpcrd", debug=True)
    molecule.writeGromacsTopolFiles()
    assert molecule
    assert len(molecule.atoms) == n1
    assert len(molecule.properDihedrals) == n2
    assert len(molecule.improperDihedrals) == n3
    assert molecule.totalCharge == n4
    assert len(molecule.atomTypes) == n5
    assert molecule.atomTypes[23].__repr__() == msg
    janitor.append(molecule.absHomeDir)


def test_ildn_gmx4_fail(janitor):

    molecule = MolTopol(acFileTop="ILDN.prmtop", acFileXyz="ILDN.inpcrd", debug=True, gmx4=True)