classdef(Hidden,Sealed) ARCHTab < internal.EconModeler.Tabs.AbstractContextualTab
    %ADFTAB Tab used to control the ARCH Tests in the econModeler App
    
    %Copyright 2017-2018 The MathWorks, Inc.
    
    properties(Access = protected)
        LagSpinner
        SigSpinner
        AddButton
        ClearButton
        Listener
    end
    
    methods(Access = ?internal.EconModeler.ContextualTabList)
        function this = ARCHTab(session)
            
            import matlab.ui.internal.toolstrip.*
            
            this@internal.EconModeler.Tabs.AbstractContextualTab();
            
            %Create the Parameter Section
            parametersSection = Section(getString(message('econ:econModeler:ParametersSection')));
            parametersSection.Tag = 'parametersSection';
            this.Tab.add(parametersSection);
            
            % Lags and Significance Level Labels
            col1 = parametersSection.addColumn('HorizontalAlignment','right');
            lagLabel = Label(getString(message('econ:econModeler:NumberOfLags')));
            lagLabel.Tag = 'lagLabel';
            lagLabel.Description = getString(message('econ:econModeler:ModelLagsDescription'));
            col1.add(lagLabel);
            
            sigLabel = Label(getString(message('econ:econModeler:SignificanceLevel')));
            sigLabel.Tag = 'significanceLevelLabel';
            sigLabel.Description = getString(message('econ:econModeler:SignificanceLevelDescription'));
            col1.add(sigLabel);
            
            %Add Lag and SignificanceLevel Controls
            col2 = parametersSection.addColumn();
            this.LagSpinner = Spinner([1 100],1);
            this.LagSpinner.StepSize = 1;
            this.LagSpinner.Tag = 'lagSpinner';
            this.LagSpinner.Description = getString(message('econ:econModeler:ModelLagsDescription'));
            col2.add(this.LagSpinner);
            
            this.SigSpinner = Spinner([0,1],0.05);
            this.SigSpinner.NumberFormat = 'double';
            this.SigSpinner.StepSize = 0.01;
            this.SigSpinner.DecimalFormat = '2f';
            this.SigSpinner.Tag = 'significanceLevelSpinner';
            this.SigSpinner.Description = getString(message('econ:econModeler:SignificanceLevelDescription'));
            col2.add(this.SigSpinner);
            
            %Test Section
            testsSection = Section(getString(message('econ:econModeler:Tests')));
            testsSection.Tag = 'testsSection';
            this.Tab.add(testsSection);
            
            %Add Button
            col3 = testsSection.addColumn();
            this.AddButton = Button(getString(message('econ:econModeler:AddTest')),Icon.RUN_24);
            this.AddButton.Tag = 'addButton';
            this.AddButton.Description = getString(message('econ:econModeler:AddTestDescription'));
            this.AddButton.ButtonPushedFcn = @this.addTest;
            col3.add(this.AddButton);
            
            %Clear Button 
            col4 = testsSection.addColumn();
            this.ClearButton = Button(getString(message('econ:econModeler:ClearTests')),Icon.CLEAR_24);
            this.ClearButton.Tag = 'clearButton';
            this.ClearButton.Description = getString(message('econ:econModeler:ClearTestsDescription'));
            this.ClearButton.ButtonPushedFcn = @this.clearTests;
            this.ClearButton.Enabled = false;
            col4.add(this.ClearButton);
            
            this.TabGroup.Tag = 'archTabGroup';
            session.addTabGroup(this.TabGroup);
        end
    end
    
    methods(Access = protected)
        function addTest(this,~,~)
            parameters = {this.LagSpinner.Value,...
                this.SigSpinner.Value};
            this.Figure.addTest(parameters);
        end
        function clearTests(this,~,~)
            this.Figure.clearTests();
        end
        function enableClear(this,~,~)
            this.ClearButton.Enabled = this.Figure.NumberOfSelections~=0;
        end
        function str = getTitle(~)
            str = getString(message('econ:econModeler:ARCHName'));
        end
        function setTabTag(this)
            this.Tab.Tag = 'archTab';
        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 ARCHFigure
            validateattributes(fig,{'internal.EconModeler.EconComponents.Figures.ARCHFigure'},{'scalar'});
            this.Figure = fig;
            
            %Set listner for changes in table size
            this.Listener = event.listener(fig,'DataChanged',@this.enableClear);
            this.enableClear;
            
%             upperLimit = length(this.Figure.getDataToTest()) - 2;
%             if this.LagSpinner.Value > upperLimit
%                 this.LagSpinner.Value = upperLimit;
%             end
%             this.LagSpinner.Limits = [1 upperLimit];
         end
    end
end

