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

public class UpgradeScreen : MonoBehaviour
{
    public Transform[] CardUpgradeAnchors = null;

    private float StartTime = 0.0f;
    private bool Initialized = false;

    public TextMeshProUGUI MaxLifeValueText = null;

    // Start is called before the first frame update
    void Start()
    {
        StartTime = Time.time + 0.5f;


    }

    // Update is called once per frame
    void Update()
    {
        if (!Initialized && Time.time > StartTime)
        {
            for (int i = 0; i < 4; i++)
            {
                Card cardToUse = CardDatabase.Instance.Cards[Random.Range(2, CardDatabase.Instance.Cards.Count)];
                CardInstance ci = GetComponent<CardFactory>().CreateCard(cardToUse);

                ci.SetBlackboardValue("Index", i);

                ci.TargetWorldPosition = CardUpgradeAnchors[i].position;
                ci.TargetRotation = CardUpgradeAnchors[i].rotation;
            }

            Initialized = true;
        }


        MaxLifeValueText.text = PlayerStats.MaxHealth.ToString();
    }

    public void OnPlayButtonPressed()
    {
        UnityEngine.SceneManagement.SceneManager.LoadScene("CombatScene");
    }

    private Transform GetTransformForCard(CardInstance ci)
    {
        int index = ci.GetBlackboardInt("Index");
        return CardUpgradeAnchors[index];
    }

    public void HoverCard(CardInstance ci)
    {
        ci.BeingHovered = true;

        Transform anchorToUse = GetTransformForCard(ci);
        Vector3 toCam = (Camera.main.transform.position - anchorToUse.position).normalized;
        ci.TargetWorldPosition = anchorToUse.position + toCam * 2.0f;
        ci.TargetRotation = anchorToUse.rotation;
    }

    public void UnhoverCard(CardInstance ci)
    {
        ci.BeingHovered = false;

        Transform anchorToUse = GetTransformForCard(ci);
        ci.TargetWorldPosition = anchorToUse.position;
        ci.TargetRotation = anchorToUse.rotation;
    }
}
