import os
import shutil
import sys

from crossnetwork_processing_functions import get_default_parameters_per_network, get_default_product_parameters_per_network
from submit_greasy import SubmitGreasy

#Read configuration file
sys.path.append(os.getcwd())
import GHOST_configuration

#get slurm job ID --> passed from job submittal script
slurm_job_ID = sys.argv[1]

#compile run details dictionary from configuration file variables
run_details = {k: v for k, v in vars(GHOST_configuration).items() if not k.startswith('_')}
#add slurm_job_id to run_details
run_details['slurm_job_ID'] = slurm_job_ID

#make a copy of the configuration file indexed by the slurm job ID
shutil.copy2(run_details['process_root']+'/run/GHOST_configuration.py',run_details['process_root']+'/run/GHOST_configurations/configuration_{}.py'.format(slurm_job_ID))

#Iterate through defined networks/parameters
arguments = []

#Iterate through networks
for network in run_details['networks']:
    #get default parameters per network if set
    if 'P1' in run_details['stages_to_process']: # product stage
        if run_details['parameters'] == 'default':
            parameters = list(get_default_product_parameters_per_network(network).keys())
        #otherwise take defined list
        else:
            parameters = run_details['parameters']
    else: # all other stages
        if run_details['parameters'] == 'default':
            parameters = get_default_parameters_per_network(network)
        #otherwise take defined list
        else:
            parameters = run_details['parameters']
            
    #Iterate through parameters
    for parameter in parameters:
        #append current iterative arguments to arguments list
        arguments.append("{} {} {}".format(slurm_job_ID, network, parameter))
        

#submit pipeline jobs (and monitor outcomes)
SubmitGreasy('GPipe_{}'.format(slurm_job_ID),'pipeline',arguments,run_details)