using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Localization;

public class QuestSystem : MonoBehaviour, IDataPersistence {
	public Quest[] quests;
	[SerializeField] private List<string> items;
	public bool _quest1;
	public bool _quest2;
	public bool _quest3;
	public TextMeshProUGUI questText;
	public Animator gatewayWallAnimator;
	public AudioSource gatewayAudio;
	[HideInInspector]
	public InventoryManager InvManager;
	private InsaneMode insaneMode;

	[System.Serializable]
	public class Quest
	{
		public LocalizedString title;
		public LocalizedString description;
		public LocalizedString hint;

		public Quest(string titleKey, string descKey, string hintKey)
		{
			title = new LocalizedString { TableReference = "TNW_Table", TableEntryReference = titleKey };
			description = new LocalizedString { TableReference = "TNW_Table", TableEntryReference = descKey };
			hint = new LocalizedString { TableReference = "TNW_Table", TableEntryReference = hintKey };
		}
	}
	// Use this for initialization
	private void Start()
    {
		questText = GetComponentInChildren<TextMeshProUGUI>();
		InvManager = FindObjectOfType(typeof(InventoryManager)) as InventoryManager;
		insaneMode = FindObjectOfType<InsaneMode>();
		items = new List<string>();

    }

	// Update is called once per frame
	void Update()
	{
		//QUEST 1
		if (!_quest1)
        {
			questText.SetText($"<b><u>{quests[0].title.GetLocalizedString()}</u></b>\n<size=90%>{quests[0].description.GetLocalizedString()}\n <size=80%><i>{quests[0].hint.GetLocalizedString()}</i>");
			//questText.SetText("<b><u>Investigation in Ashbury</u></b>\n<size=90%> Find A Weapon.\n <size=80%><i>*Search the house in the area.</i>");

			for (int i = 0; i < InvManager.MaxItems; i++)
			{
				if (InvManager.Slots[i].IsTaken == true) //Checking if there's an item in this slot.
				{
					if (InvManager.Slots[i].Item.GetComponent<Item>() != null)
					{
						Item ItemScript = InvManager.Slots[i].Item.GetComponent<Item>(); //Getting the item script from the items inside the bag.
																						 //ItemScript.Name = PlayerPrefs.GetString("Name" + i.ToString()); //Loading the item's name.
						if (ItemScript.Name == "Shotgun" || ItemScript.Name == "Colt 1911" || ItemScript.Name == "M1 Garand") //Checking if the type of the new item matches with another item already in the bag.
						{
							_quest1 = true;
						}
					}
				}
			}
        }

        //QUEST 2
        if (_quest1)
        {
			questText.SetText($"<b><u>{quests[1].title.GetLocalizedString()}</u></b>\n<size=90%>{quests[1].description.GetLocalizedString()}\n <size=80%><i>{quests[1].hint.GetLocalizedString()}</i>");
			//questText.SetText("<b><u>The Horror Begins</u></b>\n<size=90%>Find your way out.\n <size=80%><i>*Escape through Ashbury Forest.</i>");
		}

		//QUEST 3
		if (_quest2)
        {
			questText.SetText($"<b><u>{quests[2].title.GetLocalizedString()}</u></b>\n<size=90%>{quests[2].description.GetLocalizedString()}\n <size=80%><i>{quests[2].hint.GetLocalizedString()}</i>");
			//questText.SetText("<b><u>The Cosmic Plan</u></b>\n<size=90%>Activate the Gateway.\n <size=80%><i>*Retrieve the five books of cosmic knowledge in Ashbury.</i>");

        }

		//QUEST 4
		if (_quest3)
		{
			questText.SetText($"<b><u>{quests[3].title.GetLocalizedString()}</u></b>\n<size=90%>{quests[3].description.GetLocalizedString()}\n <size=80%><i>{quests[3].hint.GetLocalizedString()}</i>");
            //questText.SetText("<b><u>Into The Unknown</u></b>\n<size=90%>Escape through the Gateway.");
        }
	}

	public void ActivateGateway()
	{
		items.Clear();
        for (int i = 0; i < InvManager.Items; i++)
        {
            if (InvManager.Slots[i].IsTaken == true) //Checking if there's an item in this slot.
            {
                Item itemScript = InvManager.Slots[i].Item.GetComponent<Item>(); //Getting the item script from the items inside the bag.
                items.Add(itemScript.Name);
            }

            if (items.Contains("The House Of R'lyeh") &&
                        items.Contains("Mark Of The Old Gods") &&
                        items.Contains("Invocations of The Cosmic Horror") &&
                        items.Contains("The Shadow People") &&
                        items.Contains("The Unknown and The Deep Void") &&
                        insaneMode.insaneMode == false) //Checking if the type of the new item matches with another item already in the bag.
            {
                _quest3 = true;
				StartCoroutine(GatewayAnimation());
            }
        }
    }

	public IEnumerator GatewayAnimation()
	{
		gatewayWallAnimator.SetBool("OpenGateway", true);
		MeshCollider gatewayMeshFilter = gatewayWallAnimator.gameObject.GetComponent<MeshCollider>();
		gatewayAudio.Play();
		gatewayMeshFilter.enabled= false;
		yield return new WaitForSeconds(1f);
		gatewayWallAnimator.SetBool("OpenGateway", false);
	}

    public void LoadData(GameData data)
    {
        this._quest1 = data.quest1;
		this._quest2 = data.quest2;
		this._quest3 = data.quest3;
    }

    public void SaveData(GameData data)
    {
        data.quest1= _quest1;
		data.quest2= _quest2;
		data.quest3= _quest3;
    }
}