%% plot raincloud plot of rem vs forg reinstatement scores, clusteravg

function posthoc_cluster_raincloud(subjects,rsa_tag,roi_tag,stats_tag,testType,direction)
    
    %get index of groupstats cluster
	[clusteridx, enc_wins, rec_wins] = extract_ersclusteridx_032122(rsa_tag,stats_tag,testType,roi_tag,direction);
    
    %% set dir and fileNames
    load(['..\subject_list.mat']);
    dataDir = '..\Data';
    fs = '\';
    
    %% init empty struct which holds remembered and forgotten reinstatement scores
    rem_rsa = zeros(length(subjects),1); %ITEM
    forg_rsa = zeros(length(subjects),1); %ITEM
    same_rem = zeros(length(subjects),1); %SAME
    same_forg = zeros(length(subjects),1); %SAME
    diff_rem = zeros(length(subjects),1); %DIFF
    diff_forg = zeros(length(subjects),1); %DIFF
    
    %% for each subject now load
    for num = 1:length(subjects)
        fileName = subNames{subjects(num)};
        %load ers info and fetch remembered and forgotten idx
        ERSinfo = load([dataDir fs fileName fs 'stats' fs 'ERS' fs rsa_tag fs roi_tag fs 'ERSinfo.mat']);
        ERSinfo = ERSinfo.info; 
        
        rem_ix = find(ERSinfo.trialinfo(:,2)==1);
        forg_ix = find(ERSinfo.trialinfo(:,2) == 2);
        
        %load Reinstatement scores
        load([dataDir fs fileName fs 'stats' fs 'ERS' fs rsa_tag fs roi_tag fs stats_tag fs 'itemspecific_reinstatement.mat']);

        % extract avg across timepoints of groupstats cluster and for
        % remembered and forgotten
        ITEM = permute(Reinstatement.ITEM,[3,1,2]); %trial, time, time
        SAME = permute(Reinstatement.SAME,[3,1,2]);
        DIFF = permute(Reinstatement.DIFF,[3,1,2]);
        
        rem_rsa(num,1) = nanmean(nanmean(ITEM(rem_ix,clusteridx),2));
        forg_rsa(num,1) = nanmean(nanmean(ITEM(forg_ix,clusteridx),2));
        
        same_rem(num,1) = nanmean(nanmean(SAME(rem_ix,clusteridx),2));
        same_forg(num,1) = nanmean(nanmean(SAME(forg_ix,clusteridx),2));
        diff_rem(num,1) = nanmean(nanmean(DIFF(rem_ix,clusteridx),2));
        diff_forg(num,1) = nanmean(nanmean(DIFF(forg_ix,clusteridx),2));
    end
    
    %% plot barscatter for remembered vs forgotten of ITEM
    %figure('Renderer','painters','Position',[300 600 300 350]);
    X1 = ones(length(rem_rsa),1)*1;
    X2 = ones(length(forg_rsa),1)*2;
    %array values per subject
    Y1 = rem_rsa;
    Y2 = forg_rsa;
    %colors
    rem_c = [0.7412 0.2745 0.4431]; %prev [0 0.5 0.2]
    forg_c = [0.3294 0.4471 0.7686]; %prev [0.7 0.2 0]
    cls = [rem_c; forg_c];
    grey = [0.8 0.8 0.8];
 
    % make it rain REM VS FORG (ITEM = SAME - DIFF)   
    f = figure('Position', [650 550 420 350]);
    d = {Y1, Y2};   
    %plot rainclouds
    h1 = raincloud_plot(d{1}, 'box_on', 1, 'color', rem_c, 'alpha', 0.5,'box_dodge', 1, 'box_dodge_amount', 0.15, 'dot_dodge_amount', .15, 'box_col_match', 1);
    h2 = raincloud_plot(d{2}, 'box_on', 1, 'color', forg_c, 'alpha', 0.5,'box_dodge', 1, 'box_dodge_amount', 0.45, 'dot_dodge_amount', .45, 'box_col_match', 1);
    %add mean point
    %scatter([mean(d{1}) mean(d{2})],[0 0],40,[rem_c; forg_c],'o','filled','MarkerEdgeColor','black','LineWidth',2)
    
    %adjust raindrops
    h1{2}.SizeData = 30; h1{2}.MarkerEdgeColor=grey; h1{2}.MarkerFaceColor = grey;
    h2{2}.SizeData = 30; h2{2}.MarkerEdgeColor=grey; h2{2}.MarkerFaceColor = grey; 
    %make scatter align on one line and draw rep connecting lines
    h1{2}.YData = repmat(h1{5}.YData(1), length(h1{2}.YData), 1);
    h2{2}.YData = repmat(h2{5}.YData(1), length(h2{2}.YData), 1);
    
    %remove line of density dist
    h1{1}.EdgeColor = 'none';
    h2{1}.EdgeColor = 'none';

    %add rep mes lines
    lines = plot([h1{2}.XData; h2{2}.XData],[h1{2}.YData; h2{2}.YData],'Color',grey);
    uistack(lines,'bottom'); 
    %add legend 
    legend([h1{1} h2{1}], {'Remembered', 'Forgotten'})
    legend('AutoUpdate','off');
    %add labels and title
    title('Rem vs Forg');
    yticklabels({});
    xlabel('ERS (spearman rho)');
    %view mode
    set(gca,'XLim', [-0.25 0.25], 'YLim', [-10 11]);
    if strcmp(roi_tag,'BA39_40')
        set(gca,'XLim', [-0.2 0.15], 'YLim', [-10 18]);
    end
    camroll(90);
    set(gcf,'color','white')
    xline(0,'--');
    
    %post-hoc ttest of rem vs forg
    [~,p,~,stats]= ttest(Y1,Y2);
    disp(['rem vs forg t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    
    title(['t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    
    %test remembered and forgotten against 0!
    [h,p,ci,stats] = ttest(Y1);
    disp(['rem vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    [h,p,ci,stats] = ttest(Y2);
    disp(['forg vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    disp(' ');
    %% plot barscatter for SAME DIFF Remembered vs forgotten
    %figure('Renderer','painters','Position',[600 600 260 350]);
    %x indices
    X1 = ones(length(same_rem),1)*0.5;
    X2 = ones(length(same_rem),1)*1;
    X3 = ones(length(same_rem),1)*2;
    X4 = ones(length(same_rem),1)*2.5;

    %array values per subject
    Y1 = same_rem;
    Y2 = same_forg;
    Y3 = diff_rem;
    Y4 = diff_forg;

    %color per subject
    cols = cbrewer('qual','Paired',12);
    cols(cols>1) = 1; cols(cols<0) = 0;
    same_rem_c = cols(2,:);
    same_forg_c = cols(1,:);
    diff_rem_c = cols(8,:);
    diff_forg_c = cols(7,:);
    
    %make it rain
    figure('Position',[450 500 420 350]);
    d = {Y1, Y2, Y3, Y4};   
    %plot rainclouds
    h1 = raincloud_plot(d{1}, 'box_on', 1, 'color', same_rem_c, 'alpha', 0.8,'box_dodge', 1, 'box_dodge_amount', 0.20, 'dot_dodge_amount', 0.20, 'box_col_match', 1);
    h2 = raincloud_plot(d{2}, 'box_on', 1, 'color', same_forg_c, 'alpha', 0.8,'box_dodge', 1, 'box_dodge_amount', 0.55, 'dot_dodge_amount', 0.55, 'box_col_match', 1);
    h3 = raincloud_plot(d{3}, 'box_on', 1, 'color', diff_rem_c, 'alpha', 0.8,'box_dodge', 1, 'box_dodge_amount', 0.9, 'dot_dodge_amount', 0.9, 'box_col_match', 1);
    h4 = raincloud_plot(d{4}, 'box_on', 1, 'color', diff_forg_c, 'alpha', 0.8,'box_dodge', 1, 'box_dodge_amount', 1.25, 'dot_dodge_amount', 1.25, 'box_col_match', 1);
    uistack(h3{1}, 'bottom');
    uistack(h4{1}, 'bottom');
    
    %add mean point
    %scatter([mean(d{1}) mean(d{2}) mean(d{3}) mean(d{4})],[0 0 0 0],40,[same_rem_c; same_forg_c; diff_rem_c; diff_forg_c],'o','filled','MarkerEdgeColor','black','LineWidth',2)
    
    %adjust raindrops
    h1{2}.SizeData = 25; h1{2}.MarkerEdgeColor=grey; h1{2}.MarkerFaceColor = grey;
    h2{2}.SizeData = 25; h2{2}.MarkerEdgeColor=grey; h2{2}.MarkerFaceColor = grey;
    h3{2}.SizeData = 25; h3{2}.MarkerEdgeColor=grey; h3{2}.MarkerFaceColor = grey;
    h4{2}.SizeData = 25; h4{2}.MarkerEdgeColor=grey; h4{2}.MarkerFaceColor = grey;
    %make scatter align to y-data of boxplot whiskers
    h1{2}.YData = repmat(h1{5}.YData(1), length(h1{2}.YData), 1); 
    h2{2}.YData = repmat(h2{5}.YData(1), length(h2{2}.YData), 1); 
    h3{2}.YData = repmat(h3{5}.YData(1), length(h3{2}.YData), 1); 
    h4{2}.YData = repmat(h4{5}.YData(1), length(h4{2}.YData), 1);
     
    same_lines = plot([h1{2}.XData; h2{2}.XData],[h1{2}.YData; h2{2}.YData],'Color',[0.8 0.8 0.8]);
    diff_lines = plot([h3{2}.XData; h4{2}.XData],[h3{2}.YData; h4{2}.YData],'Color',[0.8 0.8 0.8]);
    uistack(same_lines,'bottom'); uistack(diff_lines,'bottom');
    
    %remove line of density dist
    h1{1}.EdgeColor = 'none';
    h2{1}.EdgeColor = 'none';
    h3{1}.EdgeColor = 'none';
    h4{1}.EdgeColor = 'none';
    
    %add legend 
    legend([h1{1} h2{1} h3{1} h4{1}], {'SAME rem','SAME forg','DIFF rem','DIFF forg'})
    legend('AutoUpdate','off');
    %add labels and title
    title('Rem vs Forg');
    yticklabels({});
    xlabel('ERS (spearman rho)');
    %view mode
    set(gca,'XLim', [-0.25 0.25], 'YLim', [-22 22]);
    if strcmp(roi_tag,'BA39_40')
        set(gca,'XLim', [-0.10 0.25], 'YLim', [-22 32]);
    end
    camroll(90);
    set(gcf,'color','white')
    xline(0,'--')
    
    %post-hoc ttest of rem vs forg
    [~,p,~,stats]= ttest(Y1,Y2);
    disp(['rem vs forg t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    
    title(['t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    
    %post-hoc ttest
    %SAME
    [~,p,~,stats]= ttest(Y1,Y2);
    disp(['SAME rem vs forg t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    %test remembered and forgotten against 0!
    [h,p,ci,stats] = ttest(Y1);
    disp(['same rem vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    [h,p,ci,stats] = ttest(Y2);
    disp(['same forg vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    disp(' ');

    %DIFF
    [~,p,~,stats]= ttest(Y3,Y4);
    disp(['DIFF rem vs forg t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    %test remembered and forgotten against 0!
    [h,p,ci,stats] = ttest(Y3);
    disp(['diff rem vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    [h,p,ci,stats] = ttest(Y4);
    disp(['diff forg vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    disp(' ');

    %INTERACTION
    sm = Y1-Y3;
    df = Y2-Y4;
    [~,p,~,stats]= ttest(sm,df);
    disp(['SAME (rem-forg) vs DIFF (rem-forg) INT t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    
    %% plot (rem-forg) of SAME AND DIFF 
    X1 = ones(length(same_rem),1)*0.5;
    X2 = ones(length(same_rem),1)*1;

    %array values per subject
    Y1 = same_rem - same_forg;
    Y2 = diff_rem - diff_forg;

    %color per subject
    cols = cbrewer('qual','Paired',12);
    cols(cols>1) = 1; cols(cols<0) = 0;
    same_c = cols(2,:);
    diff_c = cols(8,:);
    
    %make it rain
    figure('Position',[450 500 420 350]);
    d = {Y1, Y2};   
    %plot rainclouds
    h1 = raincloud_plot(d{1}, 'box_on', 1, 'color', same_c, 'alpha', 0.8,'box_dodge', 1, 'box_dodge_amount', 0.20, 'dot_dodge_amount', 0.20, 'box_col_match', 1);
    h2 = raincloud_plot(d{2}, 'box_on', 1, 'color', diff_c, 'alpha', 0.8,'box_dodge', 1, 'box_dodge_amount', 0.55, 'dot_dodge_amount', 0.55, 'box_col_match', 1);

    %adjust raindrops
    h1{2}.SizeData = 25; h1{2}.MarkerEdgeColor=grey; h1{2}.MarkerFaceColor = grey;
    h2{2}.SizeData = 25; h2{2}.MarkerEdgeColor=grey; h2{2}.MarkerFaceColor = grey;

    %make scatter align to y-data of boxplot whiskers
    h1{2}.YData = repmat(h1{5}.YData(1), length(h1{2}.YData), 1); 
    h2{2}.YData = repmat(h2{5}.YData(1), length(h2{2}.YData), 1); 
       
    %don't add within subject lines since its individual t-tests against 0
    %lines = plot([h1{2}.XData; h2{2}.XData],[h1{2}.YData; h2{2}.YData],'Color',[0.8 0.8 0.8]);
    %uistack(lines,'bottom'); 
    
    %remove line of density dist
    h1{1}.EdgeColor = 'none';
    h2{1}.EdgeColor = 'none';
    
    %add legend 
    legend([h1{1} h2{1}], {'SAME (rem-forg)','DIFF (rem-forg)'})
    legend('AutoUpdate','off');
    %add labels and title
    title('Memory effect for Same and Diff');
    yticklabels({});
    xlabel('ERS (spearman rho)');
    %view mode
    set(gca,'XLim', [-0.25 0.25], 'YLim', [-22 22]);
    if strcmp(roi_tag,'BA39_40')
        set(gca,'XLim', [-0.10 0.15], 'YLim', [-12 40]);
    end
    camroll(90);
    set(gcf,'color','white')
    xline(0,'--')
    
    %ttest against 0
    [h,p,ci,stats] = ttest(Y1);
    disp(['SAME (rem-forg) vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    [h,p,ci,stats] = ttest(Y2);
    disp(['DIFF (rem-forg) vs 0: t = ' num2str(stats.tstat) ' , p = ' num2str(p) ' , df = ' num2str(stats.df)]);
    disp(' ');
end
