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

public class GodHand : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {
        
    }

    private void Awake()
    {
        GetComponent<HealthUpdater>().HealthComponentDied += GodHand_HealthComponentDied;
    }

    private void OnDestroy()
    {
        GetComponent<HealthUpdater>().HealthComponentDied -= GodHand_HealthComponentDied;
    }

    private void GodHand_HealthComponentDied(object sender, System.EventArgs e)
    {
        HealthComponent hc = sender as HealthComponent;
        if (CurrentTarget == hc)
            CurrentTarget = null;
    }

    CardInstance hoveredCard = null;

    public HealthComponent GetCurrentTarget()
    {
        return CurrentTarget;
    }

    private HealthComponent CurrentTarget = null;

    private void PickANewTarget()
    {
        float lowestHealth = 5000;
        CurrentTarget = null;

        Team[] teamlist = FindObjectsOfType<Team>();
        foreach (Team t in teamlist)
        {
            if (t.team == TeamValue.Enemy)
            {
                if (t.GetComponent<HealthComponent>().Health < lowestHealth)
                {
                    CurrentTarget = t.GetComponent<HealthComponent>();
                    lowestHealth = CurrentTarget.Health;
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (GetComponent<Choreographer>().CurrentState == ChoreographerStates.Idle)
        {
            if (CurrentTarget == null)
                PickANewTarget();

            RaycastHit cardHit;
            Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out cardHit);
            if (cardHit.collider != null)
            {
                GameObject logicalObject = cardHit.collider.gameObject;
                while (logicalObject.GetComponent<CardInstance>() == null && logicalObject.transform.parent != null)
                    logicalObject = logicalObject.transform.parent.gameObject;

                CardInstance ci = logicalObject.GetComponent<CardInstance>();

                if (GetComponent<CardManager>().IsCardInHand(ci))
                    SetHoveredCard(ci);
                else
                    SetHoveredCard(null);
            }
            else
            {
                SetHoveredCard(null);
            }

            if (Input.GetMouseButtonDown(0))
            {
                if (hoveredCard != null)
                {
                    // Check if you have enough energy
                    if (hoveredCard.CardTemplate.ManaCost > GetComponent<Choreographer>().PlayerEntity.GetComponent<EnergyComponent>().Energy)
                    {
                        hoveredCard.gameObject.FloatText("Not Enough Energy");
                    }
                    else
                    {
                        // Play the hovered card
                        CardInstance cardToPlay = hoveredCard;
                        SetHoveredCard(null);
                        GetComponent<Choreographer>().PlayCard(cardToPlay);
                    }
                }

                // Check if we want to pick a monster
                RaycastHit monsterHit;
                int mask = LayerMask.GetMask("Monster");
                Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out monsterHit, 100.0f, mask);
                if (monsterHit.collider != null)
                {
                    HealthComponent hc = monsterHit.collider.GetComponent<HealthComponent>();
                    if (hc != null)
                        CurrentTarget = hc;
                }

            }
        }
    }

    private void SetHoveredCard(CardInstance target)
    {
        if (hoveredCard == target)
            return;

        if (hoveredCard != null)
        {
            GetComponent<CardManager>().UnhoverCard(hoveredCard);
        }

        if (target != null)
        {
            GetComponent<CardManager>().HoverCard(target);
        }

        hoveredCard = target;
    }
}
