%% Function for showing the video with IDs and for video export.
%
%  INPUTS
%   resultAngle: matrix of two rows which belong to selected sensors and
%   columns which represent angles of radar targets
%   rangeBins: matrix of two rows which belong to selected sensors and
%   columns which represent range bins of radar targets
%   MCC: vector of MCC for resultAngle and rangeBins variable
%   synchronization
%   hAx: number of RX antennas
%   videoFileReader: instance of videoFileReader for the input video
%   processing
%   fullOutVideo: complete path to input video file with all camera records
%   myMCC: MCC from the user input
%   CamSpinner: selected angle of camera view
%   selectedCamera: label of the camera selected by user
%   Checked: Boolean value whether the video is to be exported

%  no OUTPUTS

function Show(resultAngle,rangeBins,MCC,hAx,videoFileReader,fullOutVideo,myMCC,...
    CamSpinner,selectedCamera,Checked)

switch selectedCamera
    case 'rearCam'
        % modification to meet the camera max view angle
        resultAngle(abs(resultAngle) > CamSpinner/2) = NaN;
        % basic conversion from angles to pixel indexes in video frames
        resultPixel = round(interp1([-CamSpinner/2,CamSpinner/2],[1,458],resultAngle)); 
        lCut = 30;
        rCut = 490;
    case 'frontCam'
        resultAngle(abs(resultAngle) > CamSpinner/2) = NaN;
        resultPixel = round(interp1([-CamSpinner/2,CamSpinner/2],[1,458],resultAngle)); 
        lCut = 1420;
        rCut = 1880;
    case 'leftCam'
        resultAngle(resultAngle > -45+CamSpinner/2) = NaN;
        resultAngle(resultAngle < -45-CamSpinner/2) = NaN;
        resultPixel = round(interp1([-45-CamSpinner/2,-45+CamSpinner/2],[1,458],resultAngle)); 
        lCut = 957;
        rCut = 1417;    
    case 'rightCam'  
        resultAngle(resultAngle > 45+CamSpinner/2) = NaN;
        resultAngle(resultAngle < 45-CamSpinner/2) = NaN;
        resultPixel = round(interp1([45-CamSpinner/2,45+CamSpinner/2],[1,458],resultAngle)); 
        lCut = 493;
        rCut = 953;
end

resultPixelCleaned = [];
for row=1:size(resultPixel,1)
   if min(isnan(resultPixel(row,:))) == 0
       resultPixelCleaned = [resultPixelCleaned; resultPixel(row,:)];
   end
end

% Video processing
resultPixelRows = size(resultPixelCleaned,1);
counter = 1;
Nums = NumClass;

if Checked
    v = VideoWriter(fullOutVideo,'MPEG-4');
    open(v)
end

videoFig = image(hAx, 'CData', NaN(357,461,3));

while hasFrame(videoFileReader)
    myMCC = myMCC + 1;
    % 1 MCC per 2 frames
	defaultFrame = readFrame(videoFileReader);
    readFrame(videoFileReader);
    
    % cuts from original video
    selCam = defaultFrame(84:440,lCut:rCut,:);     % 356x460 pixels
    
    if any(myMCC == MCC)
        if counter < length(resultPixelCleaned)
            for id=1:resultPixelRows % id of peak
                % num of digits in id
                id_digNum = max(ceil(log10(abs(rangeBins(id,counter)))),1); 
                id_vect = num2str(rangeBins(id,counter)) - '0'; % vector from id
                if ~isnan(resultPixelCleaned(id,counter))
                    if ~isnan(rangeBins(id,counter))
                    % rear camera
                         for digNum=1:id_digNum
                            if resultPixelCleaned(id,counter)+digNum*9 <= 462
                                selCam(150:150+9,...
                                    resultPixelCleaned(id,counter)...
                                    +(digNum-1)*9:resultPixelCleaned(id,...
                                    counter)+digNum*9,:)= Nums.pickNum(...
                                    id_vect(digNum));
                            end
                         end
                     end
                end
            end
            counter = counter + 1;
        end
    end
    
    % display video frame to screen
    set(videoFig, 'CData', flip(selCam));
    % if needed, export video to a file
    if Checked
        writeVideo(v,selCam);      
    end  
    pause(0.001); 	
end

if Checked
    close(v)
end

end
