using UdonSharp;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.SDK3.Persistence;

[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class GlobalToggleFlags : UdonSharpBehaviour
{

    private const string PersistencePhysicalBettingKey = "WorldSettings/PhysicalChipBetting";
    private const string PersistenceBendCardsKey = "WorldSettings/BendCards";
    private const string PersistenceAnimatedChipContainersKey = "WorldSettings/AnimatedChipContainers";

    public Toggle bendCardsToggle;
    public Toggle[] allBendCardsToggles;


    public Toggle physicalBettingToggle;
    public Toggle[] allPhysicalBettingToggles;
    public HoldemPlayer[] holdemPlayers;
    public GrabbableHand[] hands;
    public Toggle animatedChipContainersToggle;
    public Toggle[] allAnimatedChipContainersToggles;

    private bool isReady;

void Start()
    {
        UpdateBendCards();
        UpdatePhysicalBetting();
        if (animatedChipContainersToggle != null)
            animatedChipContainersToggle.SetIsOnWithoutNotify(false);
        UpdateAnimatedChipContainers();
    }

public override void OnPlayerRestored(VRCPlayerApi player)
    {
        if (isReady || player == null || !player.isLocal) return;
        isReady = true;

        if (bendCardsToggle != null && PlayerData.HasKey(player, PersistenceBendCardsKey))
            bendCardsToggle.SetIsOnWithoutNotify(PlayerData.GetInt(player, PersistenceBendCardsKey) != 0);

        if (physicalBettingToggle != null)
        {
            bool enabled = PlayerData.HasKey(player, PersistencePhysicalBettingKey)
                && PlayerData.GetInt(player, PersistencePhysicalBettingKey) != 0;
            physicalBettingToggle.SetIsOnWithoutNotify(enabled);
        }

        if (animatedChipContainersToggle != null)
        {
            bool enabled = PlayerData.HasKey(player, PersistenceAnimatedChipContainersKey)
                && PlayerData.GetInt(player, PersistenceAnimatedChipContainersKey) != 0;
            animatedChipContainersToggle.SetIsOnWithoutNotify(enabled);
        }

        UpdateBendCards();
        UpdatePhysicalBetting();
        UpdateAnimatedChipContainers();
    }

    public void UpdateBendCards()
    {
        if (bendCardsToggle == null)
            return;

        bool enabled = bendCardsToggle.isOn;
        if (isReady)
        {
            PlayerData.SetInt(PersistenceBendCardsKey, enabled ? 1 : 0);
        }
        if (allBendCardsToggles != null)
        {
            foreach (Toggle targetToggle in allBendCardsToggles)
            {
                if (targetToggle != null)
                    targetToggle.SetIsOnWithoutNotify(enabled);
            }
        }

        if (hands == null)
            return;

        foreach (GrabbableHand hand in hands)
        {
            if (hand != null)
                hand.SetCardFoldingEnabled(enabled);
        }
    }

public void UpdatePhysicalBetting()
    {
        if (physicalBettingToggle == null) return;

        bool enabled = physicalBettingToggle.isOn;
        if (isReady) PlayerData.SetInt(PersistencePhysicalBettingKey, enabled ? 1 : 0);

        if (allPhysicalBettingToggles != null)
        {
            foreach (Toggle targetToggle in allPhysicalBettingToggles)
            {
                if (targetToggle != null) targetToggle.SetIsOnWithoutNotify(enabled);
            }
        }

        if (holdemPlayers == null) return;
        foreach (HoldemPlayer holdemPlayer in holdemPlayers)
        {
            if (holdemPlayer != null) holdemPlayer.SetPhysicalBettingEnabled(enabled);
        }
    }


    public void UpdateAnimatedChipContainers()
    {
        if (animatedChipContainersToggle == null) return;

        bool enabled = animatedChipContainersToggle.isOn;
        if (isReady) PlayerData.SetInt(PersistenceAnimatedChipContainersKey, enabled ? 1 : 0);
        if (allAnimatedChipContainersToggles != null)
        {
            foreach (Toggle targetToggle in allAnimatedChipContainersToggles)
            {
                if (targetToggle != null) targetToggle.SetIsOnWithoutNotify(enabled);
            }
        }
        // Scene instances already wire every HoldemPlayer for the physical-betting toggle.
        // Seat zero provides one coordinator reference per table without extra prefab setup.
        if (holdemPlayers == null) return;
        foreach (HoldemPlayer holdemPlayer in holdemPlayers)
        {
            if (holdemPlayer != null && holdemPlayer.seatIdx == 0 && holdemPlayer.game != null)
                holdemPlayer.game.SetAnimatedChipContainersEnabled(enabled);
        }
    }

}
