{
  this script will search for all catalog labels in the Places category
  that have no GEO details marked for it or without longitude defined.

  author: HB van Zwietering
  initial date: 2018-09-14

  1.0; 2018-09-14; initial version
}

var
  AProps: TCatalogItemProps;
  AProp: TCatalogItemProp;
  i: Integer;
  AList: TTntStringList;
  ASl: TTntStringList;
  AResult: Integer;
  ACheck: Boolean;
  ADoAsk: Boolean;
  AHit: Integer;
begin
  Progress.Cancel := False;

  AProps := TCatalogItemProps.Create(TCatalogItemProp, '');
  AList := TTntStringList.Create;

  Progress.Caption := 'Finding catalog labels in database...';
  Progress.Show;
  Application.ProcessMessages;

  PublicCatalog.EnumPropsForParent(PlacesGUID, AProps, True, False);

  Progress.Max := AProps.Count;
  Progress.Pos := 0;

  for i := 0 to AProps.Count - 1 do
  begin
    Progress.Pos := i + 1;
    Progress.ProgressText := IntToStr(MulDiv (i, 100, AProps.Count)) + '%; Found ' + IntToStr(AList.Count) + ' empty labels';

    AProp := AProps.Items[i];
    if AProp.PropValue = AutoPropValue then    // skip auto-props
      Continue;

    if (not AProp.GPSGeoTag) or   // not marked for GEO tagging?
       (AProp.GPSLon = cNoGPSLatLon)  // no Longitude?
    then
    begin
      AList.AddObject (PublicCatalog.PathNameForProp(AProp, '::', True), AProp);
    end;

    // for testing
    //if AList.Count = 100 then
    //  break;

    if Progress.Cancel then
      break;
  end;

  Progress.Hide;

  ASaved := False;
  ASl := TTntStringList.Create;
  ASl.Add('Save results to file');
  ASl.Add('Show results on-screen');
  ASl.Add('Delete all ' + IntToStr(AList.Count) + ' place catalog labels');
  ASl.Add('Cancel');

  ADoAsk := AList.Count > 0;
  while ADoAsk do
  begin
    ACheck := False;
    AResult := AskCustom ('What would you like to do?', 'There are ' + IntToStr(AList.Count) + ' place catalog labels without GEO details found.', '', '', ACheck, ASl);
    case AResult of
      0:      // save to file
      begin
        SaveDialog.FileName := WideIncludeTrailingBackslash(WindowsDesktopDir) + 'place labels without GEO.txt';
        if SaveDialog.Execute then
        begin
          AList.SaveToFile (SaveDialog.FileName);
          Say ('The results are saved to file ' + SaveDialog.FileName);
          ADoAsk := False;
        end;
      end;
      1:      // show on screen
      begin
        Say2 ('Place catalog labels without GEO details', AList.Text);
        ADoAsk := True;
      end;
      2:      // delete empty labels
      begin
        if Ask ('Are you sure you want to permanently delete these ' + IntToStr(AList.Count) + ' place catalog labels without GEO details?') then
        begin
          Progress.Cancel := False;
          Progress.Max := AList.Count;
          Progress.Pos := 0;
          Progress.Show;

          AHit := 0;
          for i := 0 to AList.Count - 1 do
          begin
            Progress.Pos := i + i;
            Progress.ProgressText := IntToStr(MulDiv (i, 100, AList.Count)) + ' %';

            AProp := AList.Objects[i];

            //Say (AProp.PropName);
            PublicCatalog.DeleteProp (AProp);
            Inc(AHit);

            if Progress.Cancel then
              break;
          end;

          Progress.Hide;

          Say (IntToStr(AHit) + ' place catalog labels were deleted from the catalog');
          ADoAsk := not Progress.Cancel;
        end;
      end;
      3:      // cancel
      begin
        ADoAsk := False;
      end;
    end;
  end;

  AList.Free;
  AProps.Free;
end;

