import pandas as pd
import os
import glob
import numpy as np
import scipy.stats as stats

# let's set up paths to where files are saved/will be saved etc
home = os.getcwd()
path_to_data = home + os.sep + 'data' + os.sep + 'raw' + os.sep
path_to_save = home + os.sep + 'data' + os.sep + 'preproc' + os.sep
data_files = glob.glob(path_to_data + '*.csv')
# empty list
df = []

# load all files in the list
for file in data_files:
    subdf = pd.read_csv(file, keep_default_na=False)
    MAD = stats.median_abs_deviation(subdf['rt'], scale=1) * 1.4826
    up_thres = np.median(subdf['rt'] + 3 * MAD)
    down_thres = np.median(subdf['rt'] - 3 * MAD)
    subdf.loc[subdf['rt'] < down_thres, 'rt'] = np.nan
    subdf.loc[subdf['rt'] > up_thres, 'rt'] = np.nan
    subdf = subdf.dropna(subset=['rt']).reset_index(drop=True)
    df.append(subdf)
# concatenate it into a single dataframe
output_file = pd.concat(df)
# add conditions when there's no movment/only sound/only dots
output_file.loc[(output_file[['mov', 'soundname']] == 'None').all(axis=1), 'congruent'] = 'nothing'
output_file.loc[(output_file.mov != 'None') & (output_file.soundname == 'None'), 'congruent'] = 'only_dots'
output_file.loc[(output_file.mov == 'None') & (output_file.soundname != 'None'), 'congruent'] = 'only_sound'
output_file.loc[output_file.congruent == 1, 'congruent'] = 'congruent'
output_file.loc[output_file.congruent == 2, 'congruent'] = 'incongruent'
# add variable to calculate % of correct replies to reportedMov
output_file['CorrectRepMov'] = output_file.apply(lambda x: x.reportedmov in x.mov, axis=1).replace({True: 1, False: 0})
output_file.loc[output_file.mov == 'None', 'CorrectRepMov'] = 0
# categorize by biological/non biological/no stimulus
output_file.loc[output_file.bio == 1, 'bio'] = 'bio'
output_file.loc[output_file.bio == 2, 'bio'] = 'inverse'
output_file.loc[output_file.bio == 0, 'bio'] = 'no_stim'
# create CorrectResponse variable, correct if there was no stimulus and replied no, or there was one and replied yes
# otherwise wrong
output_file['CorrectResp'] = 0
output_file.loc[(output_file.bio == 'no_stim') & (output_file.resp == 'n'), 'CorrectResp'] = 1
output_file.loc[(output_file.bio != 'no_stim') & (output_file.resp == 'y'), 'CorrectResp'] = 1

# now let's loop thru participants and assign values to columns to see if they saw nothing/had chance level perf.
for part_id in output_file['participant'].unique():
    p = output_file.loc[output_file['participant'] == part_id, 'CorrectResp'].mean()
    chance = 0.5
    sample_size = output_file.loc[output_file['participant'] == part_id, 'CorrectResp'].shape[0]  # n of trials
    standard_err = np.sqrt((chance * (1 - chance)) / sample_size)
    z = ((p - chance) / standard_err).round(2)  # get z score
    # right tailed test, since we test if it's above chance
    crit_val = stats.norm.ppf(1 - .01)
    # significant = significantly above chance
    if z > crit_val:
        pval = .01  # these are random, just to get the point
    else:
        pval = .1  # these are random, just to get the point

    if pval < .05:
        output_file.loc[output_file['participant'] == part_id, 'ChanceLevel'] = 0
    else:
        output_file.loc[output_file['participant'] == part_id, 'ChanceLevel'] = 1

cols_to_drop = ['soundname', '.thisRepN', 'sound', '.thisN', '.thisTrialN', 'language', 'timestamp',
                '.thisIndex', 'reportedmov', 'Unnamed: 17', 'dominant_eye']
output_file.drop(cols_to_drop, axis=1, inplace=True)

# drop the saw nothing guys that mostly replied no (90% of time)


output_file.to_csv(path_to_save + 'joined_data_final.csv', index=False)

