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

public class BuildingBuilder : MonoBehaviour, ICardHolder
{
    [SerializeField] private GameObject buildingResourcesDisplayParent;

    [SerializeField] private BuilderResourceDisplay buildingResourcesDisplayPrefab;
    [SerializeField] private ConsumeCardHoldArea resourceCardHoldingArea;
    [SerializeField] private PeopleCardHoldArea workers;
    [SerializeField] private Slider slider;

    [SerializeField] private AudioClipGroup builtSound;

    public PeopleCardHoldArea PeopleCardHoldArea { get { return workers; } }

    private Building building;
    private Dictionary<CardData, int> buildingResources = new ();
    private Dictionary<CardData, BuilderResourceDisplay> buildingResourceDisplays = new();
    private int resourceSum = 0;

    private float buildingTimer = 999;

    private void Awake()
    {
        resourceCardHoldingArea.cardHolder = this;
        workers.cardHolder = this;
    }

    private void Update()
    {
        if (buildingTimer <= 0 && workers.WorkerCount == 0 || GameManager.Instance.CreativeMoveOn)
        {
            building.Build();
            Destroy(gameObject);
        }
        else if (buildingTimer > 0 && workers.WorkerCount > 0 && resourceSum <= 0)
        {
            buildingTimer -= GameManager.Instance.GameSpeed;
            slider.value = (building.BuildingData.BuildTime - buildingTimer) / building.BuildingData.BuildTime;
            slider.transform.parent.gameObject.SetActive(true);

            if (buildingTimer <= 0)
            {
                builtSound.Play();
                GetComponent<SpriteRenderer>().enabled = false;
                slider.transform.parent.gameObject.SetActive(false);
            }
        }
        else slider.transform.parent.gameObject.SetActive(false);
    }

    public void Initiate(Building building)
    {
        this.building = building;
        if (GameManager.Instance.CreativeMoveOn)
        {
            buildingResourcesDisplayParent.SetActive(false);
            slider.transform.parent.parent.gameObject.SetActive(false);
            workers.gameObject.SetActive(false);
            GetComponent<SpriteRenderer>().enabled = false;
            return;
        }

        buildingTimer = building.BuildingData.BuildTime;

        float resourcesDisplayHeight = building.BuildingData.Size.y / 2f + 1f;
        float sliderHeight = building.BuildingData.Size.y / 2f + 0.3f;
        float peopleHoldAreaHeight = building.BuildingData.Size.y / 2f - 0.5f;

        buildingResourcesDisplayParent.transform.position += new Vector3(0, resourcesDisplayHeight);
        slider.transform.parent.parent.position += new Vector3(0, sliderHeight);
        workers.transform.position -= new Vector3(0, peopleHoldAreaHeight);

        RectTransform displayRectTransform = buildingResourcesDisplayParent.GetComponent<RectTransform>();
        displayRectTransform.sizeDelta = new Vector2(building.BuildingData.Size.x * 100,
            displayRectTransform.sizeDelta.y);

        RectTransform rectTransform = slider.transform.parent.parent.GetComponent<RectTransform>();
        rectTransform.sizeDelta += new Vector2(100, 0);

        SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
        spriteRenderer.size = building.BuildingData.Size;
        spriteRenderer.enabled = true;

        BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
        boxCollider2D.size = building.BuildingData.Size;

        foreach (var pair in building.BuildingData.BuildingCost)
        {
            resourceSum += pair.Amount;
            buildingResources.Add(pair.CardData, pair.Amount);
            BuilderResourceDisplay builderResourceDisplay = Instantiate(buildingResourcesDisplayPrefab, buildingResourcesDisplayParent.transform);
            builderResourceDisplay.Initiate(pair.CardData, pair.Amount);
            buildingResourceDisplays.Add(pair.CardData, builderResourceDisplay);
        }
    }

    public bool CanAddCard(CardData card)
    {
        return (buildingResources.ContainsKey(card) && 
            buildingResources[card] > 0) || 
            (card.CardType == CardType.Person);
    }

    public void AddCard(CardData card)
    {
        if (buildingResources.ContainsKey(card))
        {
            if (buildingResources[card] > 0)
            {
                buildingResources[card]--;
                resourceSum--;
                buildingResourceDisplays[card].UpdateText(buildingResources[card].ToString());

                if (buildingResources[card] <= 0)
                {
                    buildingResourceDisplays[card].gameObject.SetActive(false);
                }
            }
        }

        if (resourceSum <= 0)
        {
            buildingResourcesDisplayParent.SetActive(false);
        }
    }

    public void RemoveCard(CardData card)
    {
        /*
        if (buildingResources.ContainsKey(card))
        {
            buildingResources[card]++;
            resourceSum++;
            startBuilding = false;
            buildingResourceDisplays[card].gameObject.SetActive(true);
            buildingResourcesDisplayParent.SetActive(true);
        }*/
    }
}
