% Eventlistener for waitbar
classdef janis_gui_RespondToCycle < handle

    properties (SetAccess = protected)
        % Starting time
        Start;
        % the waitbar object
        h;
    end

   methods
      % Constructor, takes validator as input
      function obj = janis_gui_RespondToCycle(validator_obj)
         obj.h = waitbar(0, 'Please Wait', 'Name', 'Validation');
         posi = get(obj.h,'Position');
         posi(4) = posi(4) + 100;
         set(obj.h,'Position',posi);
         addlistener(validator_obj, 'cycle',@(src,evtdata)handleEvnt(obj,src,evtdata));
         obj.Start = tic;
      end

      % delete will delete this object and the waitbar.
      function delete(obj)
         delete(obj.h);
      end

      % updates the progressbar, computes remaining time and so on...
      function handleEvnt(obj,src,evtdata)
           timer = toc(obj.Start);
           obj.h = waitbar(src.actualRound/src.getRoundCount, obj.h, ...
           sprintf('Validation Round %d (%d)\n\n Performance:\nThis iteration: %3.1f  %% - Mean: %3.1f %%\n\nTime: \nRemaining: %4.0f sec. - Elapsed: %4.0f sec.\n\n',...
           src.actualRound, src.getRoundCount, ...
           src.classPerformance.LastCorrectRate*100, src.classPerformance.CorrectRate*100, ...
           (timer/src.actualRound)*(src.getRoundCount-src.actualRound), timer));
      end
   end
end