using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public abstract class MapParameterController : MonoBehaviour
{
    [Header("Refs")]
    public MeshRenderer validityLight;
    public PowerModule powerModule;
    public TMPro.TextMeshProUGUI manualButtonLabel;

    [Space]
    public string shaderParameterName;
    
    float autoUpdateTimer;
    bool manualControl = false;

    const float autoUpdateDelay = 0.1f;

    public abstract void Init();
    public abstract bool IsCorrect();

    protected virtual void Awake()
    {
        if(powerModule == null)
            powerModule = GetComponentInChildren<PowerModule>();
        else
            powerModule.Init(this);
    }

    void Start()
    {
        GameEventManager.OnJumpReady += OnJumpReady;
        GameEventManager.OnHyperspaceJump += OnHyperspaceJump;
        GameEventManager.OnJumpEnd += OnJumpEnd;
        autoUpdateTimer = Random.Range(4f, 4.6f);
    }

    protected virtual void Update()
    {
        if(powerModule.IsAuto()
            && !manualControl
            && !IsCorrect()
            && !StarsManager.Instance.IsJumping())
        {
            autoUpdateTimer -= Time.deltaTime;
            if(autoUpdateTimer < 0)
            {
                autoUpdateTimer = autoUpdateDelay + Random.Range(0.3f, 0.6f);
                AutoUpdate();
            }
        }
        //else
        //{
        //    Debug.Log("No auto update because powerModule auto: " + powerModule.IsAuto() + " and manualControl " + manualControl + " and IsCorrect " + IsCorrect() + " and IsJumping: " + StarsManager.Instance.IsJumping());
        //}
    }

    void OnJumpReady()
    {
        ActivateManualControl(false);
    }

    void OnHyperspaceJump()
    {
    }

    void OnJumpEnd()
    {
        if(!powerModule.IsAuto() && StoryManager.Instance.RemainingJumpsAmount > 0)
        {
            ActivateManualControl(true);
        }
    }

    public void AssignRandomValue(MeshRenderer resultQuad)
    {
        autoUpdateTimer = autoUpdateDelay;
        SelectRandomValue(resultQuad);
        CheckNewValue();
    }

    protected abstract void SelectRandomValue(MeshRenderer resultQuad);

    public bool LoosePower(float ratio)
    {
        return powerModule.LoosePower(ratio);
    }
    public virtual void ActivateManualControl(bool manual)
    {
        manualControl = manual || !powerModule.IsAuto();
        manualButtonLabel.text = manualControl || !powerModule.IsAuto() ? "MAN" : "AUTO";
        manualButtonLabel.color = manualControl || !powerModule.IsAuto() ? Color.red : Color.green;
    }
    //public void OnManualControlButton()
    //{
    //    ActivateManualControl(!manualControl);
    //}
    public void DisableValidityFeedback()
    {
        validityLight.material = GameUtils.Instance.lightOffMaterial;
    }

    protected virtual void CheckNewValue()
    {
        if(powerModule.HasValidityFeedback())
        {
            if(IsCorrect())
            {
                validityLight.material = GameUtils.Instance.lightGreenMaterial;
            }
            else
            {
                validityLight.material = GameUtils.Instance.lightRedMaterial;
            }
        }
        MapParametersManager.Instance.CheckParameters();
    }

    protected abstract void AutoUpdate();
}
