function renderMissingSupportPackageDialog(blockName,spkgName,spkgURL,libName)
%renderMissingSupportPackageDialog Missing support package dialog
%   This function is for internal use only and may be removed in a future
%   release.

%   renderMissingSupportPackageDialog creates a missing support package
%   message dialog. blockName is the name of the block calling this
%   function and is used as the dialog title. spkgName is the formal name
%   of the support package. spkgURL is the URL of the support package
%   landing page.

%   Copyright 2016 The MathWorks, Inc.

pathIssue = false;
if exist('sdrrroot', 'file') == 2
  if ~strcmp(fileparts(which('sdrrlib')), fullfile(sdrrroot,'sdrr'))
    pathIssue = true;
  end
end

if ~pathIssue
  msg = sprintf(['Cannot find ''%s''.\n' ...
    'To install this support package, click '...
    '''Get Support Package''. Simulink models that contain blocks, '...
    'which use this support package, will be closed. To continue '...
    'without installation, click ''Cancel''.'], spkgName);
  buttonString = 'Get Support Package';
else
  msg = sprintf(['Path for ''%s'' is not valid.\n'...
    'To fix this issue, click '...
    '''Fix Support Package''. Simulink models that contain blocks, '...
    'which use this support package, will be closed. To continue '...
    'without fixing the support package path, click ''Cancel''.'],...
    spkgName);
  buttonString = 'Fix Support Package';
end

d = dialog('Name', blockName, ...
  'Position', [1 1 560 140], ...
  'Visible', 'off', ...
  'WindowStyle', 'modal', ...
  'Resize', 'on', ...
  'Tag', 'MissingSupportPackage');

movegui(d, 'center')

% Create main container
mainContainer = uicontainer('Parent', d);

% (1) Create the main grid
gridMain = siglayout.gridbaglayout(mainContainer);
gridMain.VerticalGap = 15;
gridMain.HorizontalGap = 5;
gridMain.VerticalWeights = [1 0.25];
      
% (1.1) Text
msgText = uicontrol(mainContainer, ...
  'Style', 'text', ...
  'String', msg, ...
  'HorizontalAlignment', 'left', ...
  'Tag', 'Message Text');
add(gridMain, msgText, 1, 1, ...
  'Fill', 'Both', ...
  'MinimumHeight', msgText.Extent(4));

% (1.2) Buttons
buttonContainer = uicontainer(mainContainer);
add(gridMain, buttonContainer, 2, 1, ...
  'Fill', 'Both');

% Create a grid in 1.2
gridButtons = siglayout.gridbaglayout(buttonContainer);
gridButtons.VerticalGap = 1;
gridButtons.HorizontalGap = 5;
gridButtons.HorizontalWeights = [1 1 1 1];

% (1.2.2) Install/Fix button
installButton = uicontrol(buttonContainer, ...
  'Style', 'pushbutton', ...
  'String', buttonString, ...
  'Callback', {@installCallback,spkgURL,libName,pathIssue}, ...
  'Tag', 'Install/Fix Button');
add(gridButtons, installButton, 1, 2, ...
  'Fill', 'Both', ...
  'MinimumHeight', installButton.Extent(4)*2);

% (1.2.3) Cancel button
cancelButton = uicontrol(buttonContainer, ...
  'Style', 'pushbutton', ...
  'String', 'Cancel', ...
  'Callback', @cancelCallback, ...
  'Tag', 'Cancel Button');
add(gridButtons, cancelButton, 1, 3, ...
  'Fill', 'Both', ...
  'MinimumHeight', cancelButton.Extent(4)*2);

% (1.2.4) Space
spaceRight = uicontainer(buttonContainer);
add(gridButtons, spaceRight, 1, 4, ...
  'Fill', 'Both');

% Make it visible
set(d, 'Visible', 'on');
end

function installCallback(src,~,spkgURL,libName,pathIssue)
close(ancestor(src,'figure'));
closeShellLibrary(libName)
if pathIssue
  path(path,fullfile(matlabroot,'toolbox','comm','comm'));
else
  web(spkgURL, '-browser')
end
end

function cancelCallback(src,~)
close(ancestor(src,'figure'));
end