
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using TMPro;
using VRC.SDK3.Components;

[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class PlayerPodium : UdonSharpBehaviour
{
	public int PlayerNumber = 0;

	public GameManager GameManager;

	[UdonSynced, FieldChangeCallback(nameof(PlayerName))] private string _PlayerName = "Player";
	[UdonSynced] public int PlayerID = -1;
	[UdonSynced, FieldChangeCallback(nameof(PlayerScore))] private int _PlayerScore = 50;

	public TextMeshProUGUI Nameplate;
	public TextMeshProUGUI Scorecard;

	void Start()
	{
		PlayerName = "Player " + PlayerNumber;
	}


	public void SetPlayerName()
	{
		if (PlayerID != -1) { return; }
		Networking.SetOwner(Networking.LocalPlayer, gameObject);
		PlayerName = Networking.LocalPlayer.displayName;
		PlayerID = Networking.LocalPlayer.playerId;
	}

	public void BuzzIn()
	{
		GameManager.PlayerBuzzedIn(PlayerNumber);
	}

	public void DecreaseScoreBy5()
	{
		PlayerScore -= 5;
	}

	public void IncreaseScoreBy5()
	{
		PlayerScore += 5;
	}

	public void IncreaseScoreBy10()
	{
		PlayerScore += 10;
	}


	public string PlayerName
	{
		set
		{
			_PlayerName = value;
			Nameplate.text = value;

			RequestSerialization();
		}
		get => _PlayerName;
	}

	public int PlayerScore
	{
		set
		{
			_PlayerScore = value;
			Scorecard.text = PlayerScore.ToString();
			RequestSerialization();
		}
		get => _PlayerScore;
	}
}
