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

public class SpawnCard : MonoBehaviour
{
    public GameObject enemyPrefab;
    public GameObject warriorPrefab;
    private bool spawnWarrior = false;
    public GameObject archerPrefab;
    private bool spawnArcher = false;
    public GameObject zeppelinPrefab;
    private bool spawnZeppelin = false;
    public GameObject dragonPrefab;
    private bool spawnDragon = false;
    private Button selectCard;
    public PolygonCollider2D coll;
    public ManaRefil manaRefil;
    public entities entity;
    public float spawnCost { get; private set; }

    private void Start()
    {
        Debug.Log(entity.warrior.cost == null);
        // Debug.Log(entities.warrior.cost);
        CardSelection();
        
    }

    private void CardSelection()
    {
        selectCard = this.GetComponent<Button>();
        selectCard.onClick.AddListener(CardCheck);
    }
    private void CardCheck()
    {
        
        if (selectCard.gameObject.name == "SelectCardWarrior" && manaRefil.setManaCounter >= entity.warrior.cost)
        {
            spawnWarrior = true;
            Debug.Log("selected card: " + selectCard.gameObject.name);
            spawnCost = 2f;

        }
        else if (selectCard.gameObject.name == "SelectCardArcher" /*&& mana.mana >= 3*/)
        {
            spawnArcher = true;
            Debug.Log("selected card: " + selectCard.gameObject.name);
        }
        else if (selectCard.gameObject.name == "SelectCardZeppelin" /*&& mana.mana >= 4*/)
        {
            spawnZeppelin = true;
            Debug.Log("selected card: " + selectCard.gameObject.name);
        }
        else if (selectCard.gameObject.name == "SelectCardDragon" /*&& mana.mana >= 5*/)
        {
            spawnDragon = true;
            Debug.Log("selected card: " + selectCard.gameObject.name);
        }
        else CardCheck();
        Update();
    }

    private void SpawnCharacter()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector2 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            // Cast a ray through colliders
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit2D hit = Physics2D.GetRayIntersection(ray);

            // If it hits a collider with a tag SpawnZone it will allow to spawn entitys
            if (hit.collider != null && hit.collider.gameObject.tag == "SpawnZone")
            {
                if (spawnWarrior == true)
                {
                    GameObject a = Instantiate(warriorPrefab) as GameObject;
                    a.transform.position = new Vector2(mousePos.x, mousePos.y);
                    spawnWarrior = false;
                }
                else if (spawnArcher == true)
                {
                    GameObject a = Instantiate(archerPrefab) as GameObject;
                    a.transform.position = new Vector2(mousePos.x, mousePos.y);
                    spawnArcher = false;
                }
                else if (spawnZeppelin == true)
                {
                    GameObject a = Instantiate(zeppelinPrefab) as GameObject;
                    a.transform.position = new Vector2(mousePos.x, mousePos.y);
                    spawnZeppelin = false;
                }
                else if (spawnDragon == true)
                {
                    GameObject a = Instantiate(dragonPrefab) as GameObject;
                    a.transform.position = new Vector2(mousePos.x, mousePos.y);
                    spawnDragon = false;
                }
                else Debug.Log("No entity is selected");
            }
        }
    }

    private void Update()
    {
        SpawnCharacter();
    }
}