classdef(Hidden,Sealed) ToolStripSelectionHandler
    %TOOLSTRIPSELECTIONHANDLER control for the elements defined in
    %ToolStripDisplay
    
    %Copyright 2017-2023 The MathWorks, Inc.
    
    properties(Access = private)
        HomeTab
        PlotsTab
    end
    
    methods(Access = ?internal.EconModeler.Toolstrip.ToolStripDisplay)
        function this = ToolStripSelectionHandler(Display)
            this.HomeTab = Display.HomeTab;
            this.PlotsTab = Display.PlotsTab;
        end
        
        function newSelectionReaction(this,items)
            %Determine the number of selected time series and/or models
            numTS = sum(arrayfun(@(x)isa(x,'internal.EconModeler.EconComponents.AbstractTimeSeries'),items));
            numModels = sum(arrayfun(@(x)isa(x,'internal.EconModeler.EconComponents.AbstractModel'),items));
                        
            % Use number of TS and models to decide behavior
            if numTS == 1 && numModels == 0
                this.enableOneTS();
            elseif numTS == 2 && numModels == 0
                this.enableTwoTS();
            elseif numTS > 2 && numModels == 0
                this.enableMultiTS();
            elseif numTS == 0 && numModels == 1
                this.enableModel();
            elseif numModels >= 2 || (numTS >= 1 && numModels >= 1)
                this.enableInvalidSelection();
            elseif numTS == 0 && numModels == 0
                this.enableNoSelection();
            end            
        end
        
        function revertToInitialState(this)
            testSection = find(this.HomeTab,'testsection');
            testSection.disableAll();
            
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.disableAll();
            
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = getString(message('econ:econModeler:NoTSToModel'));
            modelGallery.Enabled = false;
             
            %Disable the diagnostics button
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = false;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = false;
            
            %Plots Tab
            plotsGallery = find(this.PlotsTab,'plotsgallery');
            plotsGallery.TextOverlay = getString(message('econ:econModeler:NoVariablesToPlot'));    
            plotsGallery.Enabled = false;
        end
    end
    
    methods(Access = private)
        function enableOneTS(this)
            %Reaction to one selected time series
            import matlab.internal.capability.Capability;
            
            %Enable the test section(All except Collinearity and Conintegration)
            testButton = find(this.HomeTab,'testbutton');
            testButton.Enabled = true;
            testGallery = testButton.Popup;
            stationarityCategory = find(testGallery,'stationarityCategory');
            stationarityCategory.enableAll();
            heteroscedasticityCategory = find(testGallery,'heteroscedasticityCategory');
            heteroscedasticityCategory.enableAll();
            autoCorrelationCategory = find(testGallery,'autoCorrelationCategory');
            autoCorrelationCategory.enableAll();
            collinearityCategory = find(testGallery,'collinearityCategory');  
            collinearityCategory.disableAll();
            cointegrationCategory = find(testGallery,'cointegrationCategory');
            cointegrationCategory.disableAll();
            
            %Enable the Transform section
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.enableAll();
            
            %Enable the model gallery
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = '';
            modelGallery.Enabled = true;
            
            %All categories besides multivariate should be enabled
            modelPopup = modelGallery.Popup;
            conditionalMeanCategory = find(modelPopup,'conditionalmeancategory');
            conditionalMeanCategory.enableAll();
            conditionalVarianceCategory = find(modelPopup,'conditionalvariancecategory');
            conditionalVarianceCategory.enableAll();
            regressionCategory = find(modelPopup,'regressioncategory');
            regressionCategory.enableAll();
            multivariateCategory = find(modelPopup,'multivariatecategory');
            multivariateCategory.disableAll();
            
            %Disable the diagnostics button
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = false;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = true;
            exportPopup = exportButton.Popup;
            exportItem = find(exportPopup,'exportvaritem');
            exportItem.Enabled = true;
            codeGenItem = find(exportPopup,'mcodeitem');
            codeGenItem.Enabled = false;
            liveCodeItem = find(exportPopup,'livecodeitem');
            liveCodeItem.Enabled = false;
            reportGenItem = find(exportPopup,'reportitem');
            reportGenItem.Enabled = Capability.isSupported(Capability.LocalClient);

            %Enable the plots gallery
            gallery = find(this.PlotsTab,'plotsgallery');
            gallery.TextOverlay = '';
            gallery.Enabled = true;
            
            plotspopup = gallery.Popup;
            
            %Enable the plot item  
            plotItem = find(plotspopup,'plotitem');
            plotItem.Enabled = true;
            
            yyItem = find(plotspopup,'yyItem');
            yyItem.Enabled = false;
            
            %Enable the autocorr item
            acfTSItem = find(plotspopup,'acftsitem');
            acfTSItem.Enabled = true;
             
            %Enable the parcorr item
            pacfItem = find(plotspopup,'pacfitem');
            pacfItem.Enabled = true;
             
            %Disable the corrplot item
            corrsItem = find(plotspopup,'corrsitem');
            corrsItem.Enabled = false;
             
            %Disable the entire model category
            modelPlotsCategory = find(plotspopup,'modelplotscategory');
            modelPlotsCategory.disableAll();           
        end
        
        function enableTwoTS(this)
            %Reaction to two selected time series
            import matlab.internal.capability.Capability;
            
            %Disable the test section(All except Collinearity and Cointegration)
            testButton = find(this.HomeTab,'testbutton');
            testButton.Enabled = true;
            testGallery = testButton.Popup;
            stationarityCategory = find(testGallery,'stationarityCategory');
            stationarityCategory.disableAll();
            heteroscedasticityCategory = find(testGallery,'heteroscedasticityCategory');
            heteroscedasticityCategory.disableAll();
            autoCorrelationCategory = find(testGallery,'autoCorrelationCategory');
            autoCorrelationCategory.disableAll();
            collinearityCategory = find(testGallery,'collinearityCategory');  
            collinearityCategory.enableAll();
            cointegrationCategory = find(testGallery,'cointegrationCategory');
            cointegrationCategory.enableAll();
            
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.enableAll();
            
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = '';
            modelGallery.Enabled = true;
            
            %Only enable the multivariate category
            modelPopup = modelGallery.Popup;
            conditionalMeanCategory = find(modelPopup,'conditionalmeancategory');
            conditionalMeanCategory.disableAll();
            conditionalVarianceCategory = find(modelPopup,'conditionalvariancecategory');
            conditionalVarianceCategory.disableAll();
            regressionCategory = find(modelPopup,'regressioncategory');
            regressionCategory.disableAll();
            multivariateCategory = find(modelPopup,'multivariatecategory');
            multivariateCategory.enableAll();
            
            %Disable the diagnostics button
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = false;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = true;
            exportPopup = exportButton.Popup;
            exportItem = find(exportPopup,'exportvaritem');
            exportItem.Enabled = true;
            codeGenItem = find(exportPopup,'mcodeitem');
            codeGenItem.Enabled = false;
            liveCodeItem = find(exportPopup,'livecodeitem');
            liveCodeItem.Enabled = false;
            reportGenItem = find(exportPopup,'reportitem');
            reportGenItem.Enabled = Capability.isSupported(Capability.LocalClient);
            
            %Plots Tab
            
            gallery = find(this.PlotsTab,'plotsgallery');
            gallery.TextOverlay = '';
            gallery.Enabled = true;
                        
            plotspopup = gallery.Popup;
              
            plotItem = find(plotspopup,'plotitem');
            plotItem.Enabled = true;
            
            yyItem = find(plotspopup,'yyItem');
            yyItem.Enabled = true;
             
            acfTSItem = find(plotspopup,'acftsitem');
            acfTSItem.Enabled = false;
             
            pacfItem = find(plotspopup,'pacfitem');
            pacfItem.Enabled = false;
             
            corrsItem = find(plotspopup,'corrsitem');
            corrsItem.Enabled = true;
             
            modelPlotsCategory = find(plotspopup,'modelplotscategory');
            modelPlotsCategory.disableAll();           
            
        end
        
        function enableMultiTS(this)
            import matlab.internal.capability.Capability;
            
            %Disable the test section(All except Collinearity and Cointegration)
            testButton = find(this.HomeTab,'testbutton');
            testButton.Enabled = true;
            testGallery = testButton.Popup;
            stationarityCategory = find(testGallery,'stationarityCategory');
            stationarityCategory.disableAll();
            heteroscedasticityCategory = find(testGallery,'heteroscedasticityCategory');
            heteroscedasticityCategory.disableAll();
            autoCorrelationCategory = find(testGallery,'autoCorrelationCategory');
            autoCorrelationCategory.disableAll();
            collinearityCategory = find(testGallery,'collinearityCategory');  
            collinearityCategory.enableAll();
            cointegrationCategory = find(testGallery,'cointegrationCategory');
            cointegrationCategory.enableAll();
            
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.enableAll();
            
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = '';
            modelGallery.Enabled = true;
            
            %Only enable the multivariate category
            modelPopup = modelGallery.Popup;
            conditionalMeanCategory = find(modelPopup,'conditionalmeancategory');
            conditionalMeanCategory.disableAll();
            conditionalVarianceCategory = find(modelPopup,'conditionalvariancecategory');
            conditionalVarianceCategory.disableAll();
            regressionCategory = find(modelPopup,'regressioncategory');
            regressionCategory.disableAll();
            multivariateCategory = find(modelPopup,'multivariatecategory');
            multivariateCategory.enableAll();
            
            %Disable the diagnostics button
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = false;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = true;
            exportPopup = exportButton.Popup;
            exportItem = find(exportPopup,'exportvaritem');
            exportItem.Enabled = true;
            codeGenItem = find(exportPopup,'mcodeitem');
            codeGenItem.Enabled = false;
            liveCodeItem = find(exportPopup,'livecodeitem');
            liveCodeItem.Enabled = false;
            reportGenItem = find(exportPopup,'reportitem');
            reportGenItem.Enabled = Capability.isSupported(Capability.LocalClient);
            
            % Plots Tab

            gallery = find(this.PlotsTab,'plotsgallery');
            gallery.TextOverlay = '';
            gallery.Enabled = true;
            
            plotspopup = gallery.Popup;
              
            plotItem = find(plotspopup,'plotitem');
            plotItem.Enabled = true;
            
            yyItem = find(plotspopup,'yyItem');
            yyItem.Enabled = false;
             
            acfTSItem = find(plotspopup,'acftsitem');
            acfTSItem.Enabled = false;
             
            pacfItem = find(plotspopup,'pacfitem');
            pacfItem.Enabled = false;
             
            corrsItem = find(plotspopup,'corrsitem');
            corrsItem.Enabled = true;
            
            collinItem = find(plotspopup,'collinitem');
            collinItem.Enabled = true;
             
            modelPlotsCategory = find(plotspopup,'modelplotscategory');
            modelPlotsCategory.disableAll();      
        end
        
        function enableModel(this)
            import matlab.internal.capability.Capability;
            
            testSection = find(this.HomeTab,'testsection');
            testSection.disableAll();
            
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.disableAll();
            
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = getString(message('econ:econModeler:NoTSToModel'));
            modelGallery.Enabled = false;
             
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = true;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = true;
            exportPopup = exportButton.Popup;
            exportItem = find(exportPopup,'exportvaritem');
            exportItem.Enabled = true;
            codeGenItem = find(exportPopup,'mcodeitem');
            codeGenItem.Enabled = true;
            liveCodeItem = find(exportPopup,'livecodeitem');
            liveCodeItem.Enabled = true;
            reportGenItem = find(exportPopup,'reportitem');
            reportGenItem.Enabled = Capability.isSupported(Capability.LocalClient);
            
            %Plots Section
            
            gallery = find(this.PlotsTab,'plotsgallery');
            gallery.TextOverlay = '';
            gallery.Enabled = true;
            
            plotspopup = gallery.Popup;
            
            tsPlotsCategory = find(plotspopup,'tsplotscategory');
            tsPlotsCategory.disableAll();
            
            modelPlotsCategory = find(plotspopup,'modelplotscategory');
            modelPlotsCategory.enableAll();  
            
        end
        
        function enableInvalidSelection(this)
            import matlab.internal.capability.Capability;
            
            testSection = find(this.HomeTab,'testsection');
            testSection.disableAll();
            
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.disableAll();
            
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = getString(message('econ:econModeler:NoTSToModel'));
            modelGallery.Enabled = false;
             
            %Disable the diagnostics button
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = false;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = true;
            exportPopup = exportButton.Popup;
            exportItem = find(exportPopup,'exportvaritem');
            exportItem.Enabled = true;
            codeGenItem = find(exportPopup,'mcodeitem');
            codeGenItem.Enabled = false;
            liveCodeItem = find(exportPopup,'livecodeitem');
            liveCodeItem.Enabled = false;
            reportGenItem = find(exportPopup,'reportitem');
            reportGenItem.Enabled = Capability.isSupported(Capability.LocalClient);
            
            %Plots Tab
            
            plotsGallery = find(this.PlotsTab,'plotsgallery');
            plotsGallery.TextOverlay = getString(message('econ:econModeler:NoVariablesToPlot'));    
            plotsGallery.Enabled = false;
        end
            
        
        function enableNoSelection(this)
            import matlab.internal.capability.Capability;
            
            testSection = find(this.HomeTab,'testsection');
            testSection.disableAll();
            
            transformSection = find(this.HomeTab,'transformsection');
            transformSection.disableAll();
            
            modelGallery = find(this.HomeTab,'modelgallery');
            modelGallery.TextOverlay = getString(message('econ:econModeler:NoTSToModel'));
            modelGallery.Enabled = false;
             
            %Disable the diagnostics button
            dgButton = find(this.HomeTab,'diagnosticsbutton');
            dgButton.Enabled = false;
            
            %Enable the export section
            exportButton = find(this.HomeTab,'exportButton');
            exportButton.Enabled = true;
            exportPopup = exportButton.Popup;
            exportItem = find(exportPopup,'exportvaritem');
            exportItem.Enabled = true;
            codeGenItem = find(exportPopup,'mcodeitem');
            codeGenItem.Enabled = false;
            liveCodeItem = find(exportPopup,'livecodeitem');
            liveCodeItem.Enabled = false;
            reportGenItem = find(exportPopup,'reportitem');
            reportGenItem.Enabled = Capability.isSupported(Capability.LocalClient);
            
            %Plots Tab
            
            plotsGallery = find(this.PlotsTab,'plotsgallery');
            plotsGallery.TextOverlay = getString(message('econ:econModeler:NoVariablesToPlot'));    
            plotsGallery.Enabled = false;
        end
    end    
end



