from importlib import resources as impresources
import itertools
import io

from . import data

<template_profiler>_file_path = impresources.files(data) / "<template_profiler>_commands.txt"
<template_profiler>_initEndSplit = -1

def define_initialise(profilefile: io.TextIOWrapper, profilerdict: dict = None):
    '''
    define_initialise creates any needed variables, and writes the required arguments into the profile_file in order
    to initialise the profiling for a user, in this case that is <template_profiler>.

    Parameters
    ----------
    profilefile: io.TextIOWrapper = Open text file that can be written to and is being used to initiate, call and
        terminate all profiling codes that are to be executed with the user specified bash script.
    profilerdict: dict = dictionary containing required arguments that the profiler has or other values.

    Returns
    -------
    None
    '''
    global <template_profiler>_file_path
    global <template_profiler>_initEndSplit
    profilefile.write('# <template_profiler> initialisation declarations\n')
    if 'requirements' in profilerdict.keys():
        for i in profilerdict['requirements']:
            profilefile.write(i)
            profilefile.write('\n')
    profilefile.write('\n')
    profilefile.write('export <TEMPLATE_PROFILER>_RUNNING_DIR=${WORKING_DIR}/<Template_Profiler>\n')
    with open(<template_profiler>_file_path, 'r') as read_file:
        for number, line in enumerate(read_file):
            if line == '# *=*\n':
                <template_profiler>_initEndSplit = number + 1
                break
            profilefile.write(line)
    profilefile.write('# <Template_Profiler> initialisation done\n')
    profilefile.write('\n')
    return

def define_run(profilefile: io.TextIOWrapper, bash_options: list, tmp_work_script: str = './tmp_workfile.sh'):
    '''
    define_run calls the user given bash script using <template_profiler> to execute and profile the work done.
    Parameters. This only has to be defined, if the profiler in question is used to execute the user given bash script.
    ----------
    profilefile: io.TextIOWrapper = Open text file that can be written to and is being used to initiate, call and
        terminate all profiling codes that are to be executed with the user specified bash script.
    bash_options: list = List of bash options that the user specified bash script needs to execute as intended
    tmp_work_script: str = Path and name of the temporary work script that contains all the users code minus
        queue options.

    Returns
    -------
    None
    '''
    profilefile.write('' +
                      '{} {}\n'.format(tmp_work_script, ' '.join(str(x) for x in bash_options)))
    profilefile.write('\n')
    return


def define_end(profilefile: io.TextIOWrapper):
    '''
    define_end terminates and scrapes any data from the profiler that was used to profile the user specified bash
    script, in this case that is <template_profiler>.
    Parameters
    ----------
    profilefile: io.TextIOWrapper = Open text file that can be written to and is being used to initiate, call and
        terminate all profiling codes that are to be executed with the user specified bash script.

    Returns
    -------
    None
    '''
    global <template_profiler>_file_path
    global <template_profiler>_initEndSplit
    profilefile.write('# <Template_Profiler> final steps declarations\n')
    with open(<template_profiler>_file_path, 'r') as read_file:
        for line in itertools.islice(read_file, <template_profiler>_initEndSplit, None):
            profilefile.write(line)
    profilefile.write('# <Template_Profiler> final steps done\n')
    profilefile.write('\n')
    return
