from __future__ import print_function
import os,sys

def flexible_input_from_user(key_prefix=""):

    inp_dict = {}
    N_arg    = len(sys.argv)
    if N_arg > 1:
        for arg_indx in range(1,N_arg):
            str_val  = sys.argv[arg_indx]
            indx_val = find_index_of_string_character(str_val)

            if key_prefix == "":
                key_name = str_val[:indx_val]
            else:
                key_name = key_prefix + "_" + str_val[:indx_val]

            key_val  = str_val[indx_val+1:].upper()
            key_val  = check_for_float_variables(key_name,key_val)
            key_val  = check_for_integer_variables(key_name,key_val)
            inp_dict.update({key_name:key_val})

    return inp_dict

def check_for_float_variables(key_name,key_val):
    var_list = ["z_em","L_box","log_Z_val_const","log_N_HI_DLA",\
    "z_DLA","b_DLA","CRVAL1","CDELT1","dia_telescope",\
    "dia_fibre_arcsec","m_source","m_sky","t_exp","airmass","beta_moffat",\
    "fwhm_seeing","PRI_fwhm","read_noise","dark_current","ccd_pix_size_micron",\
    "fFP","fcol","fcam","beamsize","r_offset"]

    if key_name in var_list:
        key_val = float(key_val)

    return key_val




def check_for_integer_variables(key_name,key_val):
    var_list = ["N_Grid","N_pixel_obs","rank","N_proc","input_type"]

    if key_name in var_list:
        key_val = int(key_val)

    return key_val





def find_index_of_string_character(str_val):
    char_list = [":","=","-"]
    for char_val in char_list:
        if char_val in str_val:
            indx_val = str_val.find(char_val)
            return indx_val
    print("Invalid argument string from user! Exiting the code")
    exit()

def custom_progressbar(perc_val,prefix="Progress",suffix="Completed",N_total=25,flag_new_line="n"):

    if flag_new_line == "y":
        if perc_val <= 0:
            print("")

    perc_char         = N_total / 100.0
    prefix_text       = prefix + ":  "
    fill_region       = int(perc_val*perc_char)
    non_fill_region   = N_total - fill_region
    progress_bar_text = "[" + "#" * int(perc_val*perc_char)  + " " * non_fill_region + "]"
    suffix_text       = " " + str("%.1f"%perc_val) + "% " + suffix
    print(prefix_text + progress_bar_text + suffix_text)

    if perc_val < 100:
        sys.stdout.write("\033[F") # Cursor up one line
    else:
        print("")

def process_completion_print_function(inp_dict,process_name):
    rank    = inp_dict["rank"]
    #spec_id = inp_dict["spec_id"]
    los_indx    = inp_dict["los_indx"]
    N_los_total = inp_dict["N_los_total"]
    #print_statement_1 = "[spec_id = " + str(spec_id)
    print_statement_1 = "[LOS = " + str(los_indx) + ", N_LOS_TOTAL = " + str("%i"%N_los_total)
    print_statement_2 = ", Rank = " + str(rank) + "] -->> "
    custom_print_function(print_statement_1 + print_statement_2 + process_name)
    #custom_print_function(print_statement_1 + print_statement_2 + process_name + " ...")

def custom_print_function(print_text):
    print(print_text)
    sys.stdout.flush()

def get_python_version():
    return sys.version[0]
