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

public class PlayerInteract : MonoBehaviour
{
    public bool canClimb;

    public GameObject collisionObject;
    public GameObject negativeThoughts;
    public GameObject positiveThoughts;
    public Items items;
    public Enemy enemy;

    public bool isCounting;
    public bool hasNegativeThoughts;
    public bool hasPositiveThoughts;
    public bool isGameOverW;
    public bool isGameOverL;

    private float damage;

    // Start is called before the first frame update
    void Awake()
    {
        damage = .2f;

        canClimb = false;
        isCounting = false;
        hasNegativeThoughts = false;
        hasPositiveThoughts = false;
        negativeThoughts.SetActive(false);
        positiveThoughts.SetActive(false);
        isGameOverW = false;
        isGameOverL = false;
    }

    // Update is called once per frame
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Ladder")
        {
            collisionObject = collision.gameObject;
            canClimb = true;
        }

        if(collision.gameObject.tag == "Machine")
        {
            collisionObject = collision.gameObject;
        }

        if(collision.gameObject.tag == "Equipment")
        {
            items.canGet = true;
            collisionObject = collision.gameObject;
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Ladder")
        {
            canClimb = false;
            ladderChildActive(true);
            collisionObject = null;
        }

        if (collision.gameObject.tag == "Machine")
        {
            collisionObject = null;
            isCounting = false;
            machineTimerActive(false);
        }

        if(collision.gameObject.tag == "Equipment")
        {
            items.canGet = false;
            collisionObject = null;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Serve")
        {
            collisionObject = collision.gameObject;
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {

        if (collision.gameObject.tag == "Serve")
        {
            collisionObject = null;
        }
    }

    public void ladderChildActive(bool foo)
    {
        collisionObject.transform.GetChild(0).gameObject.SetActive(foo);
    }

    public void machineTimerActive(bool foo)
    {
        collisionObject.transform.GetChild(0).GetChild(0).gameObject.SetActive(foo);
    }


    public void onGameOver(bool foo)
    {
        if(foo)
            PlayerPrefs.SetInt("GameOverStatus", 1);
        else
            PlayerPrefs.SetInt("GameOverStatus", 0);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (collisionObject.tag == "Machine" && hasNegativeThoughts)
            {
                items.availableCount++;
                isCounting = true;
                machineTimerActive(true);
            }

            if (collisionObject.tag == "Equipment" && items.canGet)
            {
                hasNegativeThoughts = true;
                items.availableCount--;
                negativeThoughts.SetActive(true);
            }

            if (collisionObject.tag == "Serve" && hasPositiveThoughts)
            {
                hasPositiveThoughts = false;
                items.availableCount--;
                enemy.TakeDamage(damage);
                if (enemy.currentHealth <= 0.01f)
                {
                    isGameOverW = true;
                    onGameOver(true);
                    SceneManager.LoadScene(2);
                }
                positiveThoughts.SetActive(false);
            }
        }

        if(Input.GetKeyUp(KeyCode.E))
        {
            if (collisionObject.tag == "Machine")
            {
                isCounting = false;
                machineTimerActive(false);
            }
        }
    }
}
