from psychopy import prefs
from pathlib import Path
import platform

os_name = platform.system()
# directories
input_dir = Path("data/input")
output_dir = Path("data/output")
spin_wheel_dir = Path(f"{input_dir}/spin_wheel")
snd_stim_dir = Path(f"{input_dir}/sounds/seq_sounds")
# file names
bg_fn = Path(f"{input_dir}/background/background2.jpg")
stim_bg_fn = Path(f"{input_dir}/background/stim_bg.png")
sound0_fn = Path(f'{input_dir}/sounds/reward.wav')
snd_endPause_fn = Path(f'{input_dir}/sounds/end_pause.wav')
fix_img_fn = Path(f"{input_dir}/fix/fix.png")
q_reward_fn = [Path(f'{input_dir}/sounds/reward{i}.wav') for i in range(1, 4)]
instr_fnames = {
    'instr_rest_fn': 'instructions_rest.txt',
    'instr1_fn': 'instructions_p1.txt',
    'instr2_fn' : 'instructions_p2.txt',
    'instr2_img_fn' : 'setup1.png',
    'instr3_fn' : 'instructions_p3.txt',
    'instr4_fn' : 'instructions_p4.txt',
    'instr5_fn' : 'instructions_p5.txt',
    'instr_q_fn' : 'instructions_q.txt',
    'fb_timeout_fn': 'fb_timeout.txt',
    'fb_ok_fn': 'fb_ok.txt',
    'instr_bonus_fn' : 'instructions_bonus.txt',
    'instr_bonus_img_fn' : 'setup2.png',
    'instr_bonus2_fn' : 'instructions_bonus2.txt',
    'instr_bonus3_fn' : 'instructions_bonus3.txt',
    'instr_stimpres_fn' : 'instr_stim_pres.txt',
    'instr_stimpres2_fn' : 'instr_stim_pres2.txt',
    'instr_stimpres3_fn' : 'instr_stim_pres3.txt',
    'instr_pause_fn': 'instr_pause0.txt', 
    'instr_reward_fn': 'instr_reward.txt',
    'instr_tmr1': 'instr_tmr1.txt',
    'instr_tmr2': 'instr_tmr2.txt',
    'quest_ready_fn': 'quest_ready.txt',
    'quest_vigi_fn': 'quest_vigilance.txt',
    'quest_focus_fn': 'quest_focus.txt',
    'quest_think_fn': 'quest_think.txt',
    # 'instr_locA1_fn': 'instr_locA1.txt',
    # 'instr_locA2_fn': 'instr_locA2.txt',
    # 'instr_locB1_fn': 'instr_locB1.txt',
    # 'instr_locB2_anim_fn': 'instr_locB2_anim.txt',
    # 'instr_locB2_inanim_fn': 'instr_locB2_inanim.txt',
    # 'instr_locC1_fn': 'instr_locC1.txt',
    # 'instr_locC2_fn': 'instr_locC2.txt',
    # 'instr_loc_fb1_fn': 'instr_loc_fb1.txt',
    # 'instr_loc_fb2_fn': 'instr_loc_fb2.txt',
    # 'end_loc_fn': 'end_loc.txt',
}
# mapping of keys to numbers for the response pad
key_dict = {
    'left' : '1',
    'confirm' : '2',
    'right' : '3',
    'pause' : 'b',
    'up' : '4',
    'down' : '5',
    'master_key' : 'm'
}
# for the bonus question
key_bq = {
    'left':'1',
    'right':'3',
    'up':'8',
    'down':'9',
    'confirm':'2',
    'remove':'6',
}
bq_time_limit = 60 # seconds to answer the bonus question
bq_time_margin = 7

# stim config
t = 0.001 # speed of text presentation
#seed = 42
frate = 60
bg_color = (255, 255, 255)
text_height = 0.08
img_size = 0.4
img_bg_size = 0.41
win_size = [1512, 982]
prefs.hardware['audioLib'] = ['PTB']
if os_name == "Windows":
    screen = 1
    use_mock_port = True
    pport = None #0x2FB8
elif os_name == "Linux":
    screen = 1
    use_mock_port = False
    pport = '/dev/parport0'
elif os_name == "Darwin":
    screen = 0
    use_mock_port = True
    pport = None

categories = {
    'animals':0,
    'bodyparts':1,
    'characters':2,
    'colors':3,
    'landscapes':4,
    'shapes':5,
}
# organization
n_amodal_items_per_cat = 6
n_blocks = 4
n_trials = 3
n_seq = 6
n_blocks_demo = 1
n_trials_demo = 1

# structures of how categories are ordered in each sequence
seq_structures = {
    'A1': [1, 4, 2, 5, 0, 3],
    'B1': [4, 5, 1, 3, 2, 0],
    'C1': [5, 3, 4, 0, 1, 2],
    'D1': [0, 2, 3, 1, 5, 4],
    'E1': [3, 0, 5, 2, 4, 1],
    'F1': [2, 1, 0, 4, 3, 5],
    'A2': [1, 4, 2, 5, 0, 3],
    'B2': [4, 5, 1, 3, 2, 0],
    'C2': [5, 3, 4, 0, 1, 2],
    'D2': [0, 2, 3, 1, 5, 4],
    'E2': [3, 0, 5, 2, 4, 1],
    'F2': [2, 1, 0, 4, 3, 5],
}

# seq_structures = {
#     'A': [1, 4, 2, 5, 0, 3],
#     'B': [4, 5, 1, 3, 2, 0],
#     'C': [5, 3, 4, 0, 1, 2],
#     'D': [0, 2, 3, 1, 5, 4],
#     'E': [3, 0, 5, 2, 4, 1],
#     'F': [2, 1, 0, 4, 3, 5]
# }

# seq_structures = {
#     'A': [0, 3, 5, 1, 2, 4], 
#     'B': [3, 1, 4, 2, 5, 0], 
#     'C': [1, 0, 4, 5, 3, 2], 
#     'D': [0, 2, 3, 5, 4, 1], 
#     'E': [2, 4, 0, 5, 1, 3], 
#     'F': [2, 4, 1, 3, 0, 5], 
#     'G': [3, 5, 0, 4, 2, 1], 
#     'H': [4, 5, 1, 0, 3, 2], 
#     'I': [1, 2, 5, 3, 0, 4], 
#     'J': [4, 3, 2, 0, 1, 5], 
#     'K': [5, 0, 2, 1, 4, 3], 
#     'L': [5, 2, 3, 4, 1, 0],
# }
# resting 
t_rest = 180 # 3 min

# sequence presentation timings
isi_dur = 1.5
stim_dur = 0.3 # 0.3 but + 50 ms in the actual presentation measurement
jitter = 0.05 

# sound volume
# snd_stim_vol = 0.5
# def_snd_vol = 1

# photodiode rectangles 
rect_w_size = 0.1
rect_b_size = 0.1

# params for the in-task questions
t_prep = 2
t_viz_cue = 5
t_viz_target = 3
t_act = 2
t_fb = 1
t_iqi = 0.5
t_post_q = 5

q_img_size = 0.2
q_slot_size = q_img_size + 0.001
hl_size = q_slot_size + 0.001
y_pos = 0.25

max_points = 6

# for the questionnaire after each block
bar_c = '#254E70'
tick_c = '#254E70'
slider_c = 'white'
validation_c = '#AEF3E7'
slider_lc = 'black'
y_bar = -0.2
far_l = -0.5
far_r = 0.5
n_ticks = 7
bar_len = far_r - far_l
stxt_up = 0.2 
stxt_dict = {
    'fr': {
        'vigi':['somnolent.e', 'éveillé.e'],
        'focus': ['distrait.e', 'concentré.e'],
        'think': ['autre chose', 'séquences'],
    },
    'en': {
        'vigi': ['drowsy', 'awake'],
        'focus': ['distracted', 'focused'],
        'think': ['other things', 'sequences'],
    },
}
# end block pause
t_post_block = 30 # To adjust after discussion 30
t_rotate = 2 # seconds

# end of run pause
t_post_run = 60 # 90 # NOW 60

# for rewarded sequence presentation
rw_img_size = 0.2
rw_hl_size = rw_img_size + 0.025
# rw_hl_color = (212, 175, 55) # gold
rw_hl_color = '#D4AF37'
flick_freq = 1 # Hz
t_reward_info = 10 # seconds

# params for the bonus question
bq_img_size = 0.1
bq_hl_size = bq_img_size + 0.001

# how much points rewarded (in chf)
reward_value = 0.5

# params for the TMR
t_tmr = 80
t_tmr_delay = 5
tmr_jitter = 0.5

# params for the localizer
loc_nblocks = 6 
loc_stim_dur = 0.3 # 300 ms
loc_isi_dur = 1 # 1000 ms
loc_jitter = 0.05 # 50 ms
n_tgt_low = 2 # lower bound for the number of target per block
n_tgt_high = 4 # upper bound for the number of target per block
loc_incor_thresh = 10 # threshold for the percent of incorrect responses in a block
t_loc_fb = 1.5

# trigger mapping :
# - trig1 : sequence + position -> between 11 and 126
# - trig2 : category + modality -> between ...
# - trig3 : misc -> between 201 and 232
# - trig4 : for localizer; valid and target -> 233 and 234

trig1 = {
    'img': {
        'animals': 131,
        'bodyparts': 132,
        'characters': 133,
        'colors': 134,
        'landscapes': 135,
        'shapes': 136,
    },
    'txt': {
        'animals': 241,
        'bodyparts': 242,
        'characters': 243,
        'colors': 244,
        'landscapes': 245,
        'shapes': 246,
    },
}

trig2 = {
    'A1':[11, 12, 13, 14, 15, 16],
    'B1':[21, 22, 23, 24, 25, 26],
    'C1':[31, 32, 33, 34, 35, 36],
    'D1': [41, 42, 43, 44, 45, 46],
    'E1': [51, 52, 53, 54, 55, 56],
    'F1': [61, 62, 63, 64, 65, 66],
    'A2': [71, 72, 73, 74, 75, 76],
    'B2': [81, 82, 83, 84, 85, 86],
    'C2': [91, 92, 93, 94, 95, 96],
    'D2': [101, 102, 103, 104, 105, 106],
    'E2': [111, 112, 113, 114, 115, 116],
    'F2': [121, 122, 123, 124, 125, 126],
}

trig3 = {
    'left': 201,
    'right': 202,
    'confirm': 203,
    'fb_ok': 204,
    'fb_timeout': 205, 
    'timeout': 206,
    'block_pause': 207,
    'block_endpause': 208,
    'run_pause': 209,
    'run_endpause': 210,
    'reward_info': 211,
    'tmr_A1':221,
    'tmr_B1':222,
    'tmr_C1':223,
    'tmr_D1':224,
    'tmr_E1':225,
    'tmr_F1':226,
    'tmr_A2':227,
    'tmr_B2':228,
    'tmr_C2':229,
    'tmr_D2':230,
    'tmr_E2':231,
    'tmr_F2':232,
}

trig4 = {
    'valid':233,
    'target':234, # should not use 'target', as it is used in the sequence task
}

triggers = {
    'mod_cat':trig1,
    'seq_pos': trig2,
    'misc': trig3,
    'loc': trig4,
}