set(0, 'DefaultLineLineWidth', 2);
set(0,'defaultTextFontSize', 12);
set(0,'defaultAxesFontSize',12);
set(0,'DefaultFigureColormap',jet)
set(0,'defaultfigurecolor',[1 1 1])
set(0,'DefaultAxesTitleFontWeight', 'normal')
set(0, 'DefaultAxesFontName', 'Arial');
set(0, 'DefaultLineLineWidth', 1.5);

%%
clear all
close all
clc
animalNames = {'OXYT025';
    'OXYT026';
    'OXYT027'};

fs = 20e3;
fs_dff = 100;
time_window = 20;

sessionCount = 0;
CNOTimes= [2 3]*3600;
dfFAllCNO1stHalf = []
CNOTimes1stHalf= [0.05 0.55]*3600;

for anID = 1:length(animalNames)
    Names_CNO = dir(['CNO/',animalNames{anID},'*CNO.mat']);
    filenamesCNO = {Names_CNO.name};
    foldernameCNO = {Names_CNO.folder};
    for cnID = 1:size(Names_CNO,1)
        sessionCount = sessionCount + 1
        load(fullfile(foldernameCNO{cnID},filenamesCNO{cnID}));
        if ~isfield(OXT_CNO,'lfp_fbtime')
            Tstart = 9;
        else
            Tstart =OXT_CNO.lfp_fbtime(1)/fs;
        end
        Oxt_ripples_dFF = OXT_CNO.Oxt_dFF;
        N_dFF = length(Oxt_ripples_dFF);
        ripple_times = OXT_CNO.ripples.peaks -Tstart;
        dFoverF = zscore(Oxt_ripples_dFF);

        dFF_event_mean = [];
        for i = 1:(length(ripple_times))
            if ripple_times(i)>CNOTimes(1) &&  ripple_times(i)<CNOTimes(2)
                idx = round(ripple_times(i)*fs_dff);
                if (idx-time_window*fs_dff >0) && (idx+time_window*fs_dff<N_dFF)
                    a = dFoverF(idx-time_window*fs_dff:idx+time_window*fs_dff);
                    dFF_event_mean = [dFF_event_mean a];
                end
            end
        end

        dfFAllCNO(sessionCount,:)=  mean(dFF_event_mean,2);

        dFF_event_mean2 = [];
        for i = 1:(length(ripple_times))
            if ripple_times(i)>CNOTimes1stHalf(1) &&  ripple_times(i)<CNOTimes1stHalf(2)
                idx = round(ripple_times(i)*fs_dff);
                if (idx-time_window*fs_dff >0) && (idx+time_window*fs_dff<N_dFF)
                    a = dFoverF(idx-time_window*fs_dff:idx+time_window*fs_dff);
                    dFF_event_mean2 = [dFF_event_mean2 a];
                end
            end
        end
        dfFAllCNO1stHalf(sessionCount,:)=  mean(dFF_event_mean2,2);

    end

end

%%
dfFAllCtrl =[];
sessionCount = 0
for anID = 1:length(animalNames)
    Names_Control = dir(['Control/',animalNames{anID},'*CNO_Ctrl.mat']);
    filenamesCtrl = {Names_Control.name};
    foldernameCtrl = {Names_Control.folder};
    for ctID = 1:size(Names_Control,1)
        sessionCount = sessionCount + 1
        load(fullfile(foldernameCtrl{ctID},filenamesCtrl{ctID}));
        if ~isfield(OXT_CNO_Ctrl,'lfp_fbtime')
            Tstart = 0;
        else
            Tstart =OXT_CNO_Ctrl.lfp_fbtime(1)/fs;
        end
        Oxt_ripples_dFF = OXT_CNO_Ctrl.Oxt_dFF;
        N_dFF = length(Oxt_ripples_dFF);
        ripple_times = OXT_CNO_Ctrl.ripples.peaks -Tstart;
        dFoverF = zscore(Oxt_ripples_dFF);

        dFF_event_mean = [];
        for i = 1:(length(ripple_times))
                idx = round(ripple_times(i)*fs_dff);
                if (idx-time_window*fs_dff >0) && (idx+time_window*fs_dff<N_dFF)
                    a = dFoverF(idx-time_window*fs_dff:idx+time_window*fs_dff);
                    dFF_event_mean = [dFF_event_mean a];
                end
        end

        dfFAllCtrl(sessionCount,:) = (mean(dFF_event_mean,2));


    end

end

% OXT 27 control session was problematic, using first half hour of CNO
% session
CNOTimes= [0.05 0.55]*3600;
if ~isfield(OXT_CNO,'lfp_fbtime')
    Tstart = 0;
else
    Tstart =OXT_CNO.lfp_fbtime(1)/fs;
end
Oxt_ripples_dFF = OXT_CNO.Oxt_dFF;
N_dFF = length(Oxt_ripples_dFF);
ripple_times = OXT_CNO.ripples.peaks -Tstart;
dFoverF = zscore(Oxt_ripples_dFF);

dFF_event_mean = [];
for i = 1:(length(ripple_times))
    if ripple_times(i)>CNOTimes(1) &&  ripple_times(i)<CNOTimes(2)
        idx = round(ripple_times(i)*fs_dff);
        if (idx-time_window*fs_dff >0) && (idx+time_window*fs_dff<N_dFF)
            a = dFoverF(idx-time_window*fs_dff:idx+time_window*fs_dff);
            dFF_event_mean = [dFF_event_mean a];
        end
    end
end
dfFAllCtrl(sessionCount,:) = (mean(dFF_event_mean,2));
%%
fig1 = figure();
options.handle     =  fig1;
set(gcf,'Position',[100 100 600 400])
options.color_area = [255 0 0]./255;    
options.color_line = [255 0 0]./255;
options.alpha      = 0.2;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = linspace(-time_window, time_window, size(dfFAllCtrl,2));
tbaseline = 5;
 meanBaseLines = squeeze(mean(dfFAllCNO(:,1:tbaseline*fs_dff),2));
dFF_plot= dfFAllCNO-meanBaseLines;
plot_areaerrorbar(dFF_plot,options)

hold on
options.color_area = [0 0 0]./255;    
options.color_line = [0 0 0]./255;
 meanBaseLines = squeeze(mean(dfFAllCtrl(:,1:tbaseline*fs_dff),2));
dFF_plot2= dfFAllCtrl-meanBaseLines;
plot_areaerrorbar(dFF_plot2(1:end,:),options)
xlabel('Time (s)')
ylabel('Oxt (zscored)')

% options.color_area = [0 0 255]./255;    
% options.color_line = [0 0 255]./255;
%  meanBaseLines = squeeze(mean(dfFAllCNO1stHalf(:,1:tbaseline*fs_dff),2));
% dFF_plot= dfFAllCNO1stHalf-meanBaseLines;
% plot_areaerrorbar(dFF_plot(1:end,:),options)
box off
%exportgraphics(gcf,'OXT_CNO.pdf','BackgroundColor','none','ContentType','vector')

[p,h,stats] = signrank(mean(dFF_plot),mean(dFF_plot2));
aa = mean(dFF_plot)';
bb = mean(dFF_plot2)';