function pdemhdlg(action)
%PDEMHDLG creates and manages the mesh parameter dialog box.
%       PDEMHDLG is not a stand-alone function but is called from pdeModeler.

%       Copyright 1994-2023 The MathWorks, Inc.

if nargin==0
  action='Mesh Parameters';
end

% Check if figure is already on screen
if  figflag(action)
  % No need to create new dialog
  return

elseif strcmp(action,'OKCallback')

  pde_fig=findobj(get(0,'Children'),'flat','Tag','PDETool');
  ax=findobj(get(pde_fig,'Children'),'flat','Tag','PDEAxes');
  fig=gcbf;

  ud=get(fig,'UserData');

  tristr=get(ud(1),'String');
  try
      if isequal(tristr,char())
          trisize = [];
      else
          trisize = eval(tristr);
      end
      mustBeA(trisize,'double')
      mustBeScalarOrEmpty(trisize)
      mustBeNonNan(trisize)
      mustBePositive(trisize)
      mustBeNonsparse(trisize)
      mustBeReal(trisize)
      setappdata(pde_fig,'trisize',trisize)
      set(ud(1),'UserData',get(ud(1),'String'))
  catch me
      pdeModeler('error',me.message)
      set(ud(1),'String',get(ud(1),'UserData'))
      return
  end

  str=get(ud(2),'String');
  try
      Hgrad = eval(str);
      mustBeA(Hgrad,'double')
      if numel(Hgrad)>1
          error(message('MATLAB:validation:NotScalar'))
      end
      mustBeNonempty(Hgrad)
      mustBeNonNan(Hgrad)
      mustBeNonsparse(Hgrad)
      mustBeInRange(Hgrad,1,2,"exclusive")
      setappdata(pde_fig,'Hgrad',Hgrad)
      set(ud(2),'UserData',get(ud(2),'String'))
  catch me
      pdeModeler('error',me.message)
      set(ud(2),'String',get(ud(2),'UserData'))
      return
  end

  if get(ud(3),'Value')
    jiggle='on';
  else
    jiggle='off';
  end

  mode=get(ud(4),'Value');
  if mode==1
    jigglemode='on';
  elseif mode==2
    jigglemode='minimum';
  elseif mode==3
    jigglemode='mean';
  end

  str=deblank(get(ud(5),'String'));
  try
      if isequal(str,char())
          jiggleiter  = [];
      else
          jiggleiter = eval(str);
      end
      mustBeA(jiggleiter,'double')
      mustBeScalarOrEmpty(jiggleiter)
      mustBeInteger(jiggleiter)
      mustBeInRange(jiggleiter,0,20,"inclusive")
      mustBeNonsparse(jiggleiter)
      jiggleiter = num2str(jiggleiter);
      set(ud(5),'UserData',jiggleiter)
  catch me
      pdeModeler('error',me.message);
      set(ud(5),'String',get(ud(5),'UserData'))
      return
  end

  setappdata(pde_fig,'jiggle',char(jiggle,jigglemode,jiggleiter))

  refval=get(ud(6),'Value');
  if refval==1
    refmet='regular';
  elseif refval==2
    refmet='longest';
  end
  setappdata(pde_fig,'refinemethod',refmet);
  
  meshAlgo = get(ud(7),'Value');
  if(meshAlgo==1)
    meshAlgoName = 'preR2013a';
  elseif(meshAlgo==2)
    meshAlgoName = 'R2013a';
  end
  setappdata(pde_fig,'MesherVersion',meshAlgoName);

  delete(fig)
  drawnow

  pdeinfo

  return

end

pdeinfo('Set options for jiggling and refinement of mesh.');

pde_fig=findobj(get(0,'Children'),'flat','Tag','PDETool');
set(pde_fig,'Pointer','watch')
drawnow

ax=findobj(get(pde_fig,'Children'),'flat','Tag','PDEAxes');

DlgName = 'Mesh Parameters';
PromptString = char('Initmesh parameters','Maximum edge size:',...
    'Mesh growth rate:', 'Mesher version:', 'Jiggle mesh', ...
    'Jigglemesh parameters',...
    'Jiggle mode:','Number of jiggle iterations:','Refinement method:');
TextSize = size(PromptString);

% Get layout parameters
pdelayout
mLineHeight = mLineHeight+8;
BWH = [mStdButtonWidth mStdButtonHeight+3];

% Define default position
ScreenUnits = get(0,'Units');
set(0,'Unit','pixels');
ScreenPos = get(0,'ScreenSize');
set(0,'Unit',ScreenUnits);
mCharacterWidth = 7;
Voff = 5;
numoflines=15;
FigWH = [TextSize(2) numoflines].*[mCharacterWidth BWH(2)+Voff] + [0 5*Voff]...
    +[2*(mEdgeToFrame+mFrameToText)+BWH(1)+mFrameToText mLineHeight+BWH(2)+Voff];
MinFigW = 2*(BWH(1) +mFrameToText + mEdgeToFrame);
FigWH(1) = max([FigWH(1) MinFigW]);
FigWH = min(FigWH,ScreenPos(3:4)-50);

Position = [(ScreenPos(3:4)-FigWH)/2 FigWH];

% Make the figure
DefUIBgColor = get(0,'DefaultUIControlBackgroundColor');
fig = figure('NumberTitle','off',...
    'Name',DlgName,...
    'Units','pixels',...
    'Position',Position,...
    'IntegerHandle','off',...
    'HandleVisibility','callback',...
    'MenuBar','none',...
    'Colormap',zeros(1,3),...
    'Color',DefUIBgColor,...
    'Visible','off',...
    'Tag','PDEMeshDlg');
matlab.graphics.internal.themes.figureUseDesktopTheme(fig);

% Make the 4 frame uicontrols
UIPos = mEdgeToFrame*[1 1 -2 -2] + [0 0 FigWH(1) BWH(2)+mLineHeight];
uicontrol(fig,'Style','frame','Position',UIPos);
UIPos = [UIPos(1:3)+[0 UIPos(4)+mEdgeToFrame 0] 2*(BWH(2)+Voff)+8*mEdgeToFrame];
uicontrol(fig,'Style','frame','Position',UIPos);
UIPos = [UIPos(1:3)+[0 UIPos(4)+mEdgeToFrame 0] 5*(BWH(2)+Voff)+6*mEdgeToFrame];
uicontrol(fig,'Style','frame','Position',UIPos);
UIPos = [UIPos(1:3)+[0 UIPos(4)+mEdgeToFrame 0] 8*(BWH(2)+Voff)+ 15*mEdgeToFrame];
uicontrol(fig,'Style','frame','Position',UIPos);

jiggleparam=getappdata(pde_fig,'jiggle');
jiggle=strcmp(deblank(jiggleparam(1,:)),'on');
jigglemode=deblank(jiggleparam(2,:));
if strcmp(jigglemode,'on')
  popupval1=1;
elseif strcmp(jigglemode,'minimum')
  popupval1=2;
elseif strcmp(jigglemode,'mean')
  popupval1=3;
end
jiggleiter=deblank(jiggleparam(3,:));

editstr1=num2str(getappdata(pde_fig,'trisize'));
editstr2=num2str(getappdata(pde_fig,'Hgrad'));
popupstr=getappdata(pde_fig,'refinemethod');
if strcmp(popupstr,'regular')
  popupval2=1;
elseif strcmp(popupstr,'longest')
  popupval2=2;
end
meshAlgoStr=getappdata(pde_fig,'MesherVersion');
if(isempty(meshAlgoStr) || strcmp(meshAlgoStr, 'preR2013a'))
  meshAlgoVal = 1;
elseif(strcmp(meshAlgoStr,'R2013a'))
  meshAlgoVal = 2;
end

% Make the check, text, and edit uicontrol(s)
UIPos = [mEdgeToFrame+mFrameToText FigWH(2)-3*mEdgeToFrame  ...
        FigWH(1)-2*mEdgeToFrame-2*mFrameToText mLineHeight];
ud = zeros(7,1);
for i=1:TextSize(1)
  UIPos = UIPos - [0 BWH(2) 0 0];
  if i==1 || i==6
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
        'Position',UIPos,'HorizontalAlignment','left');
  elseif i==2
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
        'Position',UIPos,'HorizontalAlignment','left','Tag',int2str(i));
    UIPos = UIPos -[0 BWH(2)+Voff 0 0];
    ud(1)=uicontrol(fig,'Style','edit',...
        'String',editstr1,'Position',UIPos,...
        'UserData',editstr1,'HorizontalAlignment','left', ...
        'Tag','PDEMeshDlgEditHmax');
  elseif i==3
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
        'Position',UIPos,'HorizontalAlignment','left','Tag',int2str(i));
    UIPos = UIPos -[0 BWH(2)+Voff 0 0];
    ud(2)=uicontrol(fig,'Style','edit',...
        'String',editstr2,'Position',UIPos,...
        'UserData',editstr2,'HorizontalAlignment','left', ...
        'Tag','PDEMeshDlgEditHgrad');
  elseif i==4
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
      'Position',UIPos,'HorizontalAlignment','left','Tag',int2str(i));
    UIPos = UIPos -[0 BWH(2)+Voff 0 0];
    ud(7)=uicontrol(fig,'Style','popupmenu',...
        'String',{'preR2013a','R2013a'},...
        'Position',UIPos,'Value',meshAlgoVal,...
        'HorizontalAlignment','left', ...
        'Tag','PDEMeshDlgVer');
    UIPos = UIPos -[0 Voff+2*mEdgeToFrame 0 0];
  elseif i==5
    ud(3)=uicontrol(fig,'Style','checkbox',...
        'String',PromptString(i,:),...
        'Position',UIPos,'Value',jiggle,...
        'HorizontalAlignment','left', ...
        'Tag','PDEMeshDlgJiggle');
    UIPos = UIPos -[0 Voff+2*mEdgeToFrame 0 0];
  elseif i==7
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
        'Position',UIPos,'HorizontalAlignment','left','Tag',int2str(i));
    UIPos = UIPos -[0 BWH(2)+Voff 0 0];
    ud(4)=uicontrol(fig,'Style','popup',...
        'String','on|optimize minimum|optimize mean','Value',popupval1,...
        'Position',UIPos,'HorizontalAlignment','left', ...
        'Tag','PDEMeshDlgOptimize');
  elseif i==8
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
        'Position',UIPos,'HorizontalAlignment','left','Tag',int2str(i));
    UIPos = UIPos -[0 BWH(2)+Voff 0 0];
    ud(5)=uicontrol(fig,'Style','edit',...
        'String',jiggleiter,'Position',UIPos,...
        'UserData',jiggleiter,'HorizontalAlignment','left');
  elseif i==9
    UIPos = UIPos -[0 Voff+2*mEdgeToFrame 0 0];
    uicontrol(fig,'Style','text','String',PromptString(i,:),...
        'Position',UIPos,'HorizontalAlignment','left','Tag',int2str(i));
    UIPos = UIPos -[0 BWH(2)+Voff 0 0];
    ud(6)=uicontrol(fig,'Style','popup',...
        'String','regular|longest','Value',popupval2,...
        'Position',UIPos,'HorizontalAlignment','left', ...
        'Tag','PDEMeshDlgRefine');
  end
  UIPos = UIPos -[0 Voff 0 0];
end

% Make the pushbuttons
Hspace = (FigWH(1)-2*BWH(1))/3;
OKFcn = 'pdemhdlg(''OKCallback'')';
uicontrol(fig,'Style','push','String','OK','Callback',OKFcn, ...
    'Position',[Hspace mLineHeight/2 BWH],'Tag','PDEMeshDlgOKBtn');
uicontrol(fig,'Style','push','String','Cancel',...
    'Callback','pdeinfo; delete(gcbf); drawnow',...
    'Tag','PDEMeshDlgCancelBtn', ...
    'Position',[2*Hspace+BWH(1) mLineHeight/2 BWH]);

% Finally, make all the uicontrols normalized and the figure visible
set(pde_fig,'Pointer','arrow')
set(get(fig,'Children'),'Unit','norm');
set(fig,'Visible','on','UserData',ud)
drawnow

% end pdemhdlg