%% v2: in the learning trials, there are both correct and error sounds, as same as in test trials.


%%
ind_left_error=0;
ind_correct=0;
ind_right_error=0;

run_correct=[]; % 1 is correct; 0 is incorrect
run_areas=[];
run_ts=[];
run_ang=[];
run_num=0;

% give me 30s to sit into the track and close the curtain
pause(30);
sound(beep0,fs_sin);

% let the rat start from location 1
% i.e. start to record (and count) when the rat reach location 1 the first time
while 1
    %Pause to let some new data load up in NetCom's buffers.
    pause(sample_time);
    %Request all new VT data that has been acquired since the last pass.
    [~, timeStampArray, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
    %Get the X and Y location value for this VT frame. The
    %locationArray is in the form [x1, y1, x2, y2, ... xN, yN].
    time_win=length(VTRecsReturned)./(2*vfs);
    x0 = double(locationArray(end - 1)).*scale_x-x_center;
    y0 = double((-1)*locationArray(end)).*scale_y-y_center;
    if x0>=0
        if x0==0
            x0=0.1;
        end
        ang0=mod(atan(y0/x0),2*pi);
    else
        ang0=mod(atan(y0/x0)+pi,2*pi);
    end
    if strcmp(rot,'Clockwise');
        ang0=2*pi-ang0;
    end
    
    ang0_diff=ang0-Ang_center;
    ang0_diff(ang0_diff<(-1)*pi)=ang0_diff(ang0_diff<(-1)*pi)+2*pi;
    ang0_diff(ang0_diff>pi)=ang0_diff(ang0_diff>pi)-2*pi;
    % -pi < and0_diff < pi
    % and0_diff < 0 means the rat is in front of the corrct spot,
    % otherwise, the rat is behind the spot
    
    % when the rat is in front of location1, and behind the last location
    if ang0_diff(1)<0 && ang0_diff(end)>0
        break
    end
end

% record the starting time
ind_area=1; % reach location1 soon
ts_start=timeStampArray(end);
ts_stop=ts_start+10*60*1000000;
sound(sound_beep_start,sound_beep_fs);
fprintf('The recording is starting....\r');



%% test laps
time_stay=0;
while 1 % when session_time>, break
    %Pause to let some new data load up in NetCom's buffers.
    pause(sample_time);
    %Request all new VT data that has been acquired since the last pass.
    [~, timeStampArray, locationArray, ~, VTRecsReturned, ~] = NlxGetNewVTData(vt_acq_ent);
    session_time=timeStampArray(end);
    %Get the X and Y location value for this VT frame. The
    %locationArray is in the form [x1, y1, x2, y2, ... xN, yN].
    time_win=length(VTRecsReturned)./(2*vfs);
    x0 = double(locationArray(end - 1)).*scale_x-x_center;
    y0 = double((-1)*locationArray(end)).*scale_y-y_center;
    if x0>=0
        if x0==0
            x0=0.1;
        end
        ang0=mod(atan(y0/x0),2*pi);
    else
        ang0=mod(atan(y0/x0)+pi,2*pi);
    end
    if strcmp(rot,'Clockwise');
        ang0=2*pi-ang0;
    end
    
    ang0_diff=ang0-Ang_center;
    ang0_diff(ang0_diff<(-1)*pi)=ang0_diff(ang0_diff<(-1)*pi)+2*pi;
    ang0_diff(ang0_diff>pi)=ang0_diff(ang0_diff>pi)-2*pi;
    % -pi < and0_diff < pi
    % and0_diff < 0 means the rat is in front of the corrct spot,
    % otherwise, the rat is behind the spot
    
    angdiff=ang0_diff(ind_area);
    if abs(angdiff)<zone_tolerance+zone_error  % in the error+correct area
        
        % in the left error area
        if angdiff>(-1)*(zone_tolerance+zone_error) && angdiff<(-1)*zone_tolerance
            % the rat is entering the left error area
            ind_left_error=ind_area;  % the current location
            time_stay=time_stay+time_win;
            if time_stay>time_stay_error_alarm   % stopped at left error area
                sound(sound_error,sound_error_fs);
                run_num=run_num+1;
                run_correct(run_num)=0;
                run_areas(run_num)=ind_area;
                run_ts(run_num)=timeStampArray(end);
                run_ang(run_num)=ang0;
                ind_area=mod(ind_area+1,num_zones);
                if ind_area==0
                    ind_area=num_zones;
                end
                ind_left_error=0;
                ind_correct=0;
                ind_right_error=0;
                time_stay=0;
            end
            
            % in the correct area
        elseif angdiff>(-1)*(zone_tolerance) && angdiff<zone_tolerance
            if ind_left_error
                ind_left_error=0;
                time_stay=0;
            end
            ind_correct=ind_area;
            time_stay=time_stay+time_win;
            if time_stay>time_stay_correct_alarm   % stopped at correct area
                sound(sound_correct,sound_correct_fs);
                run_num=run_num+1;
                run_correct(run_num)=1;
                run_areas(run_num)=ind_area;
                run_ts(run_num)=timeStampArray(end);
                run_ang(run_num)=ang0;
                ind_area=mod(ind_area+1,num_zones);
                if ind_area==0
                    ind_area=num_zones;
                end
                ind_left_error=0;
                ind_correct=0;
                ind_right_error=0;
                time_stay=0;
            end
            
            % in the right error area
        elseif angdiff>zone_tolerance && angdiff<(zone_tolerance+zone_error)
            if ind_correct
                ind_correct=0;
                time_stay=0;
            end
            ind_right_error=ind_area;
            time_stay=time_stay+time_win;
            if time_stay>time_stay_error_alarm   % stopped at right error area
                sound(sound_error,sound_error_fs);
                run_num=run_num+1;
                run_correct(run_num)=0;
                run_areas(run_num)=ind_area;
                run_ts(run_num)=timeStampArray(end);
                run_ang(run_num)=ang0;
                ind_area=mod(ind_area+1,num_zones);
                if ind_area==0
                    ind_area=num_zones;
                end
                ind_left_error=0;
                ind_correct=0;
                ind_right_error=0;
                time_stay=0;
            end
        end
    else % the rat is not in any area
        
        if ind_right_error
            % that means the rat just passed through one location very quickly,
            % without stopping at left error, correct, or right error areas.
            sound(sound_error,sound_error_fs);
            run_num=run_num+1;
            run_correct(run_num)=-1;  % not stopping at all
            run_areas(run_num)=ind_area;
            run_ts(run_num)=timeStampArray(end);
            run_ang(run_num)=ang0;
            ind_area=mod(ind_area+1,num_zones);
            if ind_area==0
                ind_area=num_zones;
            end
            ind_left_error=0;
            ind_correct=0;
            ind_right_error=0;
        end
        
        time_stay=0;
    end
    
    if session_time>ts_stop
        break
    end
end
sound(sound_beep_end,sound_beep_fs);

learn_num=min(learning_lap*num_zones,run_num);
learn_correct=run_correct(1:learn_num);
learn_areas=run_areas(1:learn_num);
learn_ts=run_ts(1:learn_num);
learn_ang=run_ang(1:learn_num);

test_num=run_num-learn_num;
test_correct=run_correct(learn_num+1:end);
test_areas=run_areas(learn_num+1:end);
test_ts=run_ts(learn_num+1:end);
test_ang=run_ang(learn_num+1:end);