% Callback to build the query conditions panel.
%
% $Id: buildConditions.m,v 1.5 2011/04/08 08:56:26 hewitson Exp $
%
function buildConditions(mainfig)

  ALLHEIGHT = 0.06;
  pmarg = 0.025;

  panel       = mainfig.panels(3);
  Ppos        = get(panel, 'Position');
  Nc          = mainfig.Nconditions;
  conn        = mainfig.connection;
  Gproperties = mainfig.Gproperties;
  fontsize    = Gproperties.fontsize;

  % before deleting, record the table selection
  tableSelected = mainfig.tableSelected;
  % record field selection
  fieldList = findobj(mainfig.handle, 'Tag', 'fieldsList');
  fselection = get(fieldList, 'Value');
  % record order by
  fieldOrder = findobj(mainfig.handle, 'Tag', 'fieldOrderBy');
  forder     = get(fieldOrder, 'Value');
  % record sort direction
  sortDir    = findobj(mainfig.handle, 'Tag', 'sortDir');
  sort       = get(sortDir, 'Value');
  % record current conditions
  conds = cell(Nc,4);
  for k=1:Nc
    pmh = findobj(mainfig.handle, 'Tag', sprintf('c%02dField', k));
    conds{k,1} = get(pmh, 'Value');
    pmh = findobj(mainfig.handle, 'Tag', sprintf('c%02dCondition', k));
    conds{k,2} = get(pmh, 'Value');
    pmh = findobj(mainfig.handle, 'Tag', sprintf('c%02dValue', k));
    conds{k,3} = get(pmh, 'String');
    if k<Nc
      pmh = findobj(mainfig.handle, 'Tag', sprintf('c%02dCompound', k));
      conds{k,4} = get(pmh, 'Value');
    end
  end

  ch = get(panel, 'Children');
  if ~isempty(ch)
    delete(ch)
    drawnow
  end
  %--------------------------------------------------------------------------
  %-- Draw Table selection

  % get tables
  tables  = repogui.getTables(mainfig);
  Sleft   = pmarg;
  Sheight = ALLHEIGHT;
  Sbottom = 1 -pmarg - Sheight;
  Swidth  = 0.1;
  
  sth = uicontrol(panel,'Style','text',...
    'String', 'From',...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'HorizontalAlignment', 'left',...
    'Position',[Sleft Sbottom Swidth Sheight]);

  Sleft  = Sleft + Swidth + pmarg;
  Swidth = 0.15;
  pmh = uicontrol(panel,'Style','popupmenu',...
    'String', tables,...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'Value', 1,...
    'Position',[Sleft Sbottom Swidth Sheight],...
    'Tag', 'tableSelect');
  if ~isempty(tableSelected) && tableSelected <= numel(tables)
    set(pmh, 'Value', tableSelected);
  end
  set(pmh, 'Callback', {'repogui.cb_tableSelect', pmh, mainfig});

  %--------------------------------------------------------------------------
  %-- Draw Fields Selection list

  % get fields
  fields = repogui.getFields(mainfig, conn);
  Sleft   = pmarg;
  Sheight = ALLHEIGHT;
  Sbottom = Sbottom - pmarg - Sheight;
  Swidth  = 0.1;

  sth = uicontrol(panel,'Style','text',...
    'String', 'Select',...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'HorizontalAlignment', 'left',...
    'Position',[Sleft Sbottom Swidth Sheight]);

  Sheight = 0.5;
  Sleft   = Sleft + pmarg + Swidth;
  Sbottom = 1 - 2*pmarg - ALLHEIGHT - Sheight;
  Swidth  = 0.15;
  lbh = uicontrol(panel,'Style','listbox',...
    'String', fields,...
    'Value',1,...
    'Units', 'normalized', ...
    'BackgroundColor', 'w',...
    'Fontsize', fontsize,...
    'Max', 1000,...
    'Position',[Sleft Sbottom Swidth Sheight],...
    'Tag', 'fieldsList');

  set(lbh, 'Callback', {'repogui.cb_fieldSelect', pmh, mainfig});
  if ~isempty(fselection) && tableSelected <= numel(fields)
    set(lbh, 'Value', fselection);
  end

  %--------------------------------------------------------------------------
  %-- Query display
  listpos = get(lbh, 'Position');
  Sleft   = pmarg;
  Sheight = ALLHEIGHT;
  Sbottom = 0.1; %listpos(2)  - 40 - Sheight;
  Swidth  = 0.1;

  sth = uicontrol(panel,'Style','text',...
    'String', 'Query:',...
    'Units', 'normalized', ...
    'Fontsize', fontsize,...
    'Max', 5,...
    'BackgroundColor', Gproperties.Gcol{3},...
    'HorizontalAlignment', 'left',...
    'Position', [Sleft Sbottom Swidth Sheight]);

  Sleft   = Sleft + Swidth + pmarg;
  Swidth  = 1-Sleft-pmarg;
  Sheight = 0.2;
  Sbottom = 0.06 + pmarg;
  sth = uicontrol(panel,'Style','edit',...
    'String', '',...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'Max', 5,...
    'BackgroundColor', 'w',...
    'HorizontalAlignment', 'left',...
    'Position', [Sleft Sbottom Swidth Sheight],...
    'Tag', 'queryDisplayTxt');



  %--------------------------------------------------------------------------
  %-- Draw conditions

  conditions = {'=', '<>', '<=', '>=', '<', '>', 'like', 'not like', 'is'};
  compounds  = {'AND', 'OR', 'XOR'};

  pos = get(lbh, 'Position');
  Sleft   = 0.35;
  Sheight = ALLHEIGHT;
  Sbottom = 1-pmarg-Sheight;
  Swidth  = 0.1;

  sth = uicontrol(panel,'Style','text',...
    'String', 'Where',...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'HorizontalAlignment', 'left',...
    'Position',[Sleft Sbottom Swidth Sheight]);


  pos = get(sth, 'Position');
  Cheight = ALLHEIGHT;
  STEP    = pmarg;
  for k=1:Nc
    Cleft   = pos(1);
    Cbottom = pos(2) - pos(4) - pmarg - (k-1)*(Cheight+pmarg);
    Cwidth  = 0.1;
    % Field Dropdown
    pmh = uicontrol(panel,'Style','popupmenu',...
      'String', fields,...
      'Fontsize', fontsize,...
      'Units', 'normalized', ...
      'BackgroundColor', 'w', ...
      'Value',1,'Position',[Cleft Cbottom Cwidth Cheight],...
      'Tag', sprintf('c%02dField', k));
    set(pmh, 'Callback', {'repogui.buildquery', mainfig});
    if ~isempty(conds{k,1}) && conds{k,1} <= numel(fields)
      set(pmh, 'Value', conds{k,1});
    end
    %   Gproperties.Gcol(3),
    % Condition Dropdown
    Cleft  = Cleft + Cwidth + STEP;
    Cwidth = 0.1;
    pmh = uicontrol(panel,'Style','popupmenu',...
      'String', conditions,...
      'Fontsize', fontsize,...
      'Units', 'normalized', ...
      'BackgroundColor', 'w',...
      'Value',1,'Position',[Cleft Cbottom Cwidth Cheight],...
      'Tag', sprintf('c%02dCondition', k));
    set(pmh, 'Callback', {'repogui.buildquery', mainfig});
    if ~isempty(conds{k,2})
      set(pmh, 'Value', conds{k,2});
    end

    % Value txt box
    Cleft  = Cleft + Cwidth + STEP;
    Cwidth = 0.2;
    sth = uicontrol(panel,'Style','edit',...
      'String','',...
      'Fontsize', fontsize,...
      'BackgroundColor', 'w',...
      'Units', 'normalized', ...
      'HorizontalAlignment', 'left',...
      'Position',[Cleft Cbottom Cwidth Cheight],...
      'Tag', sprintf('c%02dValue', k));
    set(sth, 'Callback', {'repogui.buildquery', mainfig});
    if ~isempty(conds{k,3})
      set(sth, 'String', conds{k,3});
    end

    % Add compound statement
    if k<Nc
      Cleft  = Cleft + Cwidth + STEP;
      Cwidth = 0.1;
      pmh = uicontrol(panel,'Style','popupmenu',...
        'String', compounds,...
        'Fontsize', fontsize,...
        'Units', 'normalized', ...
        'BackgroundColor', Gproperties.Gcol{3},...
        'Value',1,'Position',[Cleft Cbottom Cwidth Cheight],...
        'Tag', sprintf('c%02dCompound', k));
      set(pmh, 'Callback', {'repogui.buildquery', mainfig});
      if ~isempty(conds{k,4})
        set(pmh, 'Value', conds{k,4});
      end
    end

  end

  %--------------------------------------------------------------------------
  %-- add +/- buttons
  k = Nc+1;
  Bbottom = pos(2) - pos(4)  - pmarg - (k-1)*(Cheight+pmarg);
  Bleft   = pos(1);
  Bheight = ALLHEIGHT;
  Bwidth  = 0.03;
  pbh = uicontrol(panel,'Style','pushbutton','Fontsize', fontsize, 'String','+',...
    'Units', 'normalized', ...
    'Position',[Bleft Bbottom Bwidth Bheight ],...
    'Tag', 'condPlusBtn');
  set(pbh, 'Callback', {'repogui.cb_condPlusBtn', pbh, panel, mainfig});
  Bleft   = pos(1) + Bwidth + pmarg;
  pbh = uicontrol(panel,'Style','pushbutton','Fontsize', fontsize, 'String','-',...
    'Units', 'normalized', ...
    'Position',[Bleft Bbottom Bwidth Bheight ],...
    'Tag', 'condSubtractBtn');
  set(pbh, 'Callback', {'repogui.cb_condSubtractBtn', pbh, panel, mainfig});


  %--------------------------------------------------------------------------
  %-- order by
  k = Nc + 2;
  Sleft   = pos(1);
  Sheight = ALLHEIGHT;
  Swidth  = 0.07;
  Sbottom = pos(2) - pos(4)  - pmarg -(k-1)*(Cheight+pmarg);
  sth = uicontrol(panel,'Style','text',...
    'String', 'Order by',...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'HorizontalAlignment', 'left',...
    'Position',[Sleft Sbottom Swidth Sheight]);
  % Gproperties.Gcol
  % Field Dropdown
  Sleft = pos(1) + pmarg + Swidth;
  Swidth = 0.2;
  pmh = uicontrol(panel,'Style','popupmenu',...
    'String', fields,...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'Value',1,'Position',[Sleft Sbottom Swidth Sheight],...
    'Tag', 'fieldOrderBy');
  set(pmh, 'Callback', {'repogui.buildquery', mainfig});
  if ~isempty(forder)
    set(pmh, 'Value', forder);
  end

  % Desc Dropdown
  Sleft = Sleft + pmarg + Swidth;
  Swidth = 0.2;
  pmh = uicontrol(panel,'Style','popupmenu',...
    'String', {'DESC', 'ASC'},...
    'Fontsize', fontsize,...
    'Units', 'normalized', ...
    'BackgroundColor', Gproperties.Gcol{3},...
    'Value',1,'Position',[Sleft Sbottom Swidth Sheight],...
    'Tag', 'sortDir');
  set(pmh, 'Callback', {'repogui.buildquery', mainfig});
  if ~isempty(sort)
    set(pmh, 'Value', sort);
  end


  %--------------------------------------------------------------------------
  %-- Run query

  Swidth  = 0.2;
  Sleft   = 1-Swidth-pmarg;
  Sheight = ALLHEIGHT;
  Sbottom = pmarg;
  uicontrol(panel,'Style','pushbutton','String','Execute query',...
    'Units', 'normalized', ...
    'Position',[Sleft Sbottom Swidth Sheight ],...
    'Tag', 'refreshListBtn', ...
    'Callback', {'repogui.cb_executeQuery', mainfig});

  % Build query
  repogui.buildquery(mainfig)

  drawnow

end
