#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
This script simply uses the Manager class to transform some simulations.
'''

import sys
import importlib.util
import argparse

from hateno import jsonfiles, string
from hateno.ui import UI
from hateno.manager import Manager

def addArguments(parser):
	parser.add_argument('folder_path', type = str, help = 'path to the folder to manage')
	parser.add_argument('transform_script', type = argparse.FileType('r'), help = 'path to the script where the transformation function is defined')
	parser.add_argument('simulations_list', type = argparse.FileType('r'), nargs = '?', help = 'path to the file where the simulations list to transform can be found')

def action(args):
	with Manager(args.folder_path) as manager:
		ui = UI()

		simulations = None
		if args.simulations_list:
			simulations = jsonfiles.read(args.simulations_list.name, allow_generator = True)

			if not(type(simulations) is list):
				simulations = [simulations]

			# We only want a list of simulations settings, without the global settings
			# Then, we test the first item: if it seems to contain global settings, we remove them
			if type(simulations[0]) is dict and 'settings' in simulations[0]:
				simulations = [s['settings'] for s in simulations]

		n_simulations = len(simulations) if not(simulations is None) else manager.getSimulationsNumber()

		infos_line = ui.addTextLine(f'Transforming {string.plural(n_simulations, "simulation", "simulations")}…')
		progress_bar = ui.addProgressBar(n_simulations)

		module_name = 'tmpsimulationstransformation'
		spec = importlib.util.spec_from_file_location(module_name, args.transform_script.name)
		module = importlib.util.module_from_spec(spec)
		sys.modules[module_name] = module
		spec.loader.exec_module(module)

		manager.transform(module.transform, simulations, callback = progress_bar.update)

		ui.removeItem(progress_bar)
		infos_line.text = f'{string.plural(n_simulations, "simulation", "simulations")} transformed'

if __name__ == '__main__':
	parser = argparse.ArgumentParser(description = 'Manage the simulations stored in a given folder.')
	addArguments(parser)
	action(parser.parse_args())
