using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MindRead : MonoBehaviour
{

    public int thoughtType = 0;

    private int ammoManage;
    private int randomNumber;
    public bool mindExhausted = false;

    private bool readAvailable = false;
    private float exhaustionTimer = 3f;
    static public float exhaustionInit = -1.0f;

    private SpriteRenderer question, heart, skull;

void Awake()
    {
        question = GameObject.Find("questionMark").GetComponent<SpriteRenderer>();
        heart = GameObject.Find("heart").GetComponent<SpriteRenderer>();
        //heart.enabled = false;
        skull = GameObject.Find("skull").GetComponent<SpriteRenderer>();
        //skull.enabled = false;
    }


void ReadRoller()
    {
        randomNumber = Random.Range(0, 10);

        //Debug.Log(randomNumber);

        if (randomNumber > 5)
        {
            thoughtType = 1;
            mindExhausted = true;
        }

        else if (randomNumber < 5)
        {
            thoughtType = 2;
            mindExhausted = true;

        }

        else if (randomNumber == 5)
        {
            thoughtType = 3;
            mindExhausted = false;
        }

		if ( mindExhausted )
		{
			exhaustionTimer = time.time + 2f;
		}
		else
		{
			exhaustionTimer = -1.0f;
		}

    }


void OnTriggerEnter2D(Collider2D npcHit)
    {

        Debug.Log("Mind exhausted?: " + mindExhausted);
        if (npcHit.gameObject.tag == ("NPCRead"))
        {
            readAvailable = true;


        }
    }

	
   void OnTriggerExit2D(Collider2D npcHit)
    {
        Debug.Log("Trigger Exit");
		
		// zeuc: I removed this
		/*
        exhaustionTimer = exhaustionTimer - Time.deltaTime; /////////////////// I JUST ADDED THIS TIMER TO SEE IF I COULD SOMEHOW MAKE
																	/////////// MINDEXHAUSTED FALSE AGAIN, BUT NO DICE
        if (exhaustionTimer == 0)
        {
            mindExhausted = false;
            exhaustionTimer = exhaustionInit;
        }
		*/
        readAvailable = false;
    }

void Update()
    {


        switch (thoughtType)
        {
            case 0:
                heart.enabled = false;
                skull.enabled = false;
                question.enabled = true;
                break;

            case 1:
                heart.enabled = true;
                skull.enabled = false;
                question.enabled = false;
                break;

            case 2:
                heart.enabled = false;
                skull.enabled = true;
                question.enabled = false;
                break;

            default:
                question.enabled = true;
                break;
        }


        if (readAvailable == true)
        {
			bool check_exhaust_again = false;
			
			if ( exhaustionTimer > -1.0f && exhaustionTimer < time.time )
			{
				check_exhaust_again = true;
			}
			
            if (Input.GetKeyDown(KeyCode.R) && (mindExhausted == false || check_exhaust_again) )
            {
                ReadRoller();

                if (thoughtType != 3)
                {
                    GetComponent<PlayerResource>().bulletCount += 1;
                }
                Debug.Log("Thought type: " + thoughtType + " // (" + randomNumber + ")");




                if (GetComponent<MindRead>().thoughtType == 1)
                {
                    //if (GetComponent<PlayerMove>().emotionalBaggage > 1)
                    if (GetComponent<PlayerMove>().emotionalBaggage > 0)
                    {
                        GetComponent<PlayerMove>().emotionalBaggage -= 1;
                    }
                    GetComponent<PlayerMove>().pSpeed += 4f;

                    if ((int)GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyScript>().eHP != 1)
                    {

                        GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyScript>().eHP -= 1;
                        Debug.Log("Think happy thoughts and feel your feet dance across clouds of love");
                    }
                }

                if (GetComponent<MindRead>().thoughtType == 2)
                {
                    GetComponent<PlayerMove>().emotionalBaggage += 1;
                    GetComponent<PlayerMove>().pSpeed -= 4f;
                    GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyScript>().eHP += 1;
                    Debug.Log("Ugh.. Nothing but darkness. Those thoughts are not going to be easy to forget..");

                }
                if (GetComponent<MindRead>().thoughtType == 3)
                {
                    Debug.Log("This one's mind is a bit tricky.. Try again!");
                    GetComponent<PlayerMove>().pSpeed += 1f;
                }



                if (GetComponent<PlayerMove>().pSpeed >= 40f)
                {
                    GetComponent<PlayerMove>().pSpeed = 40f;
                }

                if (GetComponent<PlayerMove>().pSpeed <= 12f)
                {
                    GetComponent<PlayerMove>().pSpeed = 12f;
                }

                if (GetComponent<PlayerMove>().emotionalBaggage < 0)
                {
                    GetComponent<PlayerMove>().emotionalBaggage = 0;
                }

                if (GetComponent<PlayerMove>().emotionalBaggage > 3)
                {
                    GetComponent<PlayerMove>().emotionalBaggage = 3;
                }

 if ((int)GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyScript>().eHP >= 5)
                {
                    GameObject.FindGameObjectWithTag("Enemy").GetComponent<EnemyScript>().eHP = 5;
                }

readAvailable = false;

            }
		}
    }
}