from textwrap import dedent


def create_script(molecule_name, temperature=273.15, pressure=101325,
                  helium_void_fraction=1.0, unit_cells=(1, 1, 1),
                  simulation_type="MonteCarlo", cycles=2000,
                  init_cycles="auto", forcefield="CrystalGenerator",
                  input_file_type="cif", **kwargs):
    """Creates a RASPA simulation input file from parameters.

    Args:
        molecule_name: The molecule to test for adsorption. A file of the same
            name must exist in `$RASPA_DIR/share/raspa/molecules/TraPPE`.
        temperature: (Optional) The temperature of the simulation, in Kelvin.
        pressure: (Optional) The pressure of the simulation, in Pascals.
        helium_void_fraction: (Optional) The helium void fraction of the input
            structure. Required for excess adsorption back-calculation.
        unit_cells: (Optional) The number of unit cells to use, by dimension.
        simulation_type: (Optional) The type of simulation to run, defaults
            to "MonteCarlo".
        cycles: (Optional) The number of simulation cycles to run.
        init_cycles: (Optional) The number of initialization cycles to run.
            Defaults to the minimum of cycles / 2 and 10,000.
        forcefield: (Optional) The forcefield to use. Name must match a folder
            in `$RASPA_DIR/share/raspa/forcefield`, which contains the properly
            named `.def` files.
        input_file_type: (Optional) The type of input structure. Assumes cif.
        charged: (Optional) A boolean indicating whether or not to use Ewald
            charge parameters.
    Returns:
        A string representing the contents of a simulation input file.

    The goal of this function is to mask the complexity of RASPA by limiting
    parameters and assuming sensible defaults. This should streamline common-
    case usage, but it means that this function won't work in all use cases.
    In these cases, look into loading your own simulation input file and
    passing it to `RASPA.run_script`.
    """
    is_mol = "yes" if input_file_type.lower() == "mol" else "no"
    print_every = cycles // 10
    a, b, c = unit_cells
    if init_cycles == "auto":
        init_cycles = min(cycles // 2, 10000)

    return dedent("""
                  SimulationType                {simulation_type}
                  NumberOfCycles                {cycles}
                  NumberOfInitializationCycles  {init_cycles}
                  PrintEvery                    {print_every}
                  RestartFile                   no

                  Forcefield                    {forcefield}
                  CutOff                        12.8
                  ChargeMethod                  Ewald
                  EwaldPrecision                1e-6
                  UseChargesFromMOLFile         {is_mol}

                  Framework                     0
                  FrameworkName                 streamed
                  InputFileType                 {input_file_type}
                  UnitCells                     {a} {b} {c}
                  HeliumVoidFraction            {helium_void_fraction}
                  ExternalTemperature           {temperature}
                  ExternalPressure              {pressure}

                  Movies                        no
                  WriteMoviesEvery              100

                  Component 0 MoleculeName             {molecule_name}
                              StartingBead             0
                              MoleculeDefinition       TraPPE
                              IdealGasRosenbluthWeight 1.0
                              TranslationProbability   1.0
                              RotationProbability      1.0
                              ReinsertionProbability   1.0
                              SwapProbability          1.0
                              CreateNumberOfMolecules  0
                  """.format(**locals())).strip()