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

public class Pile : MonoBehaviour
{
    public Card CardPrefab;
    public Card CurrentCard;

    private Hand HandSystem;
    
    // Start is called before the first frame update
    void Start()
    {
        HandSystem = GameObject.Find("Hand").GetComponent<Hand>();
        DrawCard();
    }

    public bool ApplyCard(Card AddCard)
    {
        if (!CurrentCard) return false;

        CurrentCard.StopPreview();
        for (int i=0; i<AddCard.Gauges.Count; ++i)
        {
            Gauge CurAdd = AddCard.Gauges[i];
            for(int j=0; j<CurrentCard.Gauges.Count; ++j)
            {
                Gauge CardGauge = CurrentCard.Gauges[j];
                if(CurAdd.Name == CardGauge.Name) // Same theme
                {
                    CardGauge.Value = Mathf.Max(0.0f, CardGauge.Value - CurAdd.Value);
                }
            }
        }
        return CheckCard();
    }

    public bool CheckCard()
    {
        if (!CurrentCard) return false;

        bool IsComplete = true;
        for (int j = 0; j < CurrentCard.Gauges.Count; ++j)
        {
            Gauge CardGauge = CurrentCard.Gauges[j];
            if(CardGauge.Value>=0.05f)
            {
                IsComplete = false;
                break;
            }
        }
        if(IsComplete)
        {
            DrawCard();
        }
        return IsComplete;
    }

    public void ResetPile()
    {
        if (CurrentCard)
        {
            CurrentCard.StopHover();
            GameObject.Destroy(CurrentCard.gameObject);            
            CurrentCard = null;
        }
        DrawCard();
    }

    public void DrawCard() {
        if(CurrentCard)
        {
            Interpol NewInterpol = CurrentCard.gameObject.AddComponent<Interpol>();
            NewInterpol.DestroyOnArrival = true;
            NewInterpol.Goal = new Vector3(14, 2, 10.0f);
            CurrentCard.StopHover();
            //GameObject.Destroy(CurrentCard.gameObject);            
            CurrentCard = null;
        }
        GameObject NewCardObj = GameObject.Instantiate(CardPrefab.gameObject, transform);
        Card NewCard = NewCardObj.GetComponent<Card>();
        //NewCard.Price = 100 * Random.Range(1,3);
        float BonusRandom = Random.value;
        if (BonusRandom<0.4f)
        {
            NewCard.PriceMult = Random.Range(2, 4);
        }
        else if(BonusRandom<0.8f)
        {
            NewCard.CardMult = Random.Range(2, 4);
        }
        else
        {

        }
        
        CurrentCard = NewCard;
        Vector3 CurPos = CurrentCard.transform.localPosition;
        CurPos.y = 10;
        CurrentCard.transform.localPosition = CurPos;
    }

    // Update is called once per frame
    void Update()
    {
        if(CurrentCard)
        {
            Vector3 CurPos = CurrentCard.transform.localPosition;
            CurPos.y = Mathf.Lerp(CurPos.y, 0.0f, Time.deltaTime*3.0f);
            CurrentCard.transform.localPosition = CurPos;
        }
    }


    public void StartHover()
    {
        Vector3 NewPos = transform.localPosition;
        NewPos.z = -10;
        transform.localPosition = NewPos;
        if (CurrentCard) CurrentCard.StartHover();
    }

    public void StopHover()
    {
        Vector3 NewPos = transform.localPosition;
        NewPos.z = 0;
        transform.localPosition = NewPos;
        if (CurrentCard) CurrentCard.StopHover();
    }
}
