﻿using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

public class DunceCapInteract : UdonSharpBehaviour
{
    [Tooltip("Amount of chips it costs to remove the cap")]
    public int removalCost = 5000;
    
    [Header("Audio Feedback")]
    [Tooltip("Sound to play on successful removal")]
    public AudioPoolPlayer successSound;
    
    [Tooltip("Sound to play when removal fails")]
    public AudioPoolPlayer failSound;

    private DunceCapPool pool;
    private int poolIndex = -1;

    public void Initialize(DunceCapPool owningPool, int index)
    {
        pool = owningPool;
        poolIndex = index;
    }

    public VRCPlayerApi GetWearer()
    {
        if (pool == null || poolIndex < 0) return null;

        int playerId = pool.GetAssignedPlayerId(poolIndex);
        if (playerId < 0) return null;

        VRCPlayerApi player = VRCPlayerApi.GetPlayerById(playerId);
        if (player == null || !Utilities.IsValid(player)) return null;
        return player;
    }

    public override void Interact()
    {
        VRCPlayerApi localPlayer = Networking.LocalPlayer;
        if (localPlayer == null) return;

        // The person wearing this cap, resolved from the pool slot
        VRCPlayerApi targetPlayer = GetWearer();
        if (targetPlayer == null)
        {
            Debug.Log("[DunceCapInteract] Target player not found");
            if (failSound != null) failSound.Play();
            return;
        }

        // Get the target's balance component (to check if they have a dunce cap)
        PlayerBalanceComponent targetBalance = PlayerBalanceComponent.GetPlayerBalanceComponent(targetPlayer);
        if (targetBalance == null || !targetBalance.IsReady())
        {
            Debug.Log("[DunceCapInteract] Target balance component not ready");
            if (failSound != null) failSound.Play();
            return;
        }

        // Ensure the target actually has a Dunce Cap active
        if (!targetBalance.HasDunceCapSynced)
        {
            Debug.Log("[DunceCapInteract] Target does not have a Dunce Cap");
            if (failSound != null) failSound.Play();
            return;
        }

        // Get the local player's balance component (for payment)
        PlayerBalanceComponent localBalance = PlayerBalanceComponent.GetPlayerBalanceComponent(localPlayer);
        if (localBalance == null || !localBalance.IsReady())
        {
            Debug.Log("[DunceCapInteract] Local balance component not ready");
            if (failSound != null) failSound.Play();
            return;
        }

        // Check if local player has enough chips to pay for removal
        if (localBalance.GetBalance() >= removalCost)
        {
            // Deduct from local player (the one paying)
            localBalance.ModifyBalance(-removalCost);
            // Remove dunce cap from target (the one wearing it)
            targetBalance.RemoveDunceCap();
            
            if (successSound != null) successSound.Play();
            
            Debug.Log($"[DunceCapInteract] Paid {removalCost} chips to remove {targetPlayer.displayName}'s Dunce Cap.");
        }
        else
        {
            // Not enough chips to remove
            Debug.Log($"[DunceCapInteract] Insufficient funds to remove cap. Have: {localBalance.GetBalance()}, Need: {removalCost}");
            if (failSound != null) failSound.Play();
        }
    }
}