function dialogCallback(hObj, hDlg, tag, action)
%DIALOGCALLBACK - Callback for the HDLCC dialog.

%   Copyright 2005-2017 The MathWorks, Inc.

% For the use by event handler to enable the Apply button
actionIsEnableApply = strcmpi(action, 'enableApply');
if isempty(tag) && actionIsEnableApply
    EnableApplyButton(hDlg);
    return;
end

% Updates the HDL Coder object with the correct model name if necessary.
getDriver(hObj);

% Extract the action name (CheckHDL, Generate, Browse, Default)
ObjProp = tag(length('ConfigSet_HDLCoder_MainPanel_')+1:end);
setting_unchanged = true;
% Execute the dynamic-dialog callback. Note that the message viewer is used.
try
    setting_unchanged = feval([ObjProp '_callback'], hObj, hDlg, tag, action);
catch e
    % Use new message viewer instead of the simulink viewer.
    Simulink.output.Stage('HDLCoder', 'ModelName', gcs(), 'UIMode', true);
    Simulink.output.error( e,'Component','HDLCoder','Category','HDL');
end

if actionIsEnableApply && setting_unchanged == false
    EnableApplyButton(hDlg);
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function unchanged_flag = CheckHDL_callback(hObj, hDlg, tag, action) %#ok
% "Run Compatibility Checker" pushbutton callback.
hdriver = getDriver(hObj);
unchanged_flag = true;

% Check if the Configuration Parameters dialog has unapplied changes.
% If so, prompt the user.
commitBuild = slprivate('checkSimPrm', hObj.getSourceObject.getConfigSet);
if commitBuild
    hdriver.checkhdl;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function unchanged_flag = Generate_callback(hObj, hDlg, tag, action) %#ok
% "Generate" pushbutton callback.
hdriver = getDriver(hObj);
unchanged_flag = true;

% Check if the Configuration Parameters dialog has unapplied changes,
% if so, prompt the user.
commitBuild = slprivate('checkSimPrm', hObj.getSourceObject.getConfigSet);
if commitBuild
    hdriver.makehdl;
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function unchanged_flag = Browse_callback(hObj, hDlg, tag, action) %#ok
unchanged_flag = true;

tdir = uigetdir('',DAStudio.message('HDLShared:hdldialog:hdlccSelectTargetDir'));
if tdir ~= 0
    hdlcc = gethdlcconfigset(hObj.getSourceObject.getConfigSet);
    hCLI = hdlcc.getCLI;
    set(hCLI, 'TargetDirectory', tdir);
    unchanged_flag = false;
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function unchanged_flag = Default_callback(hObj, hDlg, tag, action) %#ok
% "Restore Factory Defaults" pushbutton callback.
unchanged_flag = true;
str = DAStudio.message('HDLShared:hdldialog:hdlccRestoreDefaultQuestion');

% Prompt the user for restore factory setting and lose current settings
answer = questdlg(str, ...
    DAStudio.message('HDLShared:hdldialog:hdlccRestoreDefault'),...
    DAStudio.message('HDLShared:hdldialog:hdlccYes'),...
    DAStudio.message('HDLShared:hdldialog:hdlccNo'),...
    DAStudio.message('HDLShared:hdldialog:hdlccNo'));

if strcmp(answer, DAStudio.message('HDLShared:hdldialog:hdlccYes'))
    modelName = hObj.getModelName;
    hObj.registerEventsAtPostLoad(modelName);

    % create new CLI on hdlcc without reading existing params
    hObj.createCLI(false);

    % clear all params on the model and reset the start node name
    mP = slprops.hdlmdlprops({'HDLSubsystem', modelName});
    set_param(modelName, 'HDLParams', mP);

    % the above call should do this anyway...
    set_param(modelName, 'dirty', 'on');
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function EnableApplyButton(hDlg)
% Force Apply button to be enabled; bug in Dynamic Dialog
if ~isempty(hDlg) && isa(hDlg, 'DAStudio.Dialog')
    hDlg.enableApplyButton(true);
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [hdriver, mdlName] = getDriver(hObj)
% Get the HDLCoder driver associated w/ this model. Ensure it has the correct
% model name in it.
mdlName = hObj.getModelName;
hdriver = hObj.getHDLCoder;
if ~strcmp(mdlName, hdriver.ModelName)
    hdriver.ModelName = mdlName;
end
end
