classdef EstimationMessenger < handle
   % A manager for estimation events.
   % Passes messages to the progress dialog.
   % Replaces nlutilspack.optimmessenger and nlutilspack.messenger
   
   % Copyright 2020 The MathWorks, Inc.
   
   properties
      Stop(1,1) logical = false;
      Enabled(1,1)logical = true;
   end
   
   properties (Access = private)
      Listeners = cell(0,1);
   end
   
   events
      identguichange
      optiminfo
   end
   
   methods(Static)
      function h = getOptimMessenger(Model)
         % return messenger instance, if one exists in the model
         h = [];
         if isa(Model,'idmodel')
            h = pvget(Model,'OptimMessenger');
         elseif isa(Model,'idnlfun')
            h = Model(1).OptimMessenger; % Model may be vector valued
         end
      end
      
      function messenger = getAppMessengerInstance(XID)
         % Fetch the messenger instance used by the GUI singleton.
         try
            if nargin<1
               Xsum = getIdentGUIFigure;
               XID = get(Xsum,'Userdata');
            end
            messenger = XID.OptimMessenger;
         catch
            messenger = [];
         end
      end
      
      function z = getCurrentValidationData()
         % Current validation data in Ident GUI (an iddata or idfrd object)
         z = iduigetd('v');
      end
      
      function z = getCurrentEstimationData()
         % Current estimation data in Ident GUI (an iddata or idfrd object)
         z = iduigetd('e');
      end
      
      function unames = getInputNames(datatype)
         %Obtain cell array of input name strings for estimation or validation data
         
         if nargin<2
            datatype = 'estimation';
         end
         
         if strcmpi(datatype,'estimation')
            z = idestimatorpack.EstimationMessenger.getCurrentEstimationData;
         elseif strcmpi(datatype,'validation')
            z = idestimatorpack.EstimationMessenger.getCurrentValidationData;
         else
            ctrlMsgUtils.error('Ident:idguis:getNames1','getInputNames(DATATYPE)');
         end
         
         unames = pvget(z,'InputName');
      end
      
      function ynames = getOutputNames(datatype)
         %Obtain cell array of output name strings for estimation or validation
         %data.
         
         if nargin<2
            datatype = 'estimation';
         end
         
         if strcmpi(datatype,'estimation')
            z = idestimatorpack.EstimationMessenger.getCurrentEstimationData;
         elseif strcmpi(datatype,'validation')
            z = idestimatorpack.EstimationMessenger.getCurrentValidationData;
         else
            ctrlMsgUtils.error('Ident:idguis:getNames1','getOutputNames(DATATYPE)')
         end
         
         ynames = pvget(z,'OutputName');
      end
   end
end

