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

public class PlayerStatsManager : MonoBehaviour
{
    public static PlayerStatsManager Instance;

    [Header("Stats Values")]
    public float oxigenCurrentValue;
    private const float oxigenMaxValue = 100;
    public float energyCurrentValue;
    private const float energyMaxValue = 100;

    [Header("UI References")]
    public Transform oxigenBarTransform;
    public Transform energyBarTransform;

    private CoroutineHandle oxigenTimerHandle;

    private void Start()
    {
        Instance = this;

        oxigenCurrentValue = 200f;
        energyCurrentValue = 200f;

        oxigenTimerHandle = Timing.RunCoroutine(C_OxigenTimer());
    }

    public void ModifyOxigen(int p_value)
    {
        oxigenCurrentValue += p_value;

        if (oxigenCurrentValue > oxigenMaxValue)
            oxigenCurrentValue = oxigenMaxValue;

        if (oxigenCurrentValue <= 0) 
        {
            oxigenCurrentValue = 0;
            // DIE CODe
        }

            oxigenBarTransform.localScale = new Vector3(1f, (oxigenCurrentValue / oxigenMaxValue), 1f);
    }

    public void ModifyEnergy(int p_value)
    {
        energyCurrentValue += p_value;

        if (energyCurrentValue > energyMaxValue)
            energyCurrentValue = energyMaxValue;

        if (energyCurrentValue <= 0) 
        {
            energyCurrentValue = 0;
            // DIE CODE
        }

        energyBarTransform.localScale = new Vector3(1f, (energyCurrentValue / energyMaxValue), 1f);
    }


    private IEnumerator<float> C_OxigenTimer()
    {
        while(oxigenCurrentValue > 0)
        {
            ModifyOxigen(-1);
            yield return Timing.WaitForSeconds(1f);
        }

        // Die code
        yield break;
    }

    #region <<<<<-----     COLLISIONS     ----->>>>>

    private PlanetManager _planetManager;
    private ItemManager _itemsManager;
    // ???
    private int repairIndex = 0;
    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("SpaceshipCrash"))
        {
            StoryManager.Instance.canContinueStory = true;

            StoryManager.spaceshipFirstEncounter = true;
            Destroy(other.gameObject);
        }

        if (other.CompareTag("Planet"))
        {
            StoryManager.Instance.canContinueStory = false;

            _planetManager = other.gameObject.GetComponent<PlanetManager>();
            _planetManager.GetResource();
            StoryManager.planetResourceLoaded = true;
        }

        if (other.CompareTag("Items"))
        {
            StoryManager.Instance.canContinueStory = false;

            _itemsManager = other.GetComponent<ItemManager>();

            switch (_itemsManager.itemType)
            {
                case E_ItemType.Ball:

                    StoryManager.Instance.GetItemDetail(0);

                    break;

                case E_ItemType.Clock:

                    StoryManager.Instance.GetItemDetail(1);

                    break;

                case E_ItemType.Can:

                    StoryManager.Instance.GetItemDetail(2);

                    break;


                case E_ItemType.Portrait:

                    StoryManager.Instance.GetItemDetail(3);

                    break;

                case E_ItemType.Laptop:

                    StoryManager.Instance.GetItemDetail(4);

                    break;

                case E_ItemType.Coach:

                    StoryManager.Instance.GetItemDetail(5);

                    break;

                case E_ItemType.Luggage:

                    StoryManager.Instance.GetItemDetail(6);
                    StoryManager.luggagePicked = true;
                    StoryManager.linearStoryIndex++;

                    break;
            }

            Destroy(other.gameObject, 1f);
        }


        if(other.CompareTag("SpaceshipPart"))
        {
            StoryManager.Instance.canContinueStory = true;
            SpaceshipPartManager spaceshipPartManager = other.GetComponent<SpaceshipPartManager>();
            repairIndex = spaceshipPartManager.spaceshipPartIndex;
            PlayerMovement.Instance.spaceshipPartObject = other.transform;
            PlayerMovement.arrowTarget = PlayerMovement.Instance.spaceshipTranform;
        }

        if(other.CompareTag("Spaceship"))
        {
            StoryManager.Instance.canContinueStory = true;

            SpaceshipManager.Instance.HealPlayer();

            if (PlayerMovement.Instance.spaceshipPartObject != null)
                SpaceshipManager.Instance.RepairSpaceshipPart(repairIndex);

            
        }
    }

    private float countDownTimer = 0f;
    private void OnTriggerStay(Collider other)
    {
        if (other.CompareTag("RechargePlanet"))
        {
            if (!oxigenTimerHandle.IsAliveAndPaused)
                Timing.PauseCoroutines(oxigenTimerHandle);

            countDownTimer += Time.deltaTime;

            if (countDownTimer >= 1f)
            {
                ModifyOxigen(2);
                ModifyEnergy(2);
                countDownTimer = 0f;
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("RechargePlanet"))
        {
            Timing.ResumeCoroutines(oxigenTimerHandle);
        }


        _planetManager = null;
        _itemsManager = null;
    }
}


#endregion