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

public class StoryManager : MonoBehaviour
{
    [Header("Linear Story")]
    public List<StoryMessage> linearStoryList;
    public static int linearStoryIndex = 0;

    [Header("Main Story")]
    [TextArea(2,4)]
    public List<string> storyItemList;
    public static StoryManager Instance;

    [Header("Message Pannel")]
    public Image messageIcon;
    public TMP_Text messageLabel;
    [Space]
    public Sprite playerIcon;
    public Sprite spaceshipIcon;


    [Header("SpaceshipParts Transforms")]
    public Transform firstPartTransform;
    public Transform secondPartTransform;
    public Transform thirdPartTransform;
    public Transform fourthPartTransform;
    public Transform fifthPartTransform;


    [Header("Extra Transforms")]
    public Transform planetTransform;

    [Header("Props")]
    public GameObject spaceshipPartsGroup;
    public GameObject memoryItemsGroup;



    private void Start()
    {
        Instance = this;
        canContinueStory = true;
        Timing.RunCoroutine(C_InitialStoryLine().CancelWith(this.gameObject), Segment.EndOfFrame);
    }

    public void GetItemDetail(int p_index)
    {
        messageLabel.text = storyItemList[p_index];
        GameEventMessage.SendEvent("ShowMessagePanel");
    }


    public void ShowLinearStoryMessage()
    {
		SetLinearStoryMessage();
    }

    private void SetLinearStoryMessage()
    {
        if(linearStoryList[linearStoryIndex].character == E_CharacterType.Player)
            messageIcon.sprite = playerIcon;
        else
            messageIcon.sprite = spaceshipIcon;

        messageLabel.text = linearStoryList[linearStoryIndex].messageDetail;
        GameEventMessage.SendEvent("ShowMessagePanel");
		linearStoryIndex++;
		canContinueStory = false;
    }


    public bool canContinueStory;
    public void ContinueStoryToNextIndex()
    {
		canContinueStory = true;
	}


    // STORY TRACKING


    public static bool spaceshipFirstEncounter = false;

    public static bool planetResourceLoaded = false;
    public static bool luggagePicked = false;

    public static bool spaceshipFirstRepair = false;
    public static bool spaceshipSecondRepair = false;
    public static bool spaceshipThirdRepair = false;
    public static bool spaceshipFourthRepair = false;
    public static bool spaceshipFifthRepair = false;

   public static bool goToHome = false;

    public IEnumerator<float> C_InitialStoryLine()
    {
        yield return Timing.WaitForSeconds(10f);
        StoryManager.Instance.ShowLinearStoryMessage(); // ¿Dónde estoy?

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
        yield return Timing.WaitForSeconds(4f);
        StoryManager.Instance.ShowLinearStoryMessage(); // Choque

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
        yield return Timing.WaitForSeconds(4f);
        StoryManager.Instance.ShowLinearStoryMessage(); // Exotraje

        // Activa la flecha
        PlayerMovement.Instance.arrowTransform.gameObject.SetActive(true);

        
        while (!spaceshipFirstEncounter) yield return Timing.WaitForOneFrame;

        StoryManager.Instance.ShowLinearStoryMessage(); // Encuentro con la nave

        spaceshipPartsGroup.SetActive(true);
        memoryItemsGroup.SetActive(true);
        yield return Timing.WaitForSeconds(2f);
        SpaceshipManager.randomMesssageBlocked = false;

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(3f);
        StoryManager.Instance.ShowLinearStoryMessage(); // Primer mensaje nave

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(5f);
        StoryManager.Instance.ShowLinearStoryMessage(); // Alerta recursos

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(5f);
        StoryManager.Instance.ShowLinearStoryMessage(); // Planeta Recursos
        PlayerMovement.arrowTarget = planetTransform;



        while (!planetResourceLoaded) yield return Timing.WaitForOneFrame;

        StoryManager.Instance.ShowLinearStoryMessage(); // Nave: no preocupes recursos
        PlayerMovement.arrowTarget = firstPartTransform;



        while (!spaceshipFirstRepair) yield return Timing.WaitForOneFrame;

        StoryManager.Instance.ShowLinearStoryMessage(); // primer reparo nave
        SpaceshipManager.repairStatus = E_SpaceshipRepairStatus.tierTwo;
        PlayerMovement.arrowTarget = secondPartTransform;

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(6f);
        StoryManager.Instance.ShowLinearStoryMessage(); // La comunicación es importante


		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(4f);
        StoryManager.Instance.ShowLinearStoryMessage(); // La comunicación es importante

		
		// Regresando a la nave - segunda reparación
		while (!spaceshipSecondRepair)
            yield return Timing.WaitForOneFrame;


        StoryManager.Instance.ShowLinearStoryMessage(); // segundo reparo nave
        SpaceshipManager.repairStatus = E_SpaceshipRepairStatus.tierThree;
        PlayerMovement.arrowTarget = thirdPartTransform;


		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(4f);
        StoryManager.Instance.ShowLinearStoryMessage(); // La comunicación es importante

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(4f);
        StoryManager.Instance.ShowLinearStoryMessage(); // La comunicación es importante

		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(4f);
        StoryManager.Instance.ShowLinearStoryMessage(); // La comunicación es importante


		// Regresando a la nave - tercera reparación
		
		while (!spaceshipThirdRepair) yield return Timing.WaitForOneFrame;

        StoryManager.Instance.ShowLinearStoryMessage(); // tercer reparo nave
        SpaceshipManager.repairStatus = E_SpaceshipRepairStatus.tierFour;
        PlayerMovement.arrowTarget = fourthPartTransform;



		// Regresando a la nave - cuarta reparación

		while (!spaceshipFourthRepair) yield return Timing.WaitForOneFrame;

        StoryManager.Instance.ShowLinearStoryMessage(); // tercer reparo nave
        SpaceshipManager.repairStatus = E_SpaceshipRepairStatus.tierFour;
        PlayerMovement.arrowTarget = fifthPartTransform;

		

		
        SpaceshipManager.randomMesssageBlocked = true;


        // Regresando a la nave - reparación final

        

        while (!spaceshipFifthRepair) yield return Timing.WaitForOneFrame;

		yield return Timing.WaitForSeconds(2f);
		StoryManager.Instance.ShowLinearStoryMessage(); // Nave: no preocupes recursos


		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(6f);
		StoryManager.Instance.ShowLinearStoryMessage(); //

        GameEventMessage.SendEvent("PickYourPathView");



		while (!goToHome) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(2f);
		StoryManager.Instance.ShowLinearStoryMessage(); //


		while (!canContinueStory) yield return Timing.WaitForOneFrame;
		yield return Timing.WaitForSeconds(4f);
        GameEventMessage.SendEvent("CreditsView");
        
        yield break;
    }



    public void OnGoToHomeButton()
    {
        goToHome = true;
    }

}

[System.Serializable]
public class StoryListItem
{
    public GameObject storyDetailObject;
    public Image backgroundImage;
}


[System.Serializable]
public class StoryMessage
{
    public E_CharacterType character;
    [TextArea(2,4)]
    public string messageDetail;
}

