classdef(Hidden, Sealed) UISARIMAParameterDialog < internal.EconModeler.Dialogs.UIModelParameterDialog
    %UISARIMAPARAMETERDIALOG Dialog for creating SARIMA Models in the
    %econModeler App
    
    %Copyright 2017-2019 The MathWorks Inc.
    
    properties(Access = protected)
        Title = getString(message('econ:econModeler:SARIMAModelTitle'));
        ModelType = internal.EconModeler.Enumerations.ModelTypes.SARIMA;
        NeedsTypeTabs = true;
        NeedsNonSeasonalPanel = true;
        NeedsSeasonalPanel = true;
        NeedsIntegration = true;
        NeedsAR = true;
        NeedsMA = true;
        NeedsConstant = true;
        NeedsSeasonal = true;
        NeedsPredictors = false;
        NeedsGARCH = false;
        NeedsARCH = false;
        NeedsLeverage = false;
        NeedsOffset = false;
    end
    
    methods(Access = ?internal.EconModeler.DialogFactory)
        function this = UISARIMAParameterDialog(session,source)
            this = this@internal.EconModeler.Dialogs.UIModelParameterDialog(session,source);
            
             if  isa(source,'internal.EconModeler.EconComponents.AbstractModel')
                %Use old model "source" to fill in the controls
                switch source.DefiningTab
                    case 'Order'
                        this.NonSeasonalAROrderSpinner.Value = length(source.ARLags);
                        this.setARVector;
                        this.NonSeasonalMAOrderSpinner.Value = length(source.MALags);
                        this.setMAVector;
                        if source.Seasonality > 0
                            this.OrderSeasonalitySpinner.Value = source.Seasonality;
                        elseif ~isempty(source.SARLags)
                            this.OrderSeasonalitySpinner.Value = source.SARLags(1);
                        elseif ~isempty(source.SMALags)
                            this.OrderSeasonalitySpinner.Value = source.SMALags(1);    
                        else
                            this.OrderSeasonalitySpinner.Value = 0;
                        end
                        this.setPeriod;
                        this.SeasonalAROrderSpinner.Value = length(source.SARLags);
                        this.setSARVector;
                        this.SeasonalMAOrderSpinner.Value = length(source.SMALags);
                        this.setSMAVector;
                        this.SeasonalDiffCheckBox.Value = source.Seasonality > 0;
                        this.setSeasonality;
                        this.TypeTabGroup.SelectedTab = this.OrderTab;
                    case 'Vector'
                        this.NonSeasonalARVectorEdit.Value = this.lags2ModifyString(source.ARLags);
                        this.NonSeasonalMAVectorEdit.Value = this.lags2ModifyString(source.MALags);
                        this.VectorSeasonalitySpinner.Value = source.Seasonality;
                        this.SeasonalARVectorEdit.Value = this.lags2ModifyString(source.SARLags);
                        this.SeasonalMAVectorEdit.Value = this.lags2ModifyString(source.SMALags);
                        this.TypeTabGroup.SelectedTab = this.VectorTab;
                end
                this.OrderDegreeIntegrationSpinner.Value = source.DegreeIntegration;
                this.VectorDegreeIntegrationSpinner.Value = source.DegreeIntegration;
                this.OrderConstantCheckBox.Value = isnan(source.Constant);
                this.VectorConstantCheckBox.Value = isnan(source.Constant); 
                this.InnovationDropDown.Value = source.Distribution;
                this.setModelEquation();
            end         
        end
    end
    
    methods(Access = protected)
       function arrangeComponents(this)
            %Position and set visibility for all relevant components
            this.Figure.Position = [100 100 725 375];
            
            this.TypeTabGroup.Visible = 'on';
            this.TypeTabGroup.Layout.Column = [1 3];
            
            this.OrderNonSeasonalPanel.Visible = 'on';
            this.OrderNonSeasonalPanel.Layout.Row = [1 5];
            this.OrderNonSeasonalPanel.Layout.Column = [1 2];
            
            this.NonSeasonalAROrderLabel.Visible = 'on';
            this.NonSeasonalAROrderLabel.Layout.Row = 1;
            this.NonSeasonalAROrderLabel.Layout.Column = 1;
            
            this.NonSeasonalAROrderSpinner.Visible = 'on';
            this.NonSeasonalAROrderSpinner.Layout.Row = 1;
            this.NonSeasonalAROrderSpinner.Layout.Column = 2;
            
            this.OrderDegreeIntegrationLabel.Visible = 'on';
            this.OrderDegreeIntegrationLabel.Layout.Row = 2;
            this.OrderDegreeIntegrationLabel.Layout.Column = 1;
            
            this.OrderDegreeIntegrationSpinner.Visible = 'on';
            this.OrderDegreeIntegrationSpinner.Layout.Row = 2;
            this.OrderDegreeIntegrationSpinner.Layout.Column = 2;
            
            this.NonSeasonalMAOrderLabel.Visible = 'on';
            this.NonSeasonalMAOrderLabel.Layout.Row = 3;
            this.NonSeasonalMAOrderLabel.Layout.Column = 1;
            
            this.NonSeasonalMAOrderSpinner.Visible = 'on';
            this.NonSeasonalMAOrderSpinner.Layout.Row = 3;
            this.NonSeasonalMAOrderSpinner.Layout.Column = 2;
            
            this.OrderConstantCheckBox.Visible = 'on';
            this.OrderConstantCheckBox.Parent = this.OrderNonSeasonalGrid;
            this.OrderConstantCheckBox.Layout.Row = 4;
            this.OrderConstantCheckBox.Layout.Column = [1 2];
            
            this.OrderSeasonalPanel.Visible = 'on';
            this.OrderSeasonalPanel.Layout.Row = [1 5];
            this.OrderSeasonalPanel.Layout.Column = [3 4];
            
            this.OrderSeasonalityLabel.Visible = 'on';
            this.OrderSeasonalityLabel.Layout.Row = 1;
            this.OrderSeasonalityLabel.Layout.Column = 1;
            
            this.OrderSeasonalitySpinner.Visible = 'on';
            this.OrderSeasonalitySpinner.Layout.Row = 1;
            this.OrderSeasonalitySpinner.Layout.Column = 2;
            
            this.SeasonalAROrderLabel.Visible = 'on';
            this.SeasonalAROrderLabel.Layout.Row = 2;
            this.SeasonalAROrderLabel.Layout.Column = 1;
            
            this.SeasonalAROrderSpinner.Visible = 'on';
            this.SeasonalAROrderSpinner.Layout.Row = 2;
            this.SeasonalAROrderSpinner.Layout.Column = 2;
            
            this.SeasonalMAOrderLabel.Visible = 'on';
            this.SeasonalMAOrderLabel.Layout.Row = 3;
            this.SeasonalMAOrderLabel.Layout.Column = 1;
            
            this.SeasonalMAOrderSpinner.Visible = 'on';
            this.SeasonalMAOrderSpinner.Layout.Row = 3;
            this.SeasonalMAOrderSpinner.Layout.Column = 2;
            
            this.SeasonalDiffCheckBox.Visible = 'on';
            this.SeasonalDiffCheckBox.Layout.Row = 4;
            this.SeasonalDiffCheckBox.Layout.Column = [1 2];
            
            this.VectorNonSeasonalPanel.Visible = 'on';
            this.VectorNonSeasonalPanel.Layout.Row = [1 5];
            this.VectorNonSeasonalPanel.Layout.Column = [1 2];
            
            this.NonSeasonalARVectorLabel.Visible = 'on';
            this.NonSeasonalARVectorLabel.Layout.Row = 1;
            this.NonSeasonalARVectorLabel.Layout.Column = 1;
            
            this.NonSeasonalARVectorEdit.Visible = 'on';
            this.NonSeasonalARVectorEdit.Layout.Row = 1;
            this.NonSeasonalARVectorEdit.Layout.Column = 2;
            
            this.VectorDegreeIntegrationLabel.Visible = 'on';
            this.VectorDegreeIntegrationLabel.Layout.Row = 2;
            this.VectorDegreeIntegrationLabel.Layout.Column = 1;
            
            this.VectorDegreeIntegrationSpinner.Visible = 'on';
            this.VectorDegreeIntegrationSpinner.Layout.Row = 2;
            this.VectorDegreeIntegrationSpinner.Layout.Column = 2;
            
            this.NonSeasonalMAVectorLabel.Visible = 'on';
            this.NonSeasonalMAVectorLabel.Layout.Row = 3;
            this.NonSeasonalMAVectorLabel.Layout.Column = 1;
            
            this.NonSeasonalMAVectorEdit.Visible = 'on';
            this.NonSeasonalMAVectorEdit.Layout.Row = 3;
            this.NonSeasonalMAVectorEdit.Layout.Column = 2;
            
            this.VectorConstantCheckBox.Visible = 'on';
            this.VectorConstantCheckBox.Parent = this.VectorNonSeasonalGrid;
            this.VectorConstantCheckBox.Layout.Row = 4;
            this.VectorConstantCheckBox.Layout.Column = [1 2];
            
            this.VectorSeasonalPanel.Visible = 'on';
            this.VectorSeasonalPanel.Layout.Row = [1 5];
            this.VectorSeasonalPanel.Layout.Column = [3 4];
            
            this.SeasonalARVectorLabel.Visible = 'on';
            this.SeasonalARVectorLabel.Layout.Row = 1;
            this.SeasonalARVectorLabel.Layout.Column = 1;
            
            this.SeasonalARVectorEdit.Visible = 'on';
            this.SeasonalARVectorEdit.Layout.Row = 1;
            this.SeasonalARVectorEdit.Layout.Column = 2;
            
            this.VectorSeasonalityLabel.Visible = 'on';
            this.VectorSeasonalityLabel.Layout.Row = 2;
            this.VectorSeasonalityLabel.Layout.Column = 1;
            
            this.VectorSeasonalitySpinner.Visible = 'on';
            this.VectorSeasonalitySpinner.Layout.Row = 2;
            this.VectorSeasonalitySpinner.Layout.Column = 2;
            
            this.SeasonalMAVectorLabel.Visible = 'on';
            this.SeasonalMAVectorLabel.Layout.Row = 3;
            this.SeasonalMAVectorLabel.Layout.Column = 1;
            
            this.SeasonalMAVectorEdit.Visible = 'on';
            this.SeasonalMAVectorEdit.Layout.Row = 3;
            this.SeasonalMAVectorEdit.Layout.Column = 2;
       end
        
       function str = getEquation(this,p)
           try
               %Build SARIMA equation
               str = "y_{t} = ";
               str = "$$" + this.getARString(p) + this.getSARString(p) + ...
                   this.getDegreeString() + this.getSeasonalityString() ...
                   + str + this.getConstantString() + this.getMAString(p) + this.getSMAString(p) + "\varepsilon_{t}$$";
           catch
               %Display message if this fails
               str = getString(message('econ:econModeler:InvalidEquation'));
           end
       end
       
       function launchHelp(~,~,~)
           helpview('econ','sarima_topic');
       end
    end  
end

