%% plotting Generalization in A over blocks per target angle:
clearvars                                                               
currentFolder = pwd;
addpath(genpath('Matlab_functions'))
path = currentFolder(1:end-8);

%% loading in the restructured AI data
load(sprintf('%s%s%',path,'data\restructured_Fig_4_5_8.mat'),'FF1','TA')
TA(TA>180) = TA(TA>180) -360; % such that training location is in the center
Apos = unique(TA(~isnan(TA))); % all possible target locations
%% Loading in the colour matrix:
% col(shade,RGBcode,colour)
[~,col] =DefColours(5,5);
plotcol = [19,7,2,2];
Xlabel={[],[]};
Ylabel={'Adaptation Index','Adaptation Index'};
%%
Ylim = {  [-0.25,1.25] [-1.25,0.5]};
n_Y_ticks = 6;
%% excluding 11 
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];
%% ticks and labels      
Xticks = [Apos];
%Xticklabels = {[ ] [{sprintf('-60%c', char(176)) sprintf('-35%c', char(176)) sprintf('-15%c', char(176)) '' '' sprintf('0%c', char(176)) '' '' sprintf('15%c', char(176)) sprintf('35%c', char(176)) sprintf('60%c', char(176))} ] [] [] [] [] [] [] [] []};
Xticklabels = {[ ] [{sprintf('-60') sprintf('-35') sprintf('-15') '' '' sprintf('0') '' '' sprintf('15') sprintf('35') sprintf('60')} ] [] [] [] [] [] [] [] []};


position = [0 0 12 11];
figure('units','centimeter',...
       'innerposition',position)
set(gca, 'FontName', 'Arial')
%%
out = zeros(2); % data points falling out of the plot
ToTN = zeros(2); % data points in plot
for c =1:2
    p1{c} = subplot(1,2,c);
    %% individual Data:
    x_data = squeeze(mean(TA(CW ==ForceFieldGroup(c),:,:,:),[4]))+[-2:1:2];
    D = nanmean(FF1(CW ==ForceFieldGroup(c),:,:,:),4); 
    scatter(x_data(:),D(:),2*ones(size(x_data(:))),ones(size(x_data(:))).*[0,0,0],'filled','o','MarkerFaceAlpha',0.5)
    %% checking what falls out of the plot:
    out(c) = sum(D(:)<Ylim{c}(1))+sum(D(:)>Ylim{c}(2));
    ToTN(c) = sum(~isnan(D(:)));
    sprintf('%.5f%s of datapoints fall out fo the plot',sum(out(c),'all')/sum(ToTN(c),'all')*100,'%')
    hold on
    %% mean and SE per target angle and block
    %% x axis target angle seperated by block
    x = mean(squeeze(mean(TA(CW ==ForceFieldGroup(c),:,:,:),[1,4])))+[-2:1:2]';
    m = squeeze(nanmean(FF1(CW ==ForceFieldGroup(c),:,:,:),[1,4]));
    sd = squeeze(nanstd(nanmean(FF1(CW ==ForceFieldGroup(c),:,:,:),4)));
    n = size(FF1(CW ==ForceFieldGroup(c),:,:,:),1);
    plot(x,m,'-','color',col(1,:,plotcol(2)))
    patch([x;flip(x)],[m+sd/sqrt(n);flip(m-sd/sqrt(n))],col(1,:,plotcol(2)),'FaceAlpha',0.5,'LineStyle','none') 
    %% setting figure limits:
    ylim(Ylim{c})
    xlim([-70,70])
    xticks(Xticks)
    xticklabels(Xticklabels{c})
    yticks(linspace(Ylim{c}(1),Ylim{c}(2),n_Y_ticks))
   
    xlabel(Xlabel{c},'FontName', 'Arial','FontSize',8)
    ylabel(Ylabel{c},'FontName', 'Arial','FontSize',8)
    %grid on
    
    
end

 


%% moving the plots around
%      motion based      plan based   trac-         generalization
%    CW CCW CCW CW

%% for restructuring figure
%figure('units','normalized','outerposition',[0 0 1 1])
nR =    2; % rows
nC =    2; % columns
Tw = [220/1920, 20/1920, 2/1920];
Th = [120/1080, 40/1080, 20/1080];
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);

Px = [1     1];
Py = [1     2];
Sx = [2     2];
Sy = [1     1];
for i = 1:2
     set(p1{i},'position',[xSPP(Px(i)),ySPP(Py(i)),w*Sx(i)+Tw(2)*(Sx(i)-1),h*Sy(i)+Th(2)*(Sy(i)-1)],'FontName', 'Arial','FontSize',8,'box','off')
end

%% xlabel    
hxlabel = annotation('textbox',[0.55  0.044   0    0],...
    'String',sprintf('Target Direction (%c)',char(176)),...
    'EdgeColor','none',...
    'FontName', 'Arial','FontSize',10,...
    'FitBoxToText','on',...
    'horizontalalignment', 'center',...
    'verticalalignment', 'middle');

% %% ylabel
% AIlabel = annotation('textarrow',...
%     'Position', [0.16 0.53 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.0115    0.97 0 0],...
    'String','A',...
    'EdgeColor','none',...
    'FontName', 'Arial','FontSize',13,...
    'FitBoxToText','on',...
    'horizontalalignment', 'center',...
    'verticalalignment', 'middle');

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

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

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

%% saving figure
saveas(gcf,sprintf('%s%s.pdf',path,'Figures\Fig_4'))