from copy import deepcopy


def xtb_equilibrate(sys, calc, prod_time=20, **kwargs):
    """
    This function will automate the process of taking a system from zero
    temperature up to room temperature (298.15 kelvin).

    This routine will work by performing 10ps simulations at temperatures
    from 0 to 298.15 kelvin, with each simulation spaced by 10 degrees
    (310 ps total). Finally, the production run can be performed.

    Args:
        sys (BigDFT.Systems.System): the system to calculate.
        calc (BigDFT.Interop.XTBInterop.XTBCalculator): a calculator to use.
        prod_time (int): the number of production picoseconds to run at
            room temperature.
        **kwargs: any argument you would normally want to pass to the
            run command.
    Returns:
        (list, list): a list of positions along the trajectory and a list of
        energy values.
    """
    positions = []
    energies = []
    param = kwargs.pop("inp", {})
    if "md" not in param:
        param["md"] = {}

    # Loop over temperature values
    usys = deepcopy(sys)
    for temp in list(range(0, 300, 10)) + [298.15]:
        param["md"]["temp"] = temp
        param["md"]["time"] = 10
        if temp == 0:
            param["md"]["restart"] = "false"
            log = calc.run(sys=usys, omd=True, inp=param, **kwargs)
        else:
            param["md"]["restart"] = "true"
            log = calc.run(sys=usys, md=True, inp=param, **kwargs)

        # Extract results
        positions += log.md_traj
        energies += log.md_energy
        usys = positions[-1]

    # Production Run
    param["md"]["time"] = prod_time
    param["md"]["restart"] = "true"
    param["md"]["temp"] = 298.15
    log = calc.run(sys=usys, md=True, inp=param, **kwargs)

    positions += log.md_traj
    energies += log.md_energy
    usys = positions[-1]

    return positions, energies