import logging
import math
import collections


def get_simulation_parameters(open_rocket_helper, sim, rocket_components,
                              random_parameters, randomize=True):
    """Collect all global simulation parameters for export"""
    logging.info("Used global parameters...")
    options = sim.getOptions()
    tilt = math.degrees(options.getLaunchRodAngle())
    azimuth = math.degrees(options.getLaunchRodDirection())
    if randomize:
        thrust_factor = random_parameters.thrust_factor()
        mass_factor = random_parameters.mass_factor()
        CG_shift = random_parameters.CG_shift()
        Caxial_factor = random_parameters.Caxial_factor()
        CN_factor = random_parameters.CN_factor()
        Cside_factor = random_parameters.Cside_factor()
    else:
        thrust_factor = 1.
        mass_factor = 1.
        CG_shift = 0.
        Caxial_factor = 1.
        CN_factor = 1.
        Cside_factor = 1.

    logging.info("Launch rail tilt    = {:6.2f}°".format(tilt))
    logging.info("Launch rail azimuth = {:6.2f}°".format(azimuth))
    logging.info("Thrust factor = {:6.2f}".format(thrust_factor))
    logging.info("Mass factor = {:6.2f}".format(mass_factor))
    logging.info("CG shift = {:6.2f}m".format(CG_shift))
    logging.info("Caxial factor = {:6.2f}".format(Caxial_factor))
    logging.info("CN factor = {:6.2f}".format(CN_factor))
    logging.info("Cside factor = {:6.2f}".format(Cside_factor))

    mcid = options.getMotorConfigurationID()
    rocket = options.getRocket()

    # stage sepration
    separationDelays = []
    for stage in rocket_components.stages:
        separationEventConfiguration = stage.getStageSeparationConfiguration().get(mcid)
        separationDelays.append(
            separationEventConfiguration.getSeparationDelay())
        logging.info("Separation delay of stage {} = {:6.2f}s".format(
            stage, separationDelays[-1]))

    fin_cants = []
    for fins in rocket_components.fin_sets:
        fin_cants.append(math.degrees(fins.getCantAngle()))

    # There can be more than one parachute -> add unbiased
    # normaldistributed value
    parachute_cds = []
    for parachute in rocket_components.parachutes:
        parachute_cds.append(parachute.getCD())

    # motor ignition
    ignitionDelays = []
    for motor in rocket_components.motors:
        ignitionDelays.append(
            motor.getIgnitionConfiguration().get(mcid).getIgnitionDelay())
        logging.info("Ignition delay of stage {} = {:6.2f}s".format(
            motor.getStage(), ignitionDelays[-1]))

    Parameters = collections.namedtuple("Parameters", [
        "tilt",
        "azimuth",
        "thrust_factor",
        "separation_delays",
        "fin_cants",
        "parachute_cds",
        "ignition_delays",
        "mass_factor",
        "CG_shift",
        "Caxial_factor",
        "CN_factor",
        "Cside_factor"])
    return Parameters(
        tilt=tilt,
        azimuth=azimuth,
        thrust_factor=thrust_factor,
        separation_delays=separationDelays,
        fin_cants=fin_cants,
        parachute_cds=parachute_cds,
        ignition_delays=ignitionDelays,
        mass_factor=mass_factor,
        CG_shift=CG_shift,
        Caxial_factor=Caxial_factor,
        CN_factor=CN_factor,
        Cside_factor=Cside_factor)