import copy
import sys
import time

from crossnetwork_processing_functions import get_default_temporal_resolutions_per_matrix, get_configuration_variables, check_GHOST_stage_temporary_directories, check_GHOST_stage_arguments_logs_submit
from process_GHOST_stage import ProcessGHOSTStage

###--------------------------------------------------------------------------------------------------###
#Get root slurm job ID, and active network/parameter to process --> passed from job submittal script
###--------------------------------------------------------------------------------------------------###
slurm_job_ID, network, parameter = sys.argv[1], sys.argv[2], sys.argv[3]

###--------------------------------------------------------------------------------------------------###
#Get dictionaries holding run specific variables, parameter specific variables and network specific variables
###--------------------------------------------------------------------------------------------------###
run_details, parameter_details, network_details = get_configuration_variables(slurm_job_ID, network, parameter)

###--------------------------------------------------------------------------------------------------###
#Check/create/clean argument/log/submit directories of relevant defined GHOST stages
###--------------------------------------------------------------------------------------------------###
check_GHOST_stage_arguments_logs_submit(run_details)

###--------------------------------------------------------------------------------------------------###
#Start Pipeline
###--------------------------------------------------------------------------------------------------###

#create pipeline logfile
pipeline_logfile = open('{}/run/pipeline_logs/{}_{}.out'.format(run_details['process_root'],network, parameter),"w")
pipeline_logfile.write('**************************************************************************************************************\n')        
pipeline_logfile.write('BEGINNING PROCESSING OF STAGES: {} FOR {}:{}\n'.format(run_details['stages_to_process'],network,parameter))
pipeline_logfile.write('**************************************************************************************************************\n')

#initialise timing variables 
run_details['pipeline_start_time'] = time.time()
run_details['total_stage_process_time'] = 0
run_details['total_stage_queue_time'] = 0

#initialise ProcessGHOSTStage class instance
process_inst = ProcessGHOSTStage(run_details, parameter_details, network_details, pipeline_logfile)

#STAGE S1
if 'S1' in run_details['stages_to_process']:
    process_inst.S1()
#STAGE S2
if 'S2' in run_details['stages_to_process']:
    process_inst.S2()
#STAGE S3
if 'S3' in run_details['stages_to_process']:
    process_inst.S3()
#STAGE T1
if 'T1' in run_details['stages_to_process']:
    process_inst.T1()         
#STAGE T2
if 'T2' in run_details['stages_to_process']:
    #if valid_temporal_resolution_codes variable does not exist, assume all defined temporal resolutions to process are valid 
    if not hasattr(process_inst, 'valid_temporal_resolution_codes'):
        if run_details['temporal_resolutions'] == 'default':
            temporal_resolutions = get_default_temporal_resolutions_per_matrix(parameter_details['matrix'])
        else:
            temporal_resolutions = run_details['temporal_resolutions']
        process_inst.valid_temporal_resolution_codes = copy.deepcopy(temporal_resolutions)
    #if valid_regions variable does not exist, assume all defined regions to process are valid 
    if not hasattr(process_inst, 'valid_regions'):
        process_inst.valid_regions = copy.deepcopy(run_details['regions'])    
    process_inst.T2()
    #clean all temporary GHOST stage data directories
    check_GHOST_stage_temporary_directories(run_details, parameter_details, stage='all')
#STAGE T1
if 'P1' in run_details['stages_to_process']:
    process_inst.P1()    
#STAGE T1
if 'M1' in run_details['stages_to_process']:
    process_inst.M1()   
#STAGE E1
if 'E1' in run_details['stages_to_process']:
    process_inst.E1()   

###--------------------------------------------------------------------------------------------------###
#Finalise Pipeline 
###--------------------------------------------------------------------------------------------------###

pipeline_end_time = time.time()
total_pipeline_time = (pipeline_end_time - run_details['pipeline_start_time'])/60.
pipeline_overhead_time = total_pipeline_time - (run_details['total_stage_process_time'] + run_details['total_stage_queue_time'])

pipeline_logfile.write('\n') 
pipeline_logfile.write('PROCESSING OF ALL DESIRED STAGES FOR {}:{} COMPLETE IN {:.2f} MINUTES\n({:.2f} MINUTES STAGE PROCESSING, {:.2f} MINUTES IN STAGE QUEUING, {:.2f} MINUTES IN PIPELINE OVERHEADS)'.format(network,parameter,total_pipeline_time,run_details['total_stage_process_time'],run_details['total_stage_queue_time'],pipeline_overhead_time))
pipeline_logfile.close() 