using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

[RequireComponent(typeof(AStarPathfindingAgent))]
class GetScroolWizard : MonoBehaviour
{
    public Item RequiredItem;

    public MissionReward Reward;
    public Transform teleportPosition;

    public bool canEscape = false;

    private void Start()
    {
        teleportPosition = GameObject.Find("DoorToHell").transform;   
    }

    public void EscapeToDungeon()
    {
        var wizardOnScene = GameObject.FindGameObjectsWithTag("NPC").ToList().Where(el => el.name == gameObject.name).First();

        wizardOnScene.GetComponent<AStarPathfindingAgent>().point = wizardOnScene.GetComponent<GetScroolWizard>().teleportPosition.position;
        Debug.Log(wizardOnScene.GetComponent<AStarPathfindingAgent>().point);
        Debug.Log(Vector2.Distance(wizardOnScene.GetComponent<AStarPathfindingAgent>().point, wizardOnScene.transform.position));

        wizardOnScene.GetComponent<AStarPathfindingAgent>().FindPoint();

        wizardOnScene.GetComponent<GetScroolWizard>().Escape();
    }

    public void Escape()
    {
        Debug.Log("Start courtine");
        StartCoroutine(gameObject.GetComponent<AStarPathfindingAgent>().FollowPath());
    }

    // CHANGE THIS - we dupplicated herelogic from mission!!!

    public void CheckCondition()
    {
        // CHECK CONDITION JEST WYWOŁYWANE W SUROWYM OBIEKCIE scriptable boject
        // TO SIE DOWOŁUJE DO prefaba wizarda wiec musimy sila szuakc obietow na scenie heh
        if(InventoryUIManager.Instance.FindItemInWarehouseByName(RequiredItem.name).Any())
        {
            var wizardOnScene = GameObject.FindGameObjectsWithTag("NPC").ToList().Where(el => el.name == gameObject.name).First();

            wizardOnScene.GetComponent<DialogueManager>().Dialogue.GoToNextSentence();
        } else
        {
            var wizardOnScene = GameObject.FindGameObjectsWithTag("NPC").ToList().Where(el => el.name == gameObject.name).First();

            wizardOnScene.GetComponent<DialogueManager>().Dialogue.BreakDialogueStep();
        }
    }

    // Move to npc action bucket (Action script)
    public void TakeItem()
    {
        if(-1 != InventoryUIManager.Instance.RemoveOneByItemName(RequiredItem.name))
        {
            TaskUIManager.Instance.RemoveByName("Wizard Quest");

            // go to next sentence
            var wizardOnScene = GameObject.FindGameObjectsWithTag("NPC").ToList().Where(el => el.name == gameObject.name).First();

            wizardOnScene.GetComponent<DialogueManager>().Dialogue.GoToNextSentence();
        } else
        {
            var wizardOnScene = GameObject.FindGameObjectsWithTag("NPC").ToList().Where(el => el.name == gameObject.name).First();

            // Beacuse we have CheckCondition this should be uselles
            if (wizardOnScene.GetComponent<DialogueManager>().Dialogue)
                wizardOnScene.gameObject.GetComponent<DialogueManager>().Dialogue.BreakDialogueStep();
        }

    }

    // copied from mission
    public void GetReward()
    {
        var Player = GameObject.FindGameObjectWithTag("Player");

        if (!Player)
            Debug.LogError("GetScroolWizard::GetReward - There is no player on scene!!!");

        Player.GetComponent<PlayerActions>().GetReward(Reward);
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.tag == "SceneTransition")
        {
            // basically useless xd
            DungeonManager.SetTeleported();

            collision.gameObject.GetComponent<DoorBehaviour>().isEnabled = true;

            Destroy(gameObject);
        }
    }
}