
using UdonSharp;
using VRC.SDKBase;
using VRC.SDK3.UdonNetworkCalling;
using TMPro;
using UnityEngine;

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

	public GameManager GameManager;

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

	[Header("Effects")]
	[SerializeField] private GameObject _BuzzedInEffect;

	[SerializeField] private TextMeshProUGUI _Nameplate;
	[SerializeField] private 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;
		RequestSerialization();
	}

	public int GetPlayerID() { return _PlayerID; }


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

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

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


	[NetworkCallable]
	public void EnableBuzzInEffect(bool Enable)
	{
		_BuzzedInEffect.SetActive(Enable);
	}


	public string PlayerName
	{
		set
		{
			_PlayerName = value;
			_Nameplate.text = value;
		}
		get => _PlayerName;
	}

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