classdef(Hidden,Sealed) DialogFactory < handle
    %DIALOGFACTORY Factory class for producing MATLAB figure dialogs
    
    %Copyright 2017 The MathWorks Inc.
    
    properties(Access = private)
        Session
        Listeners
    end
    
    methods(Access = ?internal.EconModeler.econModelerSession)
        function this = DialogFactory(session)
            this.Session = session;
            this.Listeners{end+1} = event.listener(this.Session,'NewTimeSeriesRequest',@this.createTSDialog);
            this.Listeners{end+1} = event.listener(this.Session,'ModelTypeSelection',@this.createModelDialog);
            this.Listeners{end+1} = event.listener(this.Session,'GenerateReportRequest',@this.createReportDialog);
            this.Listeners{end+1} = event.listener(this.Session,'ExportVariableRequest',@this.createExportDialog);
        end
    end
    
    methods(Access = private)
        function createTSDialog(this,~,e)
            import internal.EconModeler.Dialogs.*
            import internal.EconModeler.Enumerations.TimeSeriesTypes
            this.Session.notify('Busy');
            switch e.RequestedTimeSeries
                case TimeSeriesTypes.IMPORTWS
                    ImportDialog(this.Session,'ws');
                case TimeSeriesTypes.IMPORTFILE
                    ImportDialog(this.Session,'file');
            end
            this.Session.notify('Unbusy');
        end
        
        function createModelDialog(this,~,e)
            import internal.EconModeler.Dialogs.*
            import internal.EconModeler.Enumerations.ModelTypes
            this.Session.notify('Busy');
            try
                modelType = e.RequestedModel;
                source = e.Options.Source;
                switch modelType
                    case ModelTypes.AR
                        UIARParameterDialog(this.Session,source);
                    case ModelTypes.MA
                        UIMAParameterDialog(this.Session,source);
                    case ModelTypes.ARMA
                        UIARMAParameterDialog(this.Session,source);
                    case ModelTypes.ARIMA
                        UIARIMAParameterDialog(this.Session,source);
                    case ModelTypes.SARIMA
                        UISARIMAParameterDialog(this.Session,source);
                    case ModelTypes.ARIMAX
                        UIARIMAXParameterDialog(this.Session,source);
                    case ModelTypes.SARIMAX
                        UISARIMAXParameterDialog(this.Session,source);
                    case ModelTypes.GARCH
                        UIGARCHParameterDialog(this.Session,source);
                    case ModelTypes.EGARCH
                        UIEGARCHParameterDialog(this.Session,source);
                    case ModelTypes.GJR
                        UIGJRParameterDialog(this.Session,source);
                    case ModelTypes.MLR
                        UIMLRParameterDialog(this.Session,source);
                    case ModelTypes.REGARIMA
                        UIRegARIMAParameterDialog(this.Session,source);
                    case ModelTypes.VAR
                        UIVARParameterDialog(this.Session,source);
                    case ModelTypes.VARX
                        UIVARXParameterDialog(this.Session,source);
                    case ModelTypes.VECComponent
                        UIVECComponentParameterDialog(this.Session,source);
                    case ModelTypes.VECImpact
                        UIVECImpactParameterDialog(this.Session,source);
                end
            catch ME
                uialert(this.Session.App,ME.message,...
                    getString(message('econ:econModeler:AppTitle')),...
                    'Icon','error','Modal',true);
            end 
            this.Session.notify('Unbusy');
        end 
        
        function createReportDialog(this,~,~)
            import internal.EconModeler.Dialogs.*
            this.Session.notify('Busy');
            ReportDialog(this.Session);
            this.Session.notify('Unbusy');
        end
        
        function createExportDialog(this,~,~)
            import internal.EconModeler.Dialogs.*
            this.Session.notify('Busy');
            ExportDialog(this.Session);
            this.Session.notify('Unbusy');
        end
    end
end

