{ Migrate EM / iView status to label
  Creates a label from the the XMP field expressionmedia:Status or mediapro:Status
  The new labels are written under the label category 'MediaPro Status'

  Author: Andrew Certain
  Version history:
  1.0 2016-01-24 by Andrew Certain
      - initial version

  Author: 
  1.2 2020-07-23 by H van Zwietering
    - The category will be created if it doesn't exist yet
  1.21 2020-08-04 by H van Zwietering
    - Added dedicated progress bar

}

function DetermineParentContainer: String;
var
  ACategory: TCatalogPropCategory;
  AProp: TCatalogItemProp;

begin
//  result := PublicCatalog.Events.GUID;

  result := '';
  // alternative 1; find a category by name that should contain the status structure
  ACategory := TCatalogPropCategory.Create (nil);
  try
    if not PublicCatalog.EnumCategoryNamed ('MediaPro Status', ACategory) then        // enable this line instead of the next on to find a category by name
    begin
      ACategory.GUID := NewGUID;
      ACategory.CategoryName := 'MediaPro Status';
      if not PublicCatalog.UpdateCategory(ACategory) then
        exit;
    end;
    result := ACategory.GUID;
  finally
    ACategory.Free;
  end;
end;

var
  i: Integer;
  AMain: TCatalogItem;
  ACatItem: TCatalogItem;
  AXmp: TXMP;
  AProp: TCatalogItemProp;
  AParam: TMacroParam;
  AParentGUID: String;
  AProgress: TxomProgress;
begin
  AProgress := TxomProgress.Create(nil);
  try
    AProgress.Cancel := False;
    AProgress.Caption := 'Copy Media Pro Status To Tag';
    AProgress.Max := Selected.Count;
    AProgress.Pos := 0;

    AProgress.Show;

    AParentGUID := DetermineParentContainer;
    if AParentGUID = '' then
    begin
      Say('Parent category not found or could not be created');
      exit;
    end;

    AMain := TCatalogItem.Create(nil);
    ACatItem := TCatalogItem.Create (nil);
    AXmp := TXMP.Create (nil);
    try
      for i := 0 to Selected.Count - 1 do
      begin
        AProgress.Pos := i + 1;
        AProgress.ProgressText := Selected.Items[i].FileNameOnly;

        // find the image item in the catalog (and only process found items
        if PublicCatalog.FindImageCombined (Selected.Items[i], ACatItem, true, vptNone) then
        begin
          //Say('Found image');
          PublicCatalog.LoadXMPForItem (ACatItem, AXmp, Options.CachedXMP);

          // find the Event info
          AParam := AXmp.FindDesignProperty ('http://ns.microsoft.com/expressionmedia/1.0/', 'expressionmedia:Status');
          if AParam = nil then
            //Say('Looking again');
            AParam := AXmp.FindDesignProperty ('http://ns.iview-multimedia.com/mediapro/1.0/', 'mediapro:Status');
          if AParam <> nil then
          begin
            //Say('Found something');
            if AParam.Content <> '' then
            begin
              AProp := TCatalogItemProp.Create(nil);
              try
                //Say('Found property: ' + AParam.Content);
                // is there an existing label with this name?
                if not PublicCatalog.EnumPropName(AParentGUID, AParam.Content, AProp, false) then
                begin
                  // not available yet, so create one
                  AProp.GUID         := NewGUID;                    // generate a new GUID
                  AProp.PropName     := AParam.Content;
                  PublicCatalog.StorePropToDatabase (AProp, AParentGUID, False);
                end;

                // Assign the label to the catalog item
                PublicCatalog.AddPropToItem (ACatItem, AProp, False, False);
              finally
                AProp.Free;
              end;
            end;
          end;
        end;
      end;
    finally
      AXmp.Free;
      ACatItem.Free;
      AMain.Free;
    end;

    AProgress.Hide;

    if AProgress.Cancel then
      Say ('Cancelled')
    else
      Say ('Status is converted to catalog labels in the MediaPro Status top level Category');
  finally
    AProgress.Free;
  end;
end;

