"""Command line tool to run a YAML simulation configuration file."""

import yaml
import argparse

from utils import update_config
from simulation import Simulation, Galaxy


# Parse command line arguments: 
# > python simulation.py config_file.yml output_folder
parser = argparse.ArgumentParser(description='''Run a galactic 
	collision simulation.''')
parser.add_argument('config_file', type=argparse.FileType('r'),
	help='''Path to configuration file for the simulation, in YAML format. 
	See the config folder for examples.''')
parser.add_argument('--output_folder', default=None,
	help='''Name of the output folder in data/ where the results will be 
	saved. The directory will be created if necessary. If none is provided, 
	the name attribute in the configuration file will be used instead.''')
parser.add_argument('--verbose', action='store_true', default=False,
	help='''In verbose mode the simulation will print its progress.''')

args = parser.parse_args()

# Load the configuration for this simulation
CONFIG = yaml.load(open("config/default.yml", "r")) # default configuration
updates = list(yaml.load_all(args.config_file))

for update in updates:
	# For multiple configurations in one file, 
	# the updates are with respect to the first one.
	update_config(CONFIG, updates[0])
	update_config(CONFIG, update)
	# If no output folder is provided, the name in CONFIG is used instead
	outputFolder = (CONFIG['name'] if args.output_folder is None 
		else args.output_folder)

	# Run the simulation
	sim = Simulation(**CONFIG['simulation'], verbose=args.verbose, 
		CONFIG=CONFIG)
	galaxy1 = Galaxy(**CONFIG['galaxy1'], sim=sim) # create the galaxies
	galaxy2 = Galaxy(**CONFIG['galaxy2'], sim=sim)
	sim.setOrbit(galaxy1, galaxy2, **CONFIG['orbit']) # define the orbit
	sim.run(**CONFIG['simulation'], outputFolder=outputFolder)
