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

function posthoc_cluster_barscatter(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']);
        %if jackknife freq selection:
        %load([dataDir fs fileName fs 'stats' fs 'ERS' fs 'jackknife' fs rsa_tag fs 'wo_' exclude_freqband 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]
    
    for sp = 1:2
        if sp == 1
            gcf();
            subplot(1,2,sp)
            h1 = bar(X1(1),mean(Y1),0.3,'FaceColor',rem_c); hold on;
            h1 = scatter(X1,Y1,60,1:length(rem_rsa),'filled'); 
            h1.MarkerEdgeColor = 'black'; 
            h1.MarkerFaceColor = rem_c; hold on; 

            h2 = bar(X2(1),mean(Y2),0.3,'FaceColor',forg_c); 
            h2 = scatter(X2,Y2,60,1:length(forg_rsa),'filled'); 
            h2.MarkerEdgeColor = 'black'; 
            h2.MarkerFaceColor = forg_c; hold on; 

            xlim([0.75 2.25]); xticks([1 2]); xticklabels({'Remembered','Forgotten'}); hold on;
            for k = 1:length(Y1)
                plot([Y1(k),Y2(k)],'Color',[0.5 0.5 0.5]); hold on;
            end  
        elseif sp == 2
            subplot(1,2,sp)
            h1 = bar(X1(1),mean(Y1),0.3,'FaceColor',rem_c); hold on;
            e1 = errorbar(X1(1),mean(Y1),std(Y1)/sqrt(length(Y1)),'black','CapSize',0);

            h2 = bar(X2(1),mean(Y2),0.3,'FaceColor',forg_c); 
            e2 = errorbar(X2(1),mean(Y2),std(Y2)/sqrt(length(Y2)),'black','CapSize',0);

            %plot([mean(Y1),mean(Y2)],'Color',[0.5 0.5 0.5]); hold on;
        end
    
        %ylim([ymin ymax]);
        ylabel('Item-specific ERS (same-diff)'); yline(0,'black');
        xlim([0.75 2.5]); xticks([1 2]); xticklabels({'Remembered','Forgotten'}); hold on;
        %title([replace(roi_tag,'_',' ') ' ' direction ' cluster'])
        ax = gca; ax.FontSize = 12;
        set(gcf,'color','white');
    end
    %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,:);
    

    %% plot Same Diff wo scatter 
   
    for sp = 1:2 %2 subplots, 1 scatter, 2 bar+sem
        subplot(1,2,sp)

        if sp == 1
            gcf();
            h1 = bar(X1(1),mean(Y1),0.3,'FaceColor',same_rem_c); hold on;
            h1 = scatter(X1,Y1,15,1:length(same_rem),'filled'); h1.MarkerEdgeColor = 'black'; h1.MarkerFaceColor = same_rem_c;
            h2 = bar(X2(1),mean(Y2),0.3,'FaceColor',same_forg_c); hold on; 
            h2 = scatter(X2,Y2,15,1:length(diff_rem),'filled'); h2.MarkerEdgeColor = 'black'; h2.MarkerFaceColor = same_forg_c;
            h3 = bar(X3(1),mean(Y3),0.3,'FaceColor',diff_rem_c); hold on; 
            h3 = scatter(X3,Y3,15,1:length(same_rem),'filled');  h3.MarkerEdgeColor = 'black'; h3.MarkerFaceColor = diff_rem_c;
            h4 = bar(X4(1),mean(Y4),0.3,'FaceColor',diff_forg_c); hold on; 
            h4 = scatter(X4,Y4,15,1:length(diff_forg),'filled'); h4.MarkerEdgeColor = 'black'; h4.MarkerFaceColor = diff_forg_c;

            for k = 1:length(Y1)
                plot([X1(k) X2(k)],[Y1(k),Y2(k)],'Color',[0.8 0.8 0.8]); hold on;
                plot([X3(k) X4(k)],[Y3(k),Y4(k)],'Color',[0.8 0.8 0.8]); hold on;
            end
    
        elseif sp == 2

            %bar with sem
            h1 = bar(X1(1),mean(Y1),0.3,'FaceColor',same_rem_c); hold on;
            e1 = errorbar(X1(1),mean(Y1),std(Y1)/sqrt(length(Y1)),'black','CapSize',0);%,0.3)%'FaceColor',same_forg_c);
            h2 = bar(X2(1),mean(Y2),0.3,'FaceColor',same_forg_c); hold on; 
            e1 = errorbar(X2(1),mean(Y2),std(Y2)/sqrt(length(Y2)),'black','CapSize',0);%,0.3)%'FaceColor',same_forg_c);
            h3 = bar(X3(1),mean(Y3),0.3,'FaceColor',diff_rem_c); hold on; 
            e1 = errorbar(X3(1),mean(Y3),std(Y3)/sqrt(length(Y3)),'black','CapSize',0);%,0.3)%'FaceColor',same_forg_c);
            h4 = bar(X4(1),mean(Y4),0.3,'FaceColor',diff_forg_c); hold on; 
            e1 = errorbar(X4(1),mean(Y4),std(Y4)/sqrt(length(Y4)),'black','CapSize',0);%,0.3)%'FaceColor',same_forg_c);


        end
        %for k = 1:length(Y1)
        %    plot([Y3(k),Y4(k)],'Color',[0.5 0.5 0.5]); hold on;
        %end  

        %colormap(gca,subcol);
        xlim([0 3]); xticks([0.5 1 2 2.5]); xticklabels({'same rem','same forg','diff rem','diff forg'}); xtickangle(45);
        ymax = max(max([same_rem, diff_rem, same_forg, diff_forg]));
        ymin = min(min([same_rem, diff_rem, same_forg, diff_forg]));
        %ylim([ymin ymax]);
        ylabel('ERS spearman rho'); 
        ax = gca; ax.FontSize = 12;
        set(gcf,'color','white');
    end
    %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)]);
end
