using System;
using System.Collections.Generic;
using UnityEngine;
using Ubisoft.Hotel.Localisation;
namespace Ubisoft.Hotel.Package.Editor
{
///
/// Class responsible for storing the app name for a particular language id.
///
[Serializable]
public class AppName
{
[SerializeField]
private ELanguageId m_languageId;
[SerializeField]
private string m_name;
/// Short app name.It should be less than 16 characters long. iOS uses this value instead of m_name on some screens.
[SerializeField]
private string m_shortName;
public AppName(ELanguageId languageId, string name, string shortName)
{
LanguageId = languageId;
Name = name;
ShortName = shortName;
}
public bool Validate()
{
bool returnValue = true;
if (LanguageId == ELanguageId.None)
{
returnValue = false;
string validLanguageIds = HtSystemCollections.CollectionToString(LocalisationUtils.GetLanguageNames());
Debug.EditorLogError($"Invalid language id: {LanguageId}. The valid values are: {validLanguageIds}.");
}
if (string.IsNullOrEmpty(Name))
{
returnValue = false;
Debug.EditorLogError("It is not allowed to use an empty or null string as app name.");
}
return returnValue;
}
public ELanguageId LanguageId
{
get { return m_languageId; }
set { m_languageId = value; }
}
public string Name
{
get { return m_name; }
set { m_name = value; }
}
/// Short app name.It should be less than 16 characters long. iOS uses this value instead of Name on some screens.
public string ShortName
{
get { return m_shortName; }
set { m_shortName = value; }
}
public AppName Clone()
{
return new AppName(LanguageId, Name, ShortName);
}
}
public class AppNameProperty : PackageProperty
{
///
/// Create an AppNameProperty object.
///
/// Property name.
/// A AppNameProperty object to localise app name.
public static AppNameProperty CreateInstanceWithParams(string name)
{
AppNameProperty returnValue = CreateInstance();
returnValue.name = name;
return returnValue;
}
[SerializeField]
private List m_localisedAppNames;
internal List LocalisedAppNames
{
get { return m_localisedAppNames; }
set { m_localisedAppNames = value; }
}
internal AppName GetLocalisedAppNameByLanguageId(ELanguageId languageId)
{
AppName returnValue = null;
if (m_localisedAppNames != null)
{
int count = m_localisedAppNames.Count;
for (int i = 0; i < count && returnValue == null; ++i)
{
if (m_localisedAppNames[i].LanguageId == languageId)
{
returnValue = m_localisedAppNames[i];
}
}
}
return returnValue;
}
///
/// Add a new localised app name.
///
/// Language id for which the localised app name is passed in.
/// App name.
/// Short app name. It should be less than 16 characters long.
/// iOS uses this name instead of name on some screens.
/// Pass in a null or empty string if you don't care about this parameter. If you do so then name will be used everywhere.
///
public void AddLocaliseAppName(ELanguageId languageId, string name, string shortName = null)
{
if (m_localisedAppNames == null)
{
m_localisedAppNames = new List();
}
// If 'shortName' is not provided then we use 'name' as 'shortName'
if (string.IsNullOrEmpty(shortName))
{
shortName = name;
}
AppName appName = GetLocalisedAppNameByLanguageId(languageId);
if (appName == null)
{
appName = new AppName(languageId, name, shortName);
// Make sure that the app name is a valid one before adding it to the list of localised app names
if (appName.Validate())
{
m_localisedAppNames.Add(appName);
}
}
else
{
string msg = $"{Debug.FormatTextInUserContext(name)} can't be assigned as app name for language id {Debug.FormatTextInUserContext(languageId.ToString())}";
msg += $" because {Debug.FormatTextInUserContext(appName.Name)} has already defined as the app name for this language id.";
Debug.EditorLogError(msg);
}
}
public void RemoveLocalisedAppNameByLanguageId(ELanguageId languageId)
{
AppName appName = GetLocalisedAppNameByLanguageId(languageId);
if (appName != null)
{
_ = m_localisedAppNames.Remove(appName);
}
}
public override void Apply(BuildSuite buildSuite)
{
// Translate AppNameProperty into the corresponding properties responsible for implementing them into every supported platform
// iOS
PlatformBuildProperty_iOS iosProperty = PlatformBuildProperty_iOS.CreateInstanceWithParams($"{name}_iosProperty");
iosProperty.AddAppNameLocalisations(m_localisedAppNames);
buildSuite.OrderProperty(iosProperty);
// Android
PlatformBuildProperty_Android androidProperty = PlatformBuildProperty_Android.CreateInstanceWithParams($"{name}_androidProperty");
androidProperty.AddAppNameLocalisations(m_localisedAppNames);
buildSuite.OrderProperty(androidProperty);
}
}
}