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

[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class WelfareButtonComponent : WelfareListenerComponent
{
    public const string ButtonTriggerName = "ButtonPressed";
    private int ButtonTriggerID;
    
    [SerializeField]
    private Material LockedMaterial;
    [SerializeField]
    private Material AvailableMaterial;
    [SerializeField]
    private Material CooldownMaterial;

    [SerializeField]
    private TextMeshPro TextMeshComponent;

    private bool bCooldownUpdateTimerActive = false;
    
    [SerializeField]
    private WelfareComponent WelfareComponent = null;
    
    [SerializeField]
    private Animator AnimatorComponent = null;
    
    private void Start()
    {
        if (!WelfareComponent)
        {
            WelfareComponent = GetComponentInParent<WelfareComponent>();
        }

        if (!WelfareComponent)
        {
            Debug.LogError("WelfareButtonComponent could not find a WelfareComponent in its parent hierarchy! Deactivating.");
            gameObject.SetActive(false);
            return;
        }
        
        if (!AnimatorComponent)
        {
            AnimatorComponent = GetComponentInParent<Animator>();
        }

        if (AnimatorComponent)
        {
            ButtonTriggerID = Animator.StringToHash(ButtonTriggerName);
            AnimatorComponent.Play(0);
        }
        
        OnWelfareStatusChanged(WelfareComponent, WelfareComponent.GetWelfareStatus());
    }

    public override void Interact()
    {
        if (!WelfareComponent)
        {
            return;
        }

        WelfareComponent.RedeemWelfare(Networking.LocalPlayer);

        if (AnimatorComponent)
        {
            AnimatorComponent.SetTrigger(ButtonTriggerID);
        }
    }

    Material GetMaterialForStatus(EWelfareStatus Status)
    {
        switch (Status)
        {
            case EWelfareStatus.Locked:
                return LockedMaterial;
            case EWelfareStatus.Available:
                return AvailableMaterial;
            case EWelfareStatus.Cooldown:
                return CooldownMaterial;
        }
        
        return LockedMaterial;
    }

    public override void OnWelfareStatusChanged(WelfareComponent InWelfareComponent, EWelfareStatus NewStatus)
    {
        gameObject.GetComponent<MeshRenderer>().material = GetMaterialForStatus(NewStatus);

        if (TextMeshComponent)
        {
            TextMeshComponent.enabled = NewStatus == EWelfareStatus.Cooldown;
        }
        
        DisableInteractive = NewStatus != EWelfareStatus.Available;

        if (NewStatus == EWelfareStatus.Cooldown)
        {
            UpdateCooldownText();
            StartUpdateCooldownText();
        }
    }
    
    protected void StartUpdateCooldownText()
    {
        if (!WelfareComponent || bCooldownUpdateTimerActive)
        {
            return;
        }
        
        bCooldownUpdateTimerActive = true;
        SendCustomEventDelayedSeconds(nameof(UpdateCooldownText), 1.0f);
    }

    //Always call StartCheckWelfareCooldownTimer instead of this directly to make sure duplicate timers are not created.
    public void UpdateCooldownText()
    {
        bCooldownUpdateTimerActive = false;
        if (!WelfareComponent || WelfareComponent.GetWelfareStatus() != EWelfareStatus.Cooldown)
        {
            return;
        }
        
        long WelfareTimeRemaining = WelfareComponent.WelfareCooldownTimeRemaining(Networking.LocalPlayer, WelfareComponent.GetCooldownDuration());

        if (TextMeshComponent)
        {
            TextMeshComponent.text = "<mspace=0.5em>"+WelfareComponent.TimeToString(WelfareTimeRemaining);
        }

        StartUpdateCooldownText();
    }
}
