using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Doozy.Engine.UI;
using Doozy.Engine;
using TMPro;
using MEC;

public class SpaceshipManager : MonoBehaviour
{
    public static SpaceshipManager Instance;

    [Header("Spaceship Parts")]
    public List<ParticleSystem> sparkList;
    public List<GameObject> spaceshipParts;

    [Header("Spaceship Status")]
    public static E_SpaceshipRepairStatus repairStatus;

    [Header("Random Messages")]
    private CoroutineHandle randomMessageHandle;
    public List<RandomMessageItem> randomMessageList;


    // Popup+
    private int randomIndex;
    private string _popupName = "RandomMessagePopup";


    private bool healAllowed = true;

    private void Start()
    {
        Instance = this;
        randomMessageHandle = Timing.RunCoroutine(C_RandomMessageTimer());
    }

    public void HealPlayer()
    {
        if(healAllowed)
        {
            switch (repairStatus)
            {
                case E_SpaceshipRepairStatus.tierOne:

                    PlayerStatsManager.Instance.ModifyOxigen(30);
                    PlayerStatsManager.Instance.ModifyEnergy(30);

                    break;

                case E_SpaceshipRepairStatus.tierTwo:

                    PlayerStatsManager.Instance.ModifyOxigen(50);
                    PlayerStatsManager.Instance.ModifyEnergy(50);

                    break;

                case E_SpaceshipRepairStatus.tierThree:

                    PlayerStatsManager.Instance.ModifyOxigen(70);
                    PlayerStatsManager.Instance.ModifyEnergy(70);

                    break;

                case E_SpaceshipRepairStatus.tierFour:

                    PlayerStatsManager.Instance.ModifyOxigen(90);
                    PlayerStatsManager.Instance.ModifyEnergy(90);

                    break;
            }

            healAllowed = false;

            Timing.CallDelayed(10f, () => healAllowed = true);
        }
    }

    public void RepairSpaceshipPart(int p_index)
    {
        Destroy(PlayerMovement.Instance.spaceshipPartObject.gameObject);
        PlayerMovement.Instance.spaceshipPartObject = null;
        sparkList[p_index].Stop();
        spaceshipParts[p_index].SetActive(true);

        if (p_index == 0)
            StoryManager.spaceshipFirstRepair = true;
        else if (p_index == 1)
            StoryManager.spaceshipSecondRepair = true;
        else if (p_index == 2)
            StoryManager.spaceshipThirdRepair = true;
        else if (p_index == 3)
            StoryManager.spaceshipFourthRepair = true;
        else if (p_index == 4)
            StoryManager.spaceshipFifthRepair = true;

		print(StoryManager.spaceshipFirstRepair);
    }



    private UIPopup _popup;
    public static bool randomMesssageBlocked = false;

    private IEnumerator<float> C_RandomMessageTimer()
    { 
        while(true)
        {
            if (randomMesssageBlocked)
                yield return Timing.WaitForSeconds(1f);
            else
            {
                yield return Timing.WaitForSeconds(Random.Range(8f, 18f));
                SendRandomMessage();
            }
        }
    }

    public void SendRandomMessage()
    {
        _popup = UIPopupManager.GetPopup(_popupName);

        randomIndex = Random.Range(0, randomMessageList.Count);

        if(!randomMessageList[randomIndex].messageUsed)
        {
            _popup.Data.SetLabelsTexts(randomMessageList[randomIndex].messageDetail);
            randomMessageList[randomIndex].messageUsed = true;

            UIPopupManager.ShowPopup(_popup, _popup.AddedToQueue, false);
        }
    }
}

[System.Serializable]
public class RandomMessageItem
{
    [TextArea(2,4)]
    public string messageDetail;
    public bool messageUsed = false;
}

