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


// Card Manager is responsible for
// Deciding where all the cards should go physically on the screen
// Tracking all the logical card data
//
// UIUpdater is responsible for moving cards from where they are now to where they are supposed to be
//
// CardInstance is responsible for storing all of the data related to where a card is and where it is supposed to be

public class CardManager : MonoBehaviour
{
    public Transform DeckAnchor = null;
    public Transform[] HandAnchors = null;
    public Transform DiscardAnchor = null;


    // Deck parameters
    public List<CardInstance> Deck = new List<CardInstance>();
    private float DistanceBetweenStackedCards = 0.05f;

    // Hand parameters
    public List<CardInstance> Hand = new List<CardInstance>();
    public int MaxHandSize = 3;

    // Discard parameters
    public List<CardInstance> Discard = new List<CardInstance>();

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

    }

    // Update is called once per frame
    void Update()
    {

    }


    public void AddCardToDeck(CardInstance c)
    {
        Deck.Insert(Random.Range(0, Deck.Count), c);
        MoveCardToPositionInDeck(c);
    }

    public void MoveCardToPositionInDeck(CardInstance c)
    {
        c.TargetWorldPosition = DeckAnchor.position + new Vector3(0.0f, DistanceBetweenStackedCards * Deck.IndexOf(c), 0.0f);
        c.TargetRotation = DeckAnchor.rotation;
        // c.TargetXRotation = -90.0f;
    }

    public Transform GetTransformForCardInHand(CardInstance c)
    {
        int indexOfCard = Hand.IndexOf(c);

        if (indexOfCard < 0)
        {
            Debug.Log("WARNING: GetWorldPositionForCardInHand but card was not in hand ");
        }

        return HandAnchors[indexOfCard].transform;

    }

    public void DrawTopCardToHand()
    {
        if (Deck.Count == 0)
        {
            ReturnDiscardToDeck();
        }

        if (Hand.Count < MaxHandSize)
        {
            CardInstance cardToMove = Deck[Deck.Count - 1];
            Deck.RemoveAt(Deck.Count - 1);
            Hand.Add(cardToMove);

            // Set the movement position for the card
            Transform anchorToUse = GetTransformForCardInHand(cardToMove);
            cardToMove.TargetWorldPosition = anchorToUse.position;
            cardToMove.TargetRotation = anchorToUse.rotation;

            DeckAnchor.GetComponent<SoundRandomizer>().Play();
        }
    }

    public void ReturnDiscardToDeck()
    {
        for (int i = Discard.Count - 1; i >= 0; i--)
        {
            CardInstance cardToMove = Discard[i];
            Discard.RemoveAt(i);

            Deck.Insert(Random.Range(0, Deck.Count), cardToMove);

        }

        foreach (CardInstance c in Deck)
        {
            MoveCardToPositionInDeck(c);
        }
        DiscardAnchor.GetComponent<AudioSource>().Play();

    }

    public bool IsCardInHand(CardInstance c)
    {
        return Hand.Contains(c);
    }

    public void DiscardAllBut(CardInstance c)
    {
        for (int i = Hand.Count - 1; i >= 0; i--)
        {
            if (Hand[i] != c)
            {
                DiscardCard(Hand[i]);
            }
        }

    }
    public void DiscardAll()
    {
        for (int i = Hand.Count - 1; i >= 0; i--)
        {
            DiscardCard(Hand[i]);
        }
    }

    public void DiscardCard(CardInstance c)
    {
        Hand.Remove(c);
        Discard.Add(c);

        // Animate card to discard pile
        c.TargetWorldPosition = DiscardAnchor.position + new Vector3(0.0f, DistanceBetweenStackedCards * Discard.Count, 0.0f);
        c.TargetRotation = DiscardAnchor.rotation;
    }

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

        Transform anchorToUse = GetTransformForCardInHand(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 = GetTransformForCardInHand(ci);
        ci.TargetWorldPosition = anchorToUse.position;
        ci.TargetRotation = anchorToUse.rotation;

    }

    public void PlayCard(CardInstance ci)
    {
        ci.TargetWorldPosition = ci.transform.position + new Vector3(0.0f, 2.0f, 2.0f);
    }

}
