#!/usr/bin/env python
'''
    Submission script to DICE for src/unfolding_tests/create_unfolding_pull_data.py
'''
from __future__ import print_function
from optparse import OptionParser
from dps.config.xsection import XSectionConfig
from dps.condor import job
from dps.condor.jobtypes.produce_unfolding_hists_job import ProduceUnfoldingHistsJob
import os
import sys


def main():
    parser = OptionParser(__doc__)
    parser.add_option("-c", "--com", dest="com", type=int,
                      help="Centre-of-mass energy to be used for submission")
    parser.add_option("-v", "--variables", dest="variables",
                      help="Variables to be analysed, separated by commas")
    parser.add_option("-i", "--input_file", dest="input_file",
                      help="input file")
    parser.add_option("-f", "--filter_jobs", dest="filter_jobs",
                      help="Only submit jobs that match the IDs specified here. Multiple values separated by comma.",
                      default='')
    (options, _) = parser.parse_args()
    sys.exit('Not yet implemented, please try again later')

    # first the global variables
    centre_of_mass_energy = options.com
    config = XSectionConfig(centre_of_mass_energy)
    output_folder = 'data'

    variables = options.variables.split(',')
    filter_jobs = job.parse_filter_jobs(options.filter_jobs)
    input_file_name = options.input_file

    pull_jobs = []

    for variable in variables:
        # check if file exists
        if not os.path.exists(input_file_name):
            print('Input file {0} does not exist'.format(input_file_name))
            sys.exit(1)
        for channel in ['electron', 'muon']:
            pull_job = ProduceUnfoldingHistsJob(
            )
            pull_jobs.append(pull_job)

    n_jobs_to_run = 100 
    n_jobs_to_split = 100
    # this part should be more or less identical between scripts
    if filter_jobs:
        n_jobs_to_run = len(filter_jobs)
    condor_job = job.Condor(n_jobs_to_run, n_jobs_to_split, request_memory=50)
    for j in pull_jobs:
        condor_job.add_job(j)
    condor_job.submit()
    print('All jobs submitted')


if __name__ == '__main__':
    main()
