classdef(Hidden,Sealed) UIARParameterDialog < internal.EconModeler.Dialogs.UIModelParameterDialog
    %UIARPARAMETERDIALOG  Dialog for creating AR Models in the
    %econModeler App
    
    %Copyright 2017-2019 The MathWorks Inc.
    
    properties(Access = protected)
        Title = getString(message('econ:econModeler:ARModelTitle'));
        ModelType = internal.EconModeler.Enumerations.ModelTypes.AR;
        NeedsTypeTabs = true;
        NeedsNonSeasonalPanel = true;
        NeedsSeasonalPanel = false;
        NeedsIntegration = false;
        NeedsAR = true;
        NeedsMA = false;
        NeedsConstant = true;
        NeedsSeasonal = false;
        NeedsPredictors = false;
        NeedsGARCH = false;
        NeedsARCH = false;
        NeedsLeverage = false;
        NeedsOffset = false;
    end
    
    methods(Access = ?internal.EconModeler.DialogFactory)
        function this = UIARParameterDialog(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'
                        if ~isempty(source.ARLags)
                            this.NonSeasonalAROrderSpinner.Value = max(source.ARLags);
                            this.setARVector;
                        end
                        this.TypeTabGroup.SelectedTab = this.OrderTab;
                    case 'Vector'
                        this.NonSeasonalARVectorEdit.Value = this.lags2ModifyString(source.ARLags);
                        this.TypeTabGroup.SelectedTab = this.VectorTab;
                end
                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 300];
            
            this.TypeTabGroup.Visible = 'on';
            this.TypeTabGroup.Layout.Column = [1 3];
            
            this.OrderGrid.RowHeight = {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.OrderConstantCheckBox.Visible = 'on';
            this.OrderConstantCheckBox.Parent = this.OrderGrid;
            this.OrderConstantCheckBox.Layout.Row = 2;
            this.OrderConstantCheckBox.Layout.Column = [2 3];
            
            this.VectorGrid.RowHeight = {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.VectorConstantCheckBox.Visible = 'on';
            this.VectorConstantCheckBox.Parent = this.VectorGrid;
            this.VectorConstantCheckBox.Layout.Row = 2;
            this.VectorConstantCheckBox.Layout.Column = [2 3];
        end
        
        function str = getEquation(this,p)
            try
                %Build AR equation
                str = "y_{t} = ";
                str = "$$" + this.getARString(p) + str + this.getConstantString() + "\varepsilon_{t}$$";
            catch
                %Display message if this fails
                str = getString(message('econ:econModeler:InvalidEquation'));
            end
        end
        
        function launchHelp(~,~,~)
            helpview('econ','ar_topic');
        end
            
    end 
end

