B0a0sti8_firstunityproject/Assets/Skripts/UnityMultiplayer/Lobby/EditPlayerName.cs
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class EditPlayerName : MonoBehaviour {


    public static EditPlayerName Instance { get; private set; }


    public event EventHandler OnNameChanged;


    [SerializeField] private TextMeshProUGUI playerNameText;


    private string playerName = "Enter Name ...";


    private void Awake() {
        Instance = this;
        string descriptionText = "This is your name for this session.No numbers or special signs.If no valid name is given, it will be randomized.";

        GetComponent<Button>().onClick.AddListener(() => {
            UI_InputWindow.Show_Static("Player Name", playerName, "abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVXYWZ .,-", 20, descriptionText,
            () => {
                // Cancel
            },
            (string newName) => {
                playerName = newName;

                playerNameText.text = playerName;

                OnNameChanged?.Invoke(this, EventArgs.Empty);
            });
        });

        playerNameText.text = playerName;
    }

    private void Start() {
        OnNameChanged += EditPlayerName_OnNameChanged;
    }

    private void EditPlayerName_OnNameChanged(object sender, EventArgs e) {
        LobbyManager.Instance.UpdatePlayerName(GetPlayerName());
    }

    public string GetPlayerName() {

        if (playerName== null || playerName == "Enter Name ..." )
        {
            playerName = "";
            const string glyphs = "abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVXYWZ";

            int charAmount = UnityEngine.Random.Range(7, 13); 
            for (int i = 0; i < charAmount; i++)
            {
                playerName += glyphs[UnityEngine.Random.Range(0, glyphs.Length)];
            }
        }
        return playerName;
    }


}