idx_out = TotRespT_StopCorr_Jump > 1000;
TotRespT_StopCorr_Jump(idx_out) = NaN;

SSD_StopCorr(SSD_StopCorr == 0) = NaN;
[val,loc] = nanmin(SSD_StopCorr);
SSD_StopCorr(loc) = NaN;

SSD_StopFail(SSD_StopFail == 0) = NaN;
[val,loc] = nanmin(SSD_StopFail);
SSD_StopFail(loc) = NaN;

figure()
scatter(SSD_StopCorr, TotRespT_StopCorr_Jump); lsline;
hold on; 
scatter(SSD_StopCorr, MT2_StopCorr_Jump); lsline
scatter(SSD_StopFail, TotRespT_StopFail_Jump); lsline;
legend('StopCorr-TRT','StopCorr-TRT','StopCorr-MT','StopCorr-MT','StopFail-TRT','StopFail-TRT','Location','northwest');

figure()
scatter(1, TotRespT_GoCorr); 

median_Go = round(median(TotRespT_GoCorr,'omitnan'))
median_StopCorr = round(median(TotRespT_StopCorr_Jump,'omitnan'))
median_StopFail = round(median(TotRespT_StopFail_Jump,'omitnan'))


% SSD_StopCorr(SSD_StopCorr > 260) = NaN;
idx_lowSSD = SSD_StopCorr < mean(SSD_StopCorr, 'omitnan');
TRT_StopCorr_lowSSD = TotRespT_StopCorr_Jump(idx_lowSSD);
median_StopCorr_lowSSD = round(median(TRT_StopCorr_lowSSD,'omitnan'))

TRT_StopCorr_highSSD = TotRespT_StopCorr_Jump(~idx_lowSSD);
median_StopCorr_highSSD = round(median(TRT_StopCorr_highSSD,'omitnan'))

idx_lowSSD_fail = SSD_StopFail < mean(SSD_StopFail, 'omitnan');
TRT_StopFail_lowSSD = TotRespT_StopFail_Jump(idx_lowSSD_fail);
median_StopFail_lowSSD = round(median(TRT_StopFail_lowSSD,'omitnan'))
TRT_StopFail_highSSD = TotRespT_StopFail_Jump(~idx_lowSSD_fail);
median_StopFail_highSSD = round(median(TRT_StopFail_highSSD,'omitnan'))


%%
SSD_Stop_Jump_mn;

mouseY_GoCorr_left = nanmean(-mouseY(trl_GoCorr_left,:));
mouseY_GoCorr_right = nanmean(-mouseY(trl_GoCorr_right,:));
mouseY_GoCorr_both = nanmean([mouseY_GoCorr_left;mouseY_GoCorr_right]);
xx = diff(mouseY_GoCorr_both);

mouseT_GoCorr_left = nanmean(mouseT(trl_GoCorr_left,:));
mouseT_GoCorr_right = nanmean(mouseT(trl_GoCorr_right,:));
% mouseT_GoCorr_both = nanmean([mouseT_GoCorr_left;mouseT_GoCorr_right]);
mouseT_GoCorr_both = nanmean([mouseT_GoCorr_left;mouseT_GoCorr_right]) - SSD_Stop_Jump_mn;

mouseY_StopCorr_Jump_both = -mouseY(trl_StopCorr_Str,:);
% mouseT_StopCorr_Jump_both = mouseT(trl_StopCorr_Str,:);
mouseT_StopCorr_Jump_both = mouseT(trl_StopCorr_Str,:) - dataTable.StopSignalDelay(trl_StopCorr_Str);

idx_lowSSD = SSD_StopCorr < mean(SSD_StopCorr, 'omitnan');
mouseY_StopCorr_Jump_both_lowSSD = mean(mouseY_StopCorr_Jump_both(idx_lowSSD,:),'omitnan');
mouseT_StopCorr_Jump_both_lowSSD = mean(mouseT_StopCorr_Jump_both(idx_lowSSD,:),'omitnan');

mouseY_StopCorr_Jump_both_highSSD = mean(mouseY_StopCorr_Jump_both(~idx_lowSSD,:),'omitnan');
mouseT_StopCorr_Jump_both_highSSD = mean(mouseT_StopCorr_Jump_both(~idx_lowSSD,:),'omitnan');


mouseY_StopFail_Jump_both = -mouseY(trl_StopFail_Str,:);
% mouseT_StopFail_Jump_both = mouseT(trl_StopFail_Str,:);
mouseT_StopFail_Jump_both = mouseT(trl_StopFail_Str,:) - dataTable.StopSignalDelay(trl_StopFail_Str);

mouseY_StopFail_Jump_both_lowSSD = mean(mouseY_StopFail_Jump_both(idx_lowSSD_fail,:),'omitnan');
mouseT_StopFail_Jump_both_lowSSD = mean(mouseT_StopFail_Jump_both(idx_lowSSD_fail,:),'omitnan');

mouseY_StopFail_Jump_both_highSSD = mean(mouseY_StopFail_Jump_both(~idx_lowSSD_fail,:),'omitnan');
mouseT_StopFail_Jump_both_highSSD = mean(mouseT_StopFail_Jump_both(~idx_lowSSD_fail,:),'omitnan');

%% get nearest values so I know when to truncate time
dummy_go = repmat(median_Go,[1 length(mouseT_GoCorr_both)]);
[minValue,closestIndex_Go] = min(abs(dummy_go-mouseT_GoCorr_both));
closestValue_Go = mouseT_GoCorr_both(closestIndex_Go) ;

dummy_SS_lowSSD = repmat(median_StopCorr_lowSSD,[1 length(mouseT_StopCorr_Jump_both_lowSSD)]);
[minValue,closestIndex_SS_lowSSD] = min(abs(dummy_SS_lowSSD-mouseT_StopCorr_Jump_both_lowSSD));
closestValue_SS_lowSSD = mouseT_StopCorr_Jump_both_lowSSD(closestIndex_SS_lowSSD) ;

dummy_SS_highSSD = repmat(median_StopCorr_highSSD,[1 length(mouseT_StopCorr_Jump_both_highSSD)]);
[minValue,closestIndex_SS_highSSD] = min(abs(dummy_SS_highSSD-mouseT_StopCorr_Jump_both_highSSD));
closestValue_SS_highSSD = mouseT_StopCorr_Jump_both_highSSD(closestIndex_SS_highSSD) ;

dummy_FS_lowSSD = repmat(median_StopFail_lowSSD,[1 length(mouseT_StopFail_Jump_both_lowSSD)]);
[minValue,closestIndex_FS_lowSSD] = min(abs(dummy_FS_lowSSD-mouseT_StopFail_Jump_both_lowSSD));
closestValue_FS_lowSSD = mouseT_StopFail_Jump_both_lowSSD(closestIndex_FS_lowSSD) ;

dummy_FS_highSSD = repmat(median_StopFail_highSSD,[1 length(mouseT_StopFail_Jump_both_highSSD)]);
[minValue,closestIndex_FS_highSSD] = min(abs(dummy_FS_highSSD-mouseT_StopFail_Jump_both_highSSD));
closestValue_FS_highSSD = mouseT_StopFail_Jump_both_highSSD(closestIndex_FS_highSSD);

%%
figure()
plot(mouseT_GoCorr_both(1,1:closestIndex_Go),...
    mouseY_GoCorr_both(1,1:closestIndex_Go),'g','LineWidth',2);
hold on
plot(mouseT_StopCorr_Jump_both_lowSSD(1,1:closestIndex_SS_lowSSD),...
    mouseY_StopCorr_Jump_both_lowSSD(1,1:closestIndex_SS_lowSSD),'b','LineWidth',2);
plot(mouseT_StopCorr_Jump_both_highSSD(1,1:closestIndex_SS_highSSD),...
    mouseY_StopCorr_Jump_both_highSSD(1,1:closestIndex_SS_highSSD),'b--','LineWidth',2);

plot(mouseT_StopFail_Jump_both_lowSSD(1,1:closestIndex_FS_lowSSD),...
    mouseY_StopFail_Jump_both_lowSSD(1,1:closestIndex_FS_lowSSD),'r','LineWidth',2);
plot(mouseT_StopFail_Jump_both_highSSD(1,1:closestIndex_FS_highSSD),...
    mouseY_StopFail_Jump_both_highSSD(1,1:closestIndex_FS_highSSD),'r--','LineWidth',2);
xlabel('time (ms)');
ylabel('y position');
legend('Go','SSlowSSD','SShighSSD','FSlowSSD','FShighSSD','Location','northwest')

diff_Y_SS_lowSSD_vs_Go = mouseY_GoCorr_both - mouseY_StopCorr_Jump_both_lowSSD;
mean_T_SS_lowSSD_vs_Go = mean([mouseT_GoCorr_both; mouseT_StopCorr_Jump_both_lowSSD]);

diff_Y_SS_highSSD_vs_Go = mouseY_GoCorr_both - mouseY_StopCorr_Jump_both_highSSD;
mean_T_SS_highSSD_vs_Go = mean([mouseT_GoCorr_both; mouseT_StopCorr_Jump_both_highSSD]);

figure()
plot(mean_T_SS_lowSSD_vs_Go, diff_Y_SS_lowSSD_vs_Go, 'b-','LineWidth',2);
hold on
plot(mean_T_SS_highSSD_vs_Go, diff_Y_SS_highSSD_vs_Go, 'b--','LineWidth',2);
xlim([0,median_StopCorr_lowSSD]);

%% Do same for for X 

mouseX_GoCorr_left = nanmean(mouseX(trl_GoCorr_left,:));
mouseX_GoCorr_right = nanmean(mouseX(trl_GoCorr_right,:));
mouseX_GoCorr_both = nanmean([mouseX_GoCorr_left;mouseX_GoCorr_right]);
% convert mouse X to that all going in same direction (poor/easy way)
center_GoCorr_X = (mouseX_GoCorr_both(:,1));
mouseX_GoCorr_both = abs(mouseX_GoCorr_both - center_GoCorr_X);
xx = diff(mouseX_GoCorr_both);

mouseX_StopCorr_Jump_both = mouseX(trl_StopCorr_Str,:);
% convert mouse X to that all going in same direction (poor/easy way)
center_StopCorr_X = (mouseX_StopCorr_Jump_both(:,1));
mouseX_StopCorr_Jump_both = abs(mouseX_StopCorr_Jump_both - center_StopCorr_X);

idx_lowSSD = SSD_StopCorr < mean(SSD_StopCorr, 'omitnan');
mouseX_StopCorr_Jump_both_lowSSD = mean(mouseX_StopCorr_Jump_both(idx_lowSSD,:),'omitnan');

mouseX_StopCorr_Jump_both_highSSD = mean(mouseX_StopCorr_Jump_both(~idx_lowSSD,:),'omitnan');

mouseX_StopFail_Jump_both = mouseX(trl_StopFail_Str,:);
% convert mouse X to that all going in same direction (poor/easy way)
center_StopFail_X = (mouseX_StopFail_Jump_both(:,1));
mouseX_StopFail_Jump_both = abs(mouseX_StopFail_Jump_both - center_StopFail_X);

mouseX_StopFail_Jump_both_lowSSD = mean(mouseX_StopFail_Jump_both(idx_lowSSD_fail,:),'omitnan');

mouseX_StopFail_Jump_both_highSSD = mean(mouseX_StopFail_Jump_both(~idx_lowSSD_fail,:),'omitnan');


%%
figure()
plot(mouseT_GoCorr_both(1,1:closestIndex_Go),...
    mouseX_GoCorr_both(1,1:closestIndex_Go),'g','LineWidth',2);
hold on
plot(mouseT_StopCorr_Jump_both_lowSSD(1,1:closestIndex_SS_lowSSD),...
    mouseX_StopCorr_Jump_both_lowSSD(1,1:closestIndex_SS_lowSSD),'b','LineWidth',2);
plot(mouseT_StopCorr_Jump_both_highSSD(1,1:closestIndex_SS_highSSD),...
    mouseX_StopCorr_Jump_both_highSSD(1,1:closestIndex_SS_highSSD),'b--','LineWidth',2);

plot(mouseT_StopFail_Jump_both_lowSSD(1,1:closestIndex_FS_lowSSD),...
    mouseX_StopFail_Jump_both_lowSSD(1,1:closestIndex_FS_lowSSD),'r','LineWidth',2);
plot(mouseT_StopFail_Jump_both_highSSD(1,1:closestIndex_FS_highSSD),...
    mouseX_StopFail_Jump_both_highSSD(1,1:closestIndex_FS_highSSD),'r--','LineWidth',2);
xlabel('time (ms)');
ylabel('X position');
legend('Go','SSlowSSD','SShighSSD','FSlowSSD','FShighSSD','Location','northwest')

diff_X_SS_lowSSD_vs_Go = mouseX_GoCorr_both - mouseX_StopCorr_Jump_both_lowSSD;
mean_T_SS_lowSSD_vs_Go = mean([mouseT_GoCorr_both; mouseT_StopCorr_Jump_both_lowSSD]);

diff_X_SS_highSSD_vs_Go = mouseX_GoCorr_both - mouseX_StopCorr_Jump_both_highSSD;
mean_T_SS_highSSD_vs_Go = mean([mouseT_GoCorr_both; mouseT_StopCorr_Jump_both_highSSD]);

figure()
plot(mean_T_SS_lowSSD_vs_Go, diff_X_SS_lowSSD_vs_Go, 'b-','LineWidth',2);
hold on
plot(mean_T_SS_highSSD_vs_Go, diff_X_SS_highSSD_vs_Go, 'b--','LineWidth',2);
xlim([0,median_StopCorr_lowSSD]);