import json, os
import sys
import subprocess

import run_observatory_thumbnails as robsth
import allensdk.internal.core.lims_utilities as lu
from allensdk.internal.core.lims_pipeline_module import run_module, PipelineModule
import allensdk.core.json_utilities as ju
from allensdk.config.manifest import Manifest

def get_container_info(container_id):
    res = lu.query("""
select * from ophys_experiments oe
where experiment_container_id = %d
and oe.workflow_state != 'failed'
""" % container_id)
    return res

def debug(container_id, local=False, plots=None):
    SCRIPT = "/data/informatics/CAM/analysis/allensdk/allensdk/internal/pipeline_modules/run_observatory_container_thumbnails.py"
    SDK_PATH = "/data/informatics/CAM/analysis/allensdk/"
    OUTPUT_DIR = "/data/informatics/CAM/analysis/containers"

    container_dir = os.path.join(OUTPUT_DIR, str(container_id))

    input_data = []
    for exp in get_container_info(container_id):
        exp_data = robsth.get_input_data(exp['id'])
        exp_input_json = os.path.join(exp_data["output_directory"], "input.json")
        input_data.append(dict(
                input_json=exp_input_json,
                output_json=os.path.join(exp_data["output_directory"], "output.json")
                ))

        Manifest.safe_make_parent_dirs(exp_input_json)
        ju.write(exp_input_json, exp_data)

    run_module(SCRIPT,
               input_data,
               container_dir,
               sdk_path=SDK_PATH,
               pbs=dict(vmem=32,
                        job_name="cthumbs_%d"% container_id,
                        walltime="10:00:00"),
               local=local,
               optional_args=['--types='+','.join(plots)] if plots else None)

def main():
    mod = PipelineModule()
    mod.parser.add_argument("--types", default=','.join(robsth.PLOT_TYPES))
    mod.parser.add_argument("--threads", default=4)
    
    data = mod.input_data()
    types = mod.args.types.split(',')

    for input_file in data:
        exp_input_json = input_file['input_json']
        exp_output_json = input_file['output_json']

        exp_input_data = ju.read(exp_input_json)

        nwb_file, analysis_file, output_directory = robsth.parse_input(exp_input_data)
        
        robsth.build_experiment_thumbnails(nwb_file, analysis_file, output_directory, 
                                           types, mod.args.threads)

if __name__=='__main__': main()
