classdef PIDTuner < handle
    %PIDTUNER
    
    % Copyright 2013-2018 The MathWorks, Inc.
    
    properties
        Parent
        TC
        TabGC
        TabGroupGC
        ResponsePlots
        ImportDialog
        MessagePanel
        isGroupActionClosing = false
    end
    
    methods
        function this = PIDTuner(tooldesktop, desiredtype, baselinecontroller)
            %PIDTUNER
            
            this.Parent = tooldesktop;
            this.TC = pidtool.desktop.pidtuner.PIDTunerTC(this.Parent.PlantList, desiredtype, baselinecontroller, this.Parent.SimulinkGateway, this.Parent.Type);
            TabGC = pidtool.desktop.pidtuner.PIDTunerTabGC(this.TC);
            this.TabGC = TabGC;
            this.TabGroupGC = matlab.ui.internal.toolstrip.TabGroup();
            this.Parent.TPComponent.addTabGroup(this.TabGroupGC);
            this.TabGroupGC.add(this.TabGC.TPComponent);
            addlistener(this.TabGC.DesignSection,'AddNewPlot',@(~,evnt)this.addResponsePlot(evnt.PlotType, evnt.ResponseType));
            addlistener(this.Parent.PlantList, 'ImportRequested', @(~,~)this.launchImportDialog());
            addlistener(this.Parent.PlantList, 'SelectedPlantIndex', 'PostSet', @(~,~)cbSelectedPlantChanged(this));
            addlistener(this.TabGC.PlantSection.InspectPlantButton, 'ButtonPushed', @(~,~) cbInspectPlant(this));
        end
        function addResponsePlot(this, plottype, responsetype)
            %ADDRESPONSEPLOT
            this.Parent.configureTiling(this);
            responseplot = pidtool.desktop.pidtuner.ResponsePlotGC(plottype, responsetype, this.TC.DataSourcePlot);
            addlistener(responseplot, 'FigureCloseRequested', @(src, ~)cbCloseFigureRequest(src, this));
            this.Parent.TPComponent.addFigure(responseplot.Figure);
            this.ResponsePlots = [this.ResponsePlots; responseplot];
            WarningState = warning('off');
            set(responseplot.Figure,'visible','on');
            warning(WarningState);
            
            % Add Floating Palette to Figure
            controllib.plot.internal.createToolbar(responseplot.AxesHandle);
        end
        
        %% Button Callbacks
        function launchImportDialog(this)
            if isempty(this.ImportDialog)
                if strcmp(this.TC.ToolType, 'MATLAB')
                    this.ImportDialog = pidtool.desktop.pidtuner.gc.ImportDialogGC(this.TC.ImportDialogTC);
                else
                    this.ImportDialog = slctrlguis.pidtuner.gc.ImportDialogGC(this.TC.ImportDialogTC);
                end
            end
            this.ImportDialog.open(this);
        end
        
        %% Messages
        function postMessageOnPanel(this, str)
            show = true;
            if isempty(this.MessagePanel) || ~isvalid(this.MessagePanel)
                this.MessagePanel = ctrluis.toolstrip.MessagePanel(this.TC.DataSourcePlot.ActiveFigure);
            else
                show = ~this.MessagePanel.Minimized;
            end
            this.MessagePanel.Message = str;
            this.MessagePanel.Visible = true;
            if show
                this.MessagePanel.Minimized = false;
            else
                this.MessagePanel.Minimized = true;
            end
        end
        function removeMessagePanel(this)
            if ~isempty(this.MessagePanel) && isvalid(this.MessagePanel)
                this.MessagePanel.Visible = false;
            end
        end
        function updateMessagePanel(this)
            if this.Parent.PlantList.isSelectedPlantZero
                if this.Parent.PlantList.isSelectedPlantAdded
                    if this.Parent.PlantList.isSelectedPlantLinearized
                        txt = ctrlMsgUtils.message('Slcontrol:pidtuner:strLinearizedPlantZero','<b>','</b>','<br>');
                    else
                        txt = ctrlMsgUtils.message('Control:pidtool:strSelectedPlantZero','<b>','</b>','<br>');
                    end
                else
                    txt = ctrlMsgUtils.message('Control:pidtool:strSelectedPlantZero','<b>','</b>','<br>');
                end
                this.postMessageOnPanel(txt);
            else
                this.removeMessagePanel();
            end
        end
    end
end

function cbSelectedPlantChanged(this)
this.updateMessagePanel();
end

function cbInspectPlant(this)
selectedplant = this.TC.PlantList.SelectedPlantName();
if isempty(this.TC.InspectorTC) || ~isa(this.TC.InspectorTC,'slctrlguis.lintool.dialogs.results.LinInspectorTC')
    ve = toolpack.databrowser.VariableEditorManager.getInstance();
    ve.openDialog(this.TC.PlantList.LocalWorkspace, selectedplant, ...
        this.TabGC.PlantSection.InspectPlantButton, this.Parent.TPComponent);
else
    this.TC.InspectorTC.setVariableName([': ' selectedplant]);
    this.TC.InspectorTC.update();
    inspectorgc = this.TC.InspectorTC.createView();
    inspectorgc.show(this.TabGC.PlantSection.InspectPlantButton,this.Parent.TPComponent);
end
end

function cbCloseFigureRequest(src, this)
if ishghandle(src.Figure) && ~this.isGroupActionClosing
    if length(this.ResponsePlots) == 1
        if  strcmp(get(src.Figure, 'BeingDeleted'), 'on') % close all force call
            src.closeFigureCleanup();
            close(this.Parent);
        else
            if strcmp(this.Parent.Type, 'MATLAB')
                txt = pidtool.utPIDgetStrings('cst', 'strWantToCloseTool');
            else
                txt = pidtool.utPIDgetStrings('scd', 'strWantToCloseTool');
            end
            response = questdlg(txt, ...
                pidtool.utPIDgetStrings('cst', 'tunerdlg_title'), ...
                pidtool.utPIDgetStrings('cst', 'button_close'),...
                pidtool.utPIDgetStrings('cst', 'button_cancel'),...
                pidtool.utPIDgetStrings('cst', 'button_cancel'));
            if strcmp(response, pidtool.utPIDgetStrings('cst', 'button_close'))
                src.closeFigureCleanup();
                close(this.Parent);
            else
                this.isGroupActionClosing = false;
            end
        end
    else
        this.ResponsePlots(this.ResponsePlots == src) = [];
        src.closeFigureCleanup();
    end
else
    src.closeFigureCleanup();
end
end

