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

public class Deck : MonoBehaviour
{
    public List<CardData> cardDataList;

    public List<CardData> drawList;
    // Control Card Display
    public List<GameObject> drawDisplayList;

    public List<CardData> handList;

    public List<CardData> discardList;
    // Control Card Display
    public List<GameObject> discardDisplayList;

    public List<CardData> wasteList;
    public int randomSeed;

    public GameObject handCardTemplate;
    public GameObject cardDisplayTemplate;

    public GameObject drawListContent;
    public GameObject discardListContent;

    // Start is called before the first frame update
    void Start()
    {
        randomSeed = 200;
        Random.InitState(randomSeed);
        for (int i = 0; i < cardDataList.Count; i++)
        {
            drawList.Add(cardDataList[i]);
            GameObject tempDrawDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DrawListContent/Viewport/Content"));
            tempDrawDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(cardDataList[i]);
            tempDrawDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = cardDataList[i].artwork;
            tempDrawDisplay.SetActive(true);
            drawDisplayList.Add(tempDrawDisplay);
        }
        drawListContent.SetActive(false);
        discardListContent.SetActive(false);
    }

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

    public void DrawCards(int amount)
    {
        for (int i = 0; i < amount; i++)
        {
            if (drawList.Count == 0)
            {
                Shuffle();
            }
            if(drawList.Count==0)
            {
                return;
            }
            int randomIndex = Random.Range(0, drawList.Count);
            GameObject cardObj = Instantiate(handCardTemplate.gameObject, transform.Find("Content/HandList"));
            cardObj.SetActive(true);
            cardObj.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(drawList[randomIndex]);
            cardObj.transform.Find("Button").gameObject.GetComponent<Image>().sprite = drawList[randomIndex].artwork;
            drawList.RemoveAt(randomIndex);
            Destroy(drawDisplayList[randomIndex]);
            drawDisplayList.RemoveAt(randomIndex);
        }
    }

    public void UseACard(int index)
    {
        // TODO: Add card type into card data
        // Chech if the card is waste card;
        //if (handList[index].cardTags == wastCard)
        //{
        //    wasteList.Add(handList[index]);
        //}
        //else
        //{
        discardList.Add(handList[index]);
        //}
        handList.RemoveAt(index);
    }

    public void UseThisCard(GameObject card)
    {
        GameObject tempDiscardDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DiscardListContent/Viewport/Content"));
        tempDiscardDisplay.SetActive(true);
        tempDiscardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData);
        tempDiscardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData.artwork;
        discardDisplayList.Add(tempDiscardDisplay);
        discardList.Add(card.transform.Find("Card").gameObject.gameObject.GetComponent<CardDisplay>().cardData);
        Destroy(card);
    }

    // When unit dead
    public void AddACardIntoDiscardList(CardData card)
    {
        discardList.Add(card);
    }

    public void Shuffle()
    {
        // Put all cards in drawList into discardList
        for (int i = 0; i < drawList.Count; i++)
        {
            discardList.Add(drawList[i]);
            Destroy(drawDisplayList[i]);
        }
        drawDisplayList.Clear();
        drawList.Clear();

        while (discardList.Count != 0)
        {
            int index = Random.Range(0, discardList.Count);
            drawList.Add(discardList[index]);
            GameObject tempCardDisplay = Instantiate(cardDisplayTemplate.gameObject, transform.Find("DrawListContent/Viewport/Content"));
            tempCardDisplay.transform.Find("Card").gameObject.GetComponent<CardDisplay>().SetCardData(discardList[index]);
            tempCardDisplay.transform.Find("Button").gameObject.GetComponent<Image>().sprite = discardList[index].artwork;
            tempCardDisplay.SetActive(true);
            drawDisplayList.Add(tempCardDisplay);
            discardList[index] = discardList[discardList.Count - 1];
            discardList.RemoveAt(discardList.Count - 1);
        };
        for (int i = 0; i < discardDisplayList.Count; i++)
        {
            Destroy(discardDisplayList[i]);
        }
    }

    public void ActivateDisplayDrawList()
    {
        drawListContent.SetActive(true);
    }

    public void DeactivateDisplayDrawList()
    {
        drawListContent.SetActive(false);
    }

    public void ActivateDisplayDiscardList()
    {
        discardListContent.SetActive(true);
    }

    public void DeactivateDisplayDiscardList()
    {
        discardListContent.SetActive(false);
    }
}
