classdef(Hidden,Sealed) UIARIMAParameterDialog < internal.EconModeler.Dialogs.UIModelParameterDialog
    %UIARIMAPARAMETERDIALOG Dialog for creating ARIMA Models in the
    %econModeler App
    
    %Copyright 2017-2019 The MathWorks Inc.
    
    properties (Access = protected)
        Title = getString(message('econ:econModeler:ARIMAModelTitle'));
        ModelType = internal.EconModeler.Enumerations.ModelTypes.ARIMA;
        NeedsTypeTabs = true;
        NeedsNonSeasonalPanel = true;
        NeedsSeasonalPanel = false;
        NeedsIntegration = true;
        NeedsAR = true;
        NeedsMA = true;
        NeedsConstant = true;
        NeedsSeasonal = false;
        NeedsPredictors = false;
        NeedsGARCH = false;
        NeedsARCH = false;
        NeedsLeverage = false;
        NeedsOffset = false;
    end
    
    methods(Access = ?internal.EconModeler.DialogFactory)
        function this = UIARIMAParameterDialog(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;
                        this.TypeTabGroup.SelectedTab = this.OrderTab;
                    case 'Vector'
                        this.NonSeasonalARVectorEdit.Value = this.lags2ModifyString(source.ARLags);
                        this.NonSeasonalMAVectorEdit.Value = this.lags2ModifyString(source.MALags);
                        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 500 325];
            
            this.TypeTabGroup.Visible = 'on';
            this.TypeTabGroup.Layout.Column = [1 3];
            
            this.OrderGrid.RowHeight = {20 20 20 'fit'};
            
            this.NonSeasonalAROrderLabel.Visible = 'on';
            this.NonSeasonalAROrderLabel.Parent = this.OrderGrid;
            this.NonSeasonalAROrderLabel.Layout.Row = 1;
            this.NonSeasonalAROrderLabel.Layout.Column = 2;
            
            this.NonSeasonalAROrderSpinner.Visible = 'on';
            this.NonSeasonalAROrderSpinner.Parent = this.OrderGrid;
            this.NonSeasonalAROrderSpinner.Layout.Row = 1;
            this.NonSeasonalAROrderSpinner.Layout.Column = 3;
            
            this.OrderDegreeIntegrationLabel.Visible = 'on';
            this.OrderDegreeIntegrationLabel.Parent = this.OrderGrid;
            this.OrderDegreeIntegrationLabel.Layout.Row = 2;
            this.OrderDegreeIntegrationLabel.Layout.Column = 2;
            
            this.OrderDegreeIntegrationSpinner.Visible = 'on';
            this.OrderDegreeIntegrationSpinner.Parent = this.OrderGrid;
            this.OrderDegreeIntegrationSpinner.Layout.Row = 2;
            this.OrderDegreeIntegrationSpinner.Layout.Column = 3;
            
            this.NonSeasonalMAOrderLabel.Visible = 'on';
            this.NonSeasonalMAOrderLabel.Parent = this.OrderGrid;
            this.NonSeasonalMAOrderLabel.Layout.Row = 3;
            this.NonSeasonalMAOrderLabel.Layout.Column = 2;
            
            this.NonSeasonalMAOrderSpinner.Visible = 'on';
            this.NonSeasonalMAOrderSpinner.Parent = this.OrderGrid;
            this.NonSeasonalMAOrderSpinner.Layout.Row = 3;
            this.NonSeasonalMAOrderSpinner.Layout.Column = 3;
          
            this.OrderConstantCheckBox.Visible = 'on';
            this.OrderConstantCheckBox.Parent = this.OrderGrid;
            this.OrderConstantCheckBox.Layout.Row = 4;
            this.OrderConstantCheckBox.Layout.Column = [2 3];
            
            this.VectorGrid.RowHeight = {20 20 20 'fit'};
            
            this.NonSeasonalARVectorLabel.Visible = 'on';
            this.NonSeasonalARVectorLabel.Parent = this.VectorGrid;
            this.NonSeasonalARVectorLabel.Layout.Row = 1;
            this.NonSeasonalARVectorLabel.Layout.Column = 2;
            
            this.NonSeasonalARVectorEdit.Visible = 'on';
            this.NonSeasonalARVectorEdit.Parent = this.VectorGrid;
            this.NonSeasonalARVectorEdit.Layout.Row = 1;
            this.NonSeasonalARVectorEdit.Layout.Column = 3;
            
            this.VectorDegreeIntegrationLabel.Visible = 'on';
            this.VectorDegreeIntegrationLabel.Parent = this.VectorGrid;
            this.VectorDegreeIntegrationLabel.Layout.Row = 2;
            this.VectorDegreeIntegrationLabel.Layout.Column = 2;
            
            this.VectorDegreeIntegrationSpinner.Visible = 'on';
            this.VectorDegreeIntegrationSpinner.Parent = this.VectorGrid;
            this.VectorDegreeIntegrationSpinner.Layout.Row = 2;
            this.VectorDegreeIntegrationSpinner.Layout.Column = 3;
            
            this.NonSeasonalMAVectorLabel.Visible = 'on';
            this.NonSeasonalMAVectorLabel.Parent = this.VectorGrid;
            this.NonSeasonalMAVectorLabel.Layout.Row = 3;
            this.NonSeasonalMAVectorLabel.Layout.Column = 2;
            
            this.NonSeasonalMAVectorEdit.Visible = 'on';
            this.NonSeasonalMAVectorEdit.Parent = this.VectorGrid;
            this.NonSeasonalMAVectorEdit.Layout.Row = 3;
            this.NonSeasonalMAVectorEdit.Layout.Column = 3;
            
            this.VectorConstantCheckBox.Visible = 'on';
            this.VectorConstantCheckBox.Parent = this.VectorGrid;
            this.VectorConstantCheckBox.Layout.Row = 4;
            this.VectorConstantCheckBox.Layout.Column = [2 3];
        end
         
        function str = getEquation(this,p)
            try
                %Build ARIMA equation
                str = "y_{t} = ";
                str = "$$" + this.getARString(p) + this.getDegreeString() + str + ...
                    this.getConstantString() + this.getMAString(p) + "\varepsilon_{t}$$";
            catch
                %Display message if this fails
                str = getString(message('econ:econModeler:InvalidEquation'));
            end
        end
         
        function launchHelp(~,~,~)
            helpview('econ','arima_topic');
        end
    end 
end

