clear all; close all;

dataname = ['F:\oxytocin\OXYT010\OXYT010_004_220126_112539',
            'F:\oxytocin\OXYT011\OXYT011_005_220224_160927',
            'F:\oxytocin\OXYT014\OXYT014_001_220429_155806',
            'F:\oxytocin\OXYT012\OXYT012_001_220323_130702',
            ]

% dataname =  ['F:\LAMP37\LAMP37_006_201201_192319'];
% dataname =  ['F:\LAMP3\LAMP3_006_190613_194519']; % 58  20

RipCh = [23,70,19,44];
GammaCh = [40,103,112,55];

for ii = 1:size(dataname,1)
    cd(dataname(ii,:))
    basepath = pwd;
    basename = bz_BasenameFromBasepath(basepath);
    load([basename,'.SleepState.states.mat']);
    
   
    info = read_Intan_RHD2000_file(basepath);
    analogin= read_Intan_analogin_file('analogin.dat',info.num_board_adc_channels);
    lfp_fbtime = find(analogin(1,:)>(max(analogin(1,:))+min(analogin(1,:)))/2,1)
    if isempty(lfp_fbtime) == 1
        lfp_fbtime = [0]
    else lfp_fbtime(1,2) = find(analogin(1,:)>0.6,1,'last');
    end 
    
    % sitmulation and signal
    if exist('dFoverF.mat')==0
        name = basename(1:11);
        ext = '.mat'
        load([name,ext],[name,'_AChSti']);
        ACh_sti = eval([name,'_AChSti']);
        otr = 0.5;
        fbtime = ACh_sti.times(find(ACh_sti.values(:,1)>otr,1));fbtime(1,2) = ACh_sti.times(find(ACh_sti.values(:,1)>otr,1,'last'));
        fbIDX = round(fbtime*20000);
        load([name,ext],[name,'_AChSig1']);
        ACh_signal = eval([name,'_AChSig1']);
        ACh_signal.data = downsample(ACh_signal.values((fbIDX(1,1)):(fbIDX(1,2))),200);
        ACh_signal.time = downsample(ACh_signal.times((fbIDX(1,1)):(fbIDX(1,2))),200)- fbtime(1,1);
        p = [];
        p = polyfit(ACh_signal.time,ACh_signal.data,5);
        Ybaseline = polyval(p,ACh_signal.time);
        figure
        plot(ACh_signal.time, ACh_signal.data); hold on;
        plot(ACh_signal.time,Ybaseline);
        dFoverF = smoothdata((ACh_signal.data - Ybaseline)./Ybaseline,'movmean',10);
        hold on;
        plot(ACh_signal.time, dFoverF);xlim([0 inf]);
    else load('dFoverF.mat'); figure;plot(dFoverF);
    end
    %% find dFF peak or trough
%     durations = 250;
%     tr = [80 280]*100; %% wake [80 280] NREM [650 850];
%     [pks locs width] = findpeaks(-dFoverF(tr(1):tr(2)) ,'minpeakheight',mean(-dFoverF(tr(1):tr(2)))+0*std(-dFoverF(tr(1):tr(2))),...
%     'WidthReference','halfprom','MinPeakDistance',durations(1)*2);

%% get LFP
    THLFPs = bz_GetLFP(GammaCh(ii),'basepath',basepath,'noPrompts',true);
    THLFPs.data = double(THLFPs.data);
    THLFPs.data(abs(THLFPs.data)>5000) = nan;
    THLFPs.data = fillmissing(THLFPs.data,'linear');

    RipLFPs = bz_GetLFP(RipCh(ii),'basepath',basepath,'noPrompts',true);
    RipLFPs.data = double(RipLFPs.data);
    RipLFPs.data(abs(RipLFPs.data)>5000) = nan;
    RipLFPs.data = fillmissing(RipLFPs.data,'linear');

   % bz_eventWavelet

    datpath = 'G:\My Drive\Papers_Buzsaki\Paper_ACh_OXT\Fig3\Figure3B';
    save(fullfile(datpath,[basename,'Oxt_LFP.mat']),'RipLFPs','THLFPs','lfp_fbtime', 'dFoverF')

end