#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# --- výpis počtů značek věech subjektů
seegs = [15, 32, 45, 48]
seeg = seegs[0]

# for seeg in seegs:
#     vypocet_pac_fac(seeg)



def vypocet_pac_fac(seeg):

    """
    Phase-amplitude and Frequency-amplitude coupling at multiple low and high frequency bands

    Parameters:
    -----------
    seeg - patient number, e.g. 15, 32 or 48

    Returns:
    --------
    df_pac_all_frequency_bands - dataframe containing basic information about patient (start position of marked oscillation, selected channels, oscillation type) and value of Phase-amplitude coupling in each frequency band
        ('[[1, 4], [4, 8]]', '[[1, 4], [8, 12]]', '[[1, 4], [12, 20]]', ...  '[[4, 8], [8, 12]]', '[[4, 8], [12, 20]]',... '[[600, 1000], [1000, 2000]]'])
    df_fac_all_frequency_bands - dataframe containing Frequency-amplitude coupling values
    """

    import pandas as pd
    pd.set_option('display.width', 2000) #nezalamuj tabulky
    from fnusa_sigproc.methods_uni import pse, pac, fac
    from funkce.nacteni import znaceni, signal, info_o_patologii

    # --- výpis počtů značek věech subjektů
    # seegs = [15, 32, 45, 48] # v případě, že nemám jiná data
    #seeg = seegs[0]

    fs = 5000
    okoli = 150

    try:
        df_all_groups = pd.read_pickle("Dataframes/df_all_groups_" + str(seeg) + ".pkl") #main_df.to_pickle("Dataframes/main_df_" + str(seeg) + ".pkl")
        sig_FR = pd.read_pickle("Dataframes/sig_FR_" + str(seeg) + "_okoli_" + str(2*okoli) + ".pkl")
        sig_vFR = pd.read_pickle("Dataframes/sig_vFR_" + str(seeg) + "_okoli_" + str(2*okoli) + ".pkl")
        sig_uFR = pd.read_pickle("Dataframes/sig_uFR_" + str(seeg) + "_okoli_" + str(2*okoli) + ".pkl")

    except (OSError, IOError) as e:

        # nazev, FR, vFR, uFR, df_all_groups = znaceni(seeg)
        nazev, FR, vFR, uFR, df_all_groups = info_o_patologii(seeg)   # přidání informace o patologii
        sig_FR, sig_vFR, sig_uFR = signal(seeg, okoli)


    # čištění nepotřebných dat #super věc
    # columns = ['pse', 'fac', 'pac', 'sample_entropy0', 'shannon_entropy','sample_entropy1', 'stop', 'ch_idx', 'group']
    # df_pac_all_frequency_bands = main_df.drop(columns, 1)

    # přidání informace o čísle pacienta - v případě potřeby concatu dataframů
    df_all_groups['seeg'] = seeg

    # získání kostry databáze
    df_pac_all_frequency_bands = df_all_groups.copy()

    # --- volba frekvencí
    low_fc = [1,4],[4,8],[8,12],[12,20],[20,45],[55,80],[80,250],[250,600],[600,1000]
    high_fc = [4,8],[8,12],[12,20],[20,45],[55,80],[80,250],[250,600],[600,1000],[1000,2000]

    # --- definice nových dataframe
    for lfc in low_fc:
        for hfc in high_fc:
            if lfc[0] != hfc[0] and lfc[0] < hfc[0]:
                df_pac_all_frequency_bands[str([lfc, hfc])]=0 #df_pac['rozhrani'+str(i)]=0

    # definice druhého df
    df_fac_all_frequency_bands = df_pac_all_frequency_bands.copy()


    ### Phase-amplitude & Frequency-amplitude coupling extended

    # Fast Ripples
    # hledáni indexu sloupce pro plnění hodnot
    sloupec = df_pac_all_frequency_bands.columns.get_loc(str([low_fc[0], high_fc[0]]))
    start_sloupec = sloupec

    for i in range(0, len(sig_FR.columns)):
        for lfc in low_fc:
            for hfc in high_fc:
                if lfc[0] != hfc[0] and lfc[0] < hfc[0]:

                    pac_result = pac(sig_FR[i], fs, lfc[0], lfc[1], hfc[0], hfc[1])
                    fac_result = fac(sig_FR[i], fs, lfc[0], lfc[1], hfc[0], hfc[1])

                    #print("{}/{}: {}: \t{}".format(i, sloupec, [lfc, hfc], pac_result))

                    df_pac_all_frequency_bands.iloc[i, sloupec] = pac_result
                    df_fac_all_frequency_bands.iloc[i, sloupec] = fac_result

                    sloupec += 1

        sloupec = start_sloupec


    # Very Fast Ripples

    prvni_vFR_idx = len(sig_FR.columns)

    sloupec = df_pac_all_frequency_bands.columns.get_loc(str([low_fc[0], high_fc[0]]))
    start_sloupec = sloupec

    for i in range(0, len(sig_vFR.columns)):
        for lfc in low_fc:
            for hfc in high_fc:
                if lfc[0] != hfc[0] and lfc[0] < hfc[0]:

                    pac_result = pac(sig_vFR[i], fs, lfc[0], lfc[1], hfc[0], hfc[1])
                    fac_result = fac(sig_vFR[i], fs, lfc[0], lfc[1], hfc[0], hfc[1])

                    df_pac_all_frequency_bands.iloc[prvni_vFR_idx+i, sloupec] = pac_result
                    df_fac_all_frequency_bands.iloc[prvni_vFR_idx+i, sloupec] = fac_result

                    sloupec += 1

        sloupec = start_sloupec



    # Ultra Fast Ripples
    prvni_uFR_idx = prvni_vFR_idx + len(sig_vFR.columns)


    sloupec = df_pac_all_frequency_bands.columns.get_loc(str([low_fc[0], high_fc[0]]))
    start_sloupec = sloupec

    for i in range(0, len(sig_uFR.columns)):
        for lfc in low_fc:
            for hfc in high_fc:
                if lfc[0] != hfc[0] and lfc[0] < hfc[0]:

                    pac_result = pac(sig_uFR[i], fs, lfc[0], lfc[1], hfc[0], hfc[1])
                    fac_result = fac(sig_uFR[i], fs, lfc[0], lfc[1], hfc[0], hfc[1])

                    df_pac_all_frequency_bands.iloc[prvni_uFR_idx+i, sloupec] = pac_result
                    df_fac_all_frequency_bands.iloc[prvni_uFR_idx+i, sloupec] = fac_result

                    sloupec += 1

        sloupec = start_sloupec


    df_pac_all_frequency_bands.to_pickle("Dataframes/df_pac_all_frequency_bands_" + str(seeg) + ".pkl")
    df_fac_all_frequency_bands.to_pickle("Dataframes/df_fac_all_frequency_bands_" + str(seeg) + ".pkl")


    print('Finished and saved!')

    return df_pac_all_frequency_bands, df_fac_all_frequency_bands, okoli
