// Copyright (C) Stichting Deltares 2023. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections;
using System.Collections.Generic;
namespace Deltares.DamEngine.Data.Standard
{
public class UniqueNameProvider
{
private static readonly List collections = new List();
static UniqueNameProvider() {}
public static void Clear()
{
collections.Clear();
}
public static void Register(params IList[] lists)
{
foreach (IList list in lists)
{
if (!collections.Contains(list))
{
collections.Add(list);
}
}
}
public static void ProvideUniqueName(IList collection, IName item, Func translator = null)
{
if (string.IsNullOrEmpty(item.Name))
{
Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0];
item.Name = translator == null ? type.Name : translator(type.Name);
}
int indexer;
if (NameContainsIndexer(item.Name, out indexer))
{
string newName = item.Name;
while (NameExists(collection, item, newName))
{
newName = newName.Replace(string.Format("({0})", indexer), string.Format("({0})", ++indexer));
}
item.Name = newName;
}
else
{
if (!string.IsNullOrWhiteSpace(item.Name) && !NameExists(collection, item, item.Name))
{
return;
}
string pattern = string.IsNullOrWhiteSpace(item.Name) ? "{0}" : item.Name + " ({0})";
var index = 1;
while (NameExists(collection, item, string.Format(pattern, index)))
{
++index;
}
item.Name = string.Format(pattern, index);
}
}
public static string ProvideNewUniqueName(IList collection, string item, Func translator = null)
{
if (string.IsNullOrEmpty(item))
{
Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0];
item = translator == null ? type.Name : translator(type.Name);
}
if (string.IsNullOrWhiteSpace(item) || NameExists(collection, item))
{
string format = string.IsNullOrWhiteSpace(item) ? "{0}" : item + " ({0})";
var num = 0;
for (item = string.Format(format, num); NameExists(collection, item); item = string.Format(format, num))
{
++num;
}
}
return item;
}
private static bool NameContainsIndexer(string originalName, out int indexer)
{
indexer = -1;
if (string.IsNullOrEmpty(originalName))
{
return false;
}
string str = originalName.TrimEnd();
int num1 = str.LastIndexOf("(", StringComparison.Ordinal);
int num2 = str.LastIndexOf(")", StringComparison.Ordinal);
if (num1 < 0 || num2 < 0 || num1 > num2)
{
return false;
}
int startIndex = num1 + 1;
return int.TryParse(str.Substring(startIndex, num2 - startIndex), out indexer);
}
private static bool NameExists(IList list, IName excludedItem, string newName)
{
foreach (IName name in list)
{
if (name != excludedItem && name.Name == newName)
{
return true;
}
}
return false;
}
private static bool NameExists(IList list, string excludedItem)
{
foreach (IName name in list)
{
if (name.Name == excludedItem)
{
return true;
}
}
return false;
}
}
}