//
//  NameString.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace NosSmooth.PacketSerializer.Abstractions.Common;
/// 
/// Represents name in the game replacing "^" for " ".
/// 
public class NameString
{
    /// 
    /// Gets the character used to separate words.
    /// 
    public static char WordSeparator => '^';
    /// 
    /// Creates  instance from the given name retrieved from a packet.
    /// 
    /// The name from the packet.
    /// A name string instance.
    public static NameString FromPacket(string packetName)
    {
        return new NameString(packetName, true);
    }
    /// 
    /// Creates  instance from the given name retrieved from a packet.
    /// 
    /// The name from the packet.
    /// A name string instance.
    public static NameString FromString(string packetName)
    {
        return new NameString(packetName, true);
    }
    private NameString(string name, bool packet)
    {
        if (packet)
        {
            PacketName = name;
            Name = name.Replace(WordSeparator, ' ');
        }
        else
        {
            Name = name;
            PacketName = name.Replace(' ', WordSeparator);
        }
    }
    /// 
    /// The real name.
    /// 
    public string Name { get; }
    /// 
    /// The original name in the packet.
    /// 
    public string PacketName { get; }
    /// 
    public override string ToString()
    {
        return Name;
    }
    /// 
    /// Converts name string to regular string.
    /// Returns the real name.
    /// 
    /// The name string to convert.
    /// The real name.
    public static implicit operator string(NameString nameString)
    {
        return nameString.Name;
    }
    /// 
    /// Converts regular string to name string.
    /// 
    /// The string to convert.
    /// The name string.
    public static implicit operator NameString(string name)
    {
        return FromString(name);
    }
    /// 
    public override bool Equals(object? obj)
    {
        if (!(obj is NameString nameString))
        {
            return false;
        }
        return Name.Equals(nameString.Name);
    }
    /// 
    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}