%% Plotting the level of spontaneous recovery relative to learning in A: 
clearvars                                                               
currentFolder = pwd;
addpath(genpath('Matlab_functions'))
path = currentFolder(1:end-8);
%% in case of only few angles:
plot_angle = [-35,-15,0,15,35]; %angles to plot, in order to plot them 
k  = 11; % size of the window

%% loading in the restructured AI data:
load(sprintf('%s%s%',path,'data\restructured_Fig_4_5_8.mat'),'TA','FF1','SR')   
Apos = unique(TA(~isnan(TA)));
Apos(Apos>180) = Apos(Apos>180)-360; % all possible target locations
%% Loading in the colour matrix:
% col(shade,RGBcode,colour)
[~,col] =DefColours(5,5);
%%
n_Y_ticks = 6;
%% 
CW = [-1,-1,-1,1,1,1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,-1,-1,-1,1,1,-1];      % -1 no/CCW-CW; 1 yes/CW-CCW 
CW(11) = NaN;
ForceFieldGroup = [1, -1];
%%  
position = [0 0 18 11];
figure('units','centimeter',...
       'innerposition',position)
set(gca, 'FontName', 'Arial')

%% axis limits limits
Ylim = [-1.3,1.3];
Xlim = [-15,50];

%% axis ticks
Xticks  = [-10:10:45];
XtiLa =   {[] [] [] [] [] [] [] [] [] [] [];...
           ['FF1',num2cell([0:10:45])] [] [] [] [] [] [] [] [] [] []}; % YtickLabels
Yticks = [-1.5:0.5:1.5];
YtiLa =  {[-1.5:0.5:1.5] [] [] [] [] [] [] [] [] [] [];...
          [-1.5:0.5:1.5] [] [] [] [] [] [] [] [] [] []};   


%% in rows CW CCW
M = NaN(11,44); % mean of the sliding window
SE= NaN(11,44); % standard error of mean of sliding window

ind2 = 1:44; % clamp trials in spontaneous recovery per block
%% number of rows and columns
nR = length(ForceFieldGroup);
nC = length(plot_angle);
%%
out = zeros(2,11); % data points falling out of the plot
ToTN = zeros(2,11); % data points in plot
for cw = 1:2

    for angle = 1:length(plot_angle)
        p1{angle,cw} = subplot(nR,nC,(cw-1)*nC+angle); % the subplot
        ind_angle = find(Apos == plot_angle(angle)); % find the location in SR of that angle
        %% plotting last force field trial in A per block (maximum possible of spontaneous recovery)
        x = ones(size(FF1(CW == ForceFieldGroup(cw),:,ind_angle,4)))*-10+[-2:1:2];
        D = FF1(CW == ForceFieldGroup(cw),:,ind_angle,4);
        m = squeeze(nanmean(FF1(CW == ForceFieldGroup(cw),:,ind_angle,4),[1]));
        sd = squeeze(nanstd(FF1(CW == ForceFieldGroup(cw),:,ind_angle,4)));
        n = sum(~isnan(FF1(CW == ForceFieldGroup(cw),:,ind_angle,4)));
        %% plotting individual participant data
        scatter(x(:),D(:),2*ones(size(x(:))),ones(size(x(:))).*[0.25,0.25,0.25],'filled','o','MarkerFaceAlpha',0.25); 
        hold on
        %% checking what falls out of the plot:
        out(cw,angle) = sum(D(:)<Ylim(1))+sum(D(:)>Ylim(2));
        ToTN(cw,angle) = sum(~isnan(D(:)));
        %sprintf('%.5f%s of datapoints fall out fo the plot',sum(out(cw,angle),'all')/sum(ToTN(cw,angle),'all')*100,'%')
        
        %% mean and SE
        plot(mean(x),m,'-','color',col(1,:,7))
        patch([mean(x),flip(mean(x))],[m+sd./sqrt(n),flip(m-sd./sqrt(n))],col(1,:,7),'FaceAlpha',0.5,'LineStyle','none') 
        %% plot line at 0
        plot([-10,44],[0,0],'k-')

        %% sliding window mean 
         %% plot individual participant data:
         D = permute(SR(CW ==ForceFieldGroup(cw),:,ind_angle,:),[4,1,2,3]);
         x = ones(size(D)).*ind2';
         scatter(x(:),D(:),2*ones(size(x(:))),ones(size(x(:))).*[0.25,0.25,0.25],'filled','o','MarkerFaceAlpha',0.25); 
         %% checking what falls out of the plot:
         out(cw,angle) = out(cw,angle)+sum(D(:)<Ylim(1))+sum(D(:)>Ylim(2));
         ToTN(cw,angle) = ToTN(cw,angle)+sum(~isnan(D(:)));
         %sprintf('%.5f%s of datapoints fall out fo the plot',sum(out(cw,angle),'all')/sum(ToTN(cw,angle),'all')*100,'%')

         %% sliding window mean and SE:

        [M(ind_angle,:),SE(ind_angle,:)] = smoothing_AI(ind2,k,D,0);
       
        indSR = find(~isnan(M(ind_angle,:)));
        
        patch([indSR,flip(indSR)],[M(ind_angle,indSR)-SE(ind_angle,indSR),flip(M(ind_angle,indSR)+SE(ind_angle,indSR))],col(1,:,10),'FaceAlpha',0.5,'LineStyle','none')
        hold on
        plot(indSR,M(ind_angle,indSR),'color',col(1,:,10))
       
        
        
        %% title and limits:
        if cw == 1 
            title(sprintf('%d%c',Apos(ind_angle), char(176)))
        end
        xlim(Xlim)
        ylim(Ylim)
        xticks(Xticks)
        yticks(Yticks)
        xticklabels(XtiLa{cw,angle})
        yticklabels(YtiLa{cw,angle})
               
       
        
    end
    sprintf('Angles together: %.5f%s of datapoints fall out fo the plot',sum(out(cw,:),'all')/sum(ToTN(cw,:),'all')*100,'%')
        
end

%% moving the plots to the correct position in the figure
%% space on the:
%    left       between  right
Tw = [180/1920, 20/1920, 10/1920];
% space on the:
%    bottom     between  top
Th = [100/1080, 20/1080, 60/1080]; 
%% width of one subplot 
w = (1-sum(Tw.*[1,nC-1,1]))/nC;
h = (1-sum(Th.*[1,nR-1,1]))/nR;

xSPP = Tw(1):(w+Tw(2)):1-w-Tw(3);
ySPP = 1-h-Th(3):-(h+Th(2)):Th(1);%linspace(1-h-n/2*Th,n/2*Th,nR);

for cw = 1:nR
   for angle = 1:nC
       p1{angle,cw}
        if cw == 2 && angle == 1
            set(p1{angle,cw}.XLabel,'String', 'Trial')
        end
        if angle == 1
            set(p1{angle,cw}.YLabel,'String', 'Adaptation Index')
        end
        set(p1{angle,cw},'position',[xSPP(angle),ySPP(cw),w,h],'FontName', 'Arial','FontSize',8)
        
   end
end
   

 %% xlabel    
% hxlabel = annotation('textbox',[ 0.1719 0.0370 0 0],...
%     'String','Trial',...
%     'EdgeColor','none',...
%     'FontName', 'Arial','FontSize',10,...
%     'FitBoxToText','on',...
%     'horizontalalignment', 'center',...
%     'verticalalignment', 'middle');

% %% ylabel
% AIlabel1 = annotation('textarrow',...
%     'Position', [ 0.1275 0.33 0 0],...
%     'HorizontalAlignment','Center',...
%     'VerticalAlignment','middle',...
%     'String','Adaptation Index',...
%     'HeadStyle', 'none',...
%     'LineStyle', 'none',...
%     'FontName', 'Arial','FontSize',10,...
%     'TextRotation',90);
% %% ylabel
% AIlabel2 = annotation('textarrow',...
%     'Position', [ 0.1275 0.66 0 0],...
%     'HorizontalAlignment','Center',...
%     'VerticalAlignment','middle',...
%     'String','Adaptation Index',...
%     'HeadStyle', 'none',...
%     'LineStyle', 'none',...
%     'FontName', 'Arial','FontSize',10,...
%     'TextRotation',90);
%% A B
Alabel = annotation('textbox',[0.0094 0.9259 0 0],...
    'String','A',...
    'EdgeColor','none',...
    'FontName', 'Arial','FontSize',12,...
    'FitBoxToText','on',...
    'horizontalalignment', 'center',...
    'verticalalignment', 'middle');

Blabel = annotation('textbox',[0.0094 0.4907 0 0],...
    'String','B',...
    'EdgeColor','none',...
    'FontName', 'Arial','FontSize',12,...
    'FitBoxToText','on',...
    'horizontalalignment', 'center',...
    'verticalalignment', 'middle');

%% CW CCW CCW CW
CWlabel = annotation('textbox',[0.0938 0.9259 0 0],...
    'String','CW-CCW',...
    'EdgeColor','none',...
    'FontName', 'Arial','FontSize',10,...
    'FitBoxToText','on',...
    'horizontalalignment', 'left',...
    'verticalalignment', 'middle');

CCWlabel = annotation('textbox',[0.0938 0.4907 0 0],...
    'String','CCW-CW',...
    'EdgeColor','none',...
    'FontName', 'Arial','FontSize',10,...
    'FitBoxToText','on',...
    'horizontalalignment', 'left',...
    'verticalalignment', 'middle');
ax = gca;
ax.FontSize = 8; 
%% saving figure
saveas(gcf,sprintf('%s%s.pdf',path,'Figures\Fig_5'))