classdef(Hidden,Sealed) ACFTab < internal.EconModeler.Tabs.AbstractCorrelationTab
    %ACFTAB Contextual tab for autocorrelation function documents
    
    %Copyright 2017 The MathWorks, Inc.
    
    properties (Access = private)
        MAOrderSpinner
        DefaultMAOrder = 0
    end
    
    methods(Access = ?internal.EconModeler.ContextualTabList)
        function this = ACFTab(session)
            this@internal.EconModeler.Tabs.AbstractCorrelationTab(session);
            this.TabGroup.Tag = 'acfTabGroup';
            this.Session.addTabGroup(this.TabGroup);
        end
    end
    
    methods(Access = protected)
        function thirdLabelHook(~,col)
            import matlab.ui.internal.toolstrip.*
            %The ACF Tab has a third MA Order label
            maLabel = Label(getString(message('econ:econModeler:MAOrder')));
            maLabel.Tag = 'maLabel';
            maLabel.Description = getString(message('econ:econModeler:MAOrderDescription'));
            col.add(maLabel);
        end
        
        function thirdControlHook(this,col)
            import matlab.ui.internal.toolstrip.*
            %The ACF Tab has a third MA Order spinner control
            maSpinner = Spinner([0  this.LagSpinner.Value-1], this.DefaultMAOrder);
            maSpinner.Tag = 'maspinner';
            maSpinner.Description = getString(message('econ:econModeler:MAOrderDescription'));
            maSpinner.StepSize = 1;
            maSpinner.ValueChangedFcn = @(s,e)this.updateMAOrder(s,e);
            col.add(maSpinner);
            this.MAOrderSpinner = maSpinner;
        end
        
        function updateNumLags(this,s,e)
            adjustMA = false;
            if this.MAOrderSpinner.Value >= this.LagSpinner.Value
                adjustMA = true;
                notify(this.Session,'Busy');
                this.MAOrderSpinner.Value = this.LagSpinner.Value-1;
                this.updateMAOrder;
            end
            this.MAOrderSpinner.Limits = [0 this.LagSpinner.Value-1];
            updateNumLags@internal.EconModeler.Tabs.AbstractCorrelationTab(this,s,e);
            if adjustMA
                notify(this.Session,'Unbusy');
            end
        end
        
        function str = getTitle(~)
            %ACF Tab title
            str =getString(message('econ:econModeler:ACFPlot'));
        end
        
        function setTabTag(this)
            %ACF tab tag
            this.Tab.Tag = 'acftab';
        end
    end
    
    methods(Access = {?internal.EconModeler.econModelerSession,...
            ?internal.EconModeler.Tabs.AbstractContextualTab})
        function setFig(this,fig)
            %Associate a new figure with this tab
            
            %The figure must be an ACFPlot
            validateattributes(fig,{'internal.EconModeler.EconComponents.Figures.ACFPlot'},{'scalar'});
            this.Figure = fig;
            
            %Update the control values based on the figure's parameters
            this.LagSpinner.Value = this.Figure.NumLags;
            dataToPlot = this.Figure.getDataToPlot();
            this.LagSpinner.Limits = [1,; length(dataToPlot)-1];
            
            this.StdSpinner.Value = this.Figure.StdDev;
            this.MAOrderSpinner.Value = this.Figure.MAOrder;
            this.MAOrderSpinner.Limits = [0 this.LagSpinner.Value - 1];
        end
    end
        
    
    methods(Access = private)
        function updateMAOrder(this,~,~)
            %Update the figures MA Order when the spinner changes value
            this.Figure.updatePlot([],[],this.MAOrderSpinner.Value);
        end
    end
end

