classdef(Hidden,Sealed) EGCITab < internal.EconModeler.Tabs.AbstractContextualTab
    %EGCITAB Tab used to control the EGCI Tests in the econModeler App
    
    %Copyright 2021 The MathWorks, Inc.
    
    properties (Access = protected)
        DependentVariableDropDown matlab.ui.internal.toolstrip.DropDown
        CRegDropDown matlab.ui.internal.toolstrip.DropDown
        RRegDropDown matlab.ui.internal.toolstrip.DropDown
        LagSpinner matlab.ui.internal.toolstrip.Spinner
        SigSpinner matlab.ui.internal.toolstrip.Spinner
        TestStatDropDown matlab.ui.internal.toolstrip.DropDown
        AddButton matlab.ui.internal.toolstrip.Button
        ClearButton matlab.ui.internal.toolstrip.Button
        Listener event.listener
    end
    
    methods (Access = ?internal.EconModeler.ContextualTabList)
        function this = EGCITab(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);
            
            %Add labels for dependentVar, creg and rreg
            col1 = parametersSection.addColumn('HorizontalAlignment','right');
            dependentVarLabel = Label(getString(message('econ:econModeler:DependentVariable')));
            dependentVarLabel.Tag = 'dependentVarLabel';
            dependentVarLabel.Description = getString(message('econ:econModeler:DependentVariableDescription'));
            col1.add(dependentVarLabel);
            
            cregLabel = Label(getString(message('econ:econModeler:CointegrationRegressionForm')));
            cregLabel.Tag = 'cregLabel';
            cregLabel.Description = getString(message('econ:econModeler:CointegrationRegressionFormDescription'));
            col1.add(cregLabel);
            
            rregLabel = Label(getString(message('econ:econModeler:ResidualRegressionForm')));
            rregLabel.Tag = 'rregLabel';
            rregLabel.Description = getString(message('econ:econModeler:ResidualRegressionFormDescription'));
            col1.add(rregLabel);
            
            %Add DropDowns for Dependent Variable, CREG and RREG
            col2 = parametersSection.addColumn('Width',150);
            this.DependentVariableDropDown = DropDown();
            this.DependentVariableDropDown.Tag = 'dependentVariableDropDown';
            this.DependentVariableDropDown.Description = getString(message('econ:econModeler:DependentVariableDescription'));
            col2.add(this.DependentVariableDropDown);
            
            this.CRegDropDown = DropDown({'nc';'c';'ct';'ctt'});
            this.CRegDropDown.Value = 'c';
            this.CRegDropDown.Tag = 'cregDropDown';
            this.CRegDropDown.Description =  getString(message('econ:econModeler:CointegrationRegressionFormDescription'));
            col2.add(this.CRegDropDown);
            
            this.RRegDropDown = DropDown({'ADF';'PP'});
            this.RRegDropDown.Value = 'ADF';
            this.RRegDropDown.Tag = 'rregDropDown';
            this.RRegDropDown.Description =  getString(message('econ:econModeler:ResidualRegressionFormDescription'));
            col2.add(this.RRegDropDown);
            
            % Lags and Significance Level Labels
            col3 = parametersSection.addColumn('HorizontalAlignment','right');
            lagLabel = Label(getString(message('econ:econModeler:NumberOfLags')));
            lagLabel.Tag = 'lagLabel';
            lagLabel.Description = getString(message('econ:econModeler:ModelLagsDescription'));
            col3.add(lagLabel);
            
            testStatLabel = Label(getString(message('econ:econModeler:TestStat')));
            testStatLabel.Tag = 'testStatLabel';
            testStatLabel.Description = getString(message('econ:econModeler:TestStatDescription'));
            col3.add(testStatLabel);
            
            sigLabel = Label(getString(message('econ:econModeler:SignificanceLevel')));
            sigLabel.Tag = 'significanceLevelLabel';
            sigLabel.Description = getString(message('econ:econModeler:SignificanceLevelDescription'));
            col3.add(sigLabel);
            
            %Add Lag and SignificanceLevel Controls
            col4 = parametersSection.addColumn('Width',150);
            this.LagSpinner = Spinner([0 100],0);
            this.LagSpinner.DecimalFormat = '0f';
            this.LagSpinner.StepSize = 1;
            this.LagSpinner.Tag = 'lagSpinner';
            this.LagSpinner.Description = getString(message('econ:econModeler:ModelLagsDescription'));
            col4.add(this.LagSpinner);
            
            this.TestStatDropDown = DropDown({'t1',getString(message('econ:econModeler:t1'));...
                't2',getString(message('econ:econModeler:t2'))});
            this.TestStatDropDown.Value = 't1';
            this.TestStatDropDown.Tag = 'testStatDropDown';
            this.TestStatDropDown.Description = getString(message('econ:econModeler:TestStatDescription')); 
            col4.add(this.TestStatDropDown);
            
            this.SigSpinner = Spinner([0.001,0.999],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'));
            col4.add(this.SigSpinner);
            
            %Test Section
            testsSection = Section(getString(message('econ:econModeler:Tests')));
            testsSection.Tag = 'testsSection';
            this.Tab.add(testsSection);
            
            %Add Button
            col5 = 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;
            col5.add(this.AddButton);
            
            %Clear Button 
            col6 = 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;
            col6.add(this.ClearButton);
            
            this.TabGroup.Tag = 'egciTabGroup';
            session.addTabGroup(this.TabGroup);
        end
    end
    
    methods(Access = protected)
        function addTest(this,~,~)
            parameters = {this.DependentVariableDropDown.SelectedIndex,...
                this.CRegDropDown.Value,...
                this.RRegDropDown.Value,...
                this.LagSpinner.Value,...
                this.TestStatDropDown.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:EGCIName'));
        end
        function setTabTag(this)
            this.Tab.Tag = 'egciTab';
        end
    end
    
    methods(Access = private)
        function updateDataChange(this,~,~)
            this.enableClear;
            
            %Set the values of the dependent variable dropdown
            if isempty(this.DependentVariableDropDown.Items) || ...
                    ~isequal(this.DependentVariableDropDown.Items(:,1),{this.Figure.Parent.Name}')
                this.DependentVariableDropDown.replaceAllItems({this.Figure.Parent.Name}');
                this.DependentVariableDropDown.SelectedIndex = 1;
            end
        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 PPFigure
            validateattributes(fig,{'internal.EconModeler.EconComponents.Figures.EGCIFigure'},{'scalar'});
            this.Figure = fig;
            
            %Set listner for changes in table size
            this.Listener = event.listener(fig,'DataChanged',@this.updateDataChange);
            this.updateDataChange;
        end
    end
end

