using Newtonsoft.Json;
using System;
using System.Text.RegularExpressions;
using UnityEngine;

[Serializable]
[JsonObject]
public class Player
{
    public string name;
    public string id;
    public double creationDate;

    [JsonConstructor]
    public Player(string _name, string _id, double _creationDate)
    {
        name = _name;
        id = _id;
        creationDate = _creationDate;
    }

    public Player(string _name)
    {
        id = Guid.NewGuid().ToString();
        creationDate = DateTime.Now.ToOADate();
        SetName(_name);
    }

    /// <summary>
    /// Will sanitize the text and set it for the player
    /// </summary>
    public void SetName(string _name)
    {
        name = SanitizeString(_name);
    }

    private string SanitizeString(string input)
    {
        // Utilisez une expression régulière pour supprimer tout ce qui n'est pas une lettre ou un chiffre
        string sanitized = Regex.Replace(input, @"[^a-zA-Z0-9]", "");

        // Limitez la longueur à 16 caractères maximum
        if (sanitized.Length > 16)
        {
            sanitized = sanitized[..16];
        }

        return sanitized;
    }
}
