function fullviewlink(hFDA)
%FULLVIEWLINK Creates a linked FVTool to FilterDesigner

%   Author(s): J. Schickler
%   Copyright 1988-2021 The MathWorks, Inc.

hFig = get(hFDA, 'FigureHandle');
setptr(hFig, 'watch');

% Create a full view of FilterDesigner's Analysis
hfig = createfullview(hFDA);

% Link the full view to FilterDesigner
installfullviewlink(hfig, hFDA);

setptr(hFig, 'arrow');

end

%-------------------------------------------------------------------------
function hfig = createfullview(hFDA)
% Load a session of FVTool that matches FilterDesigner

% Get Filter information
filtobj = copy(getfilter(hFDA, 'wfs'));

% Get current analysis
hFVT        = getfvtoolhandle(hFDA);

hfig = fvtool(filtobj,param2struct(get(hFVT, 'Parameters')),...
    'Analysis', hFVT.Analysis, ...
    'OverlayedAnalysis', hFVT.OverlayedAnalysis, ...
    'Grid', hFVT.Grid, 'Legend', hFVT.Legend, 'DesignMask', hFVT.DisplayMask, ...
    'IsOpenWithLinkAndReplace', true);

end

%-------------------------------------------------------------------------
function installfullviewlink(hfig, hFDA)
%Link FilterDesigner and FVTool

appContainer = sigtools.fvtool.setGetInstance();
userData = appContainer.getDocumentGroup("fvToolFigureDocumentGroup").UserData;

% Add Value Change callback to FVTool LinkButton
linkButton = userData.LinkButton;
linkButton.ValueChangedFcn = {@fullview_link_cb, hFDA, hfig};

% Add Value Change callback to FVTool ReplaceCheckBox
replaceCheckBox = userData.ReplaceCheckBox;
replaceCheckBox.ValueChangedFcn = {@fullview_mode_cb, hfig};

% Set up listeners to FDATool's filter and FDAToolClosing
link.h.listen = handle.listener(hFDA, 'FilterUpdated', {@filterlistener, hfig});
link.h.listen(2) = handle.listener(hFDA, 'sigguiClosing', {@fdatoolclose_eventcb, hfig});

setappdata(hfig, 'fullviewlink', link);

end

%-------------------------------------------------------------------------
function filterlistener(hFDA, ~, hfig)
% Listener to the filter of fdatool

filtobj = getfilter(hFDA, 'wfs');
hFVT = siggetappdata(hfig, 'fvtool', 'handle');

appContainer = sigtools.fvtool.setGetInstance();
replaceCheckBox = appContainer.getDocumentGroup("fvToolFigureDocumentGroup").UserData.ReplaceCheckBox;

if replaceCheckBox.Value
    
    % Replace the last filter in FVTool with the new filter
    filtobjs = get(hFVT,'Filters');
    filtobjs(end) = filtobj;
    hFVT.setfilter(filtobjs);
else
    
    % Add FDATool's filter to FVTool
    hFVT.addfilter(filtobj);
end

end

%-------------------------------------------------------------------------
function fdatoolclose_eventcb(~, ~, hfig)
% Delete the full view link when FDATool is destroyed.

% Get and remove the appdata from FVTool
appStr = 'fullviewlink';
link   = getappdata(hfig, appStr);
rmappdata(hfig, appStr);

% Disable and Deselect Filter Designeer Buttons in FVTool
appContainer = sigtools.fvtool.setGetInstance();
userData = appContainer.getDocumentGroup("fvToolFigureDocumentGroup").UserData;
linkButton = userData.LinkButton;
linkButton.Enabled = false;
linkButton.Value = false;
replaceCheckBox = userData.ReplaceCheckBox;
replaceCheckBox.Enabled = false;
replaceCheckBox.Value = false;

selectedDocument = appContainer.getDocument(appContainer.SelectedChild.documentGroupTag, appContainer.SelectedChild.tag);
selectedDocument.UserData.IsOpenWithLinkAndReplace = false;

% Delete the listeners
delete(link.h.listen);

end

%-------------------------------------------------------------------------
%       Callbacks
%-------------------------------------------------------------------------

%-------------------------------------------------------------------------
function fullview_mode_cb(~, eventStruct, hfig)
% Callback to change link mode

import matlab.ui.internal.toolstrip.Icon;

appContainer = sigtools.fvtool.setGetInstance();
selectedDocument = appContainer.getDocument(appContainer.SelectedChild.documentGroupTag, appContainer.SelectedChild.tag);

% Update the state of Replace Check Box
selectedDocument.UserData.ReplaceCheckBoxSelectedState = eventStruct.EventData.NewValue;

lclfixtoolbars(hfig)

end

%-------------------------------------------------------------------------
function fullview_link_cb(~, eventStruct, hFDA, hfig)
% Callback to change linking between FVTool and Filter Designer

import matlab.ui.internal.toolstrip.Icon;

appContainer = sigtools.fvtool.setGetInstance();
userData = appContainer.getDocumentGroup("fvToolFigureDocumentGroup").UserData;
selectedDocument = appContainer.getDocument(appContainer.SelectedChild.documentGroupTag, appContainer.SelectedChild.tag);
newValue = eventStruct.EventData.NewValue;

replaceCheckBox = userData.ReplaceCheckBox;


state = matlab.lang.OnOffSwitchState(newValue);

% Enable or disable the filterlistener depending on the state of the button
link = getappdata(hfig, 'fullviewlink');
set(link.h.listen(1), 'Enabled', state);

if newValue
    % Resync the filters
    filterlistener(hFDA, eventStruct, hfig);
end

% Update the state for the Link Button
selectedDocument.UserData.LinkButtonSelectedState = newValue;

lclfixtoolbars(hfig)

% Enable or disable the Replace Check Box depending on the state of the Link button
replaceCheckBox.Enabled = newValue;

end

%-------------------------------------------------------------------
function lclfixtoolbars(hfig)

% This was meant to fix g142297, but it causes a SEGV g142769
% set(hfig, 'Children', get(hfig, 'Children'));

end

% [EOF]