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

public class Production : MonoBehaviour
{

    public int BuildingID;
    public float Interval;

    [HideInInspector]
    public GameObject Level;
    [HideInInspector]
    public GameObject Inventory;
    [HideInInspector]
    public float Counter;
    [HideInInspector]
    public List<int> ItemStack = new();

    void Start()
    {
        Counter = Interval;
    }


    void Update()
    {
        Counter -= Time.deltaTime;
        if (Counter <= 0f)
        {
            Counter = Interval;
            Produce();
        }
    }

    public void TryAdd()
    {
        var inventory = Inventory.GetComponent<Inventory>();
        var itemID = inventory.CurrentItemID;
        if (!IsItemAllowed(itemID))
            return;
        if (inventory.ItemCounts[itemID] > 0)
        {
            inventory.ItemCounts[itemID]--;
            if (ItemStack.Count == 0)
                Counter = Interval;
            ItemStack.Add(itemID);
            GameObject.Find("Audio Put").GetComponent<AudioSource>().Play();
        }
    }

    public bool IsItemAllowed(int itemID)
    {
        switch (BuildingID)
        {
            case Global.BUILDING_CHARCOAL_KILN: return itemID == Global.ITEM_WOOD;
            case Global.BUILDING_FURNACE: return itemID == Global.ITEM_COAL
                    || itemID == Global.ITEM_COPPER_ORE
                    || itemID == Global.ITEM_IRON_ORE
                    || itemID == Global.ITEM_GOLD_ORE
                    || itemID == Global.ITEM_TITANIUM_ORE;
        }
        return false;
    }

    public void Drop(int itemID, int count)
    {
        var velocity = new Vector3(1f, 1f, 0f);
        velocity = velocity.normalized;
        velocity = Quaternion.Euler(0, Random.Range(0, 360), 0) * velocity;
        for (var i = 0; i < count; i++)
            Level.GetComponent<Level>().SpawnItem(itemID, transform.position + new Vector3(0f, 3f, 0f), velocity * 5f);
    }

    public void Produce()
    {
        var produced = false;
        switch (BuildingID)
        {
            case Global.BUILDING_CHARCOAL_KILN: produced = ProduceCharcoalKiln(); break;
            case Global.BUILDING_FURNACE: produced = ProduceFurnace(); break;
        }
        if (produced)
            AudioSource.PlayClipAtPoint(GetComponent<Hit>().AudioSource.clip, transform.position);
    }

    private bool ProduceCharcoalKiln()
    {
        if (ItemStack.Count == 0)
            return false;
        ItemStack.RemoveAt(0);
        Drop(Global.ITEM_COAL, 1);
        return true;
    }

    private bool ProduceFurnace()
    {
        var coalIndex = -1;
        var oreIndex = -1;
        for (var i = 0; i < ItemStack.Count; i++)
        {
            if (coalIndex != -1 && oreIndex != -1)
                break;
            if (coalIndex == -1 && ItemStack[i] == Global.ITEM_COAL)
                coalIndex = i;
            if (oreIndex == -1 && (
                ItemStack[i] == Global.ITEM_COPPER_ORE
                || ItemStack[i] == Global.ITEM_IRON_ORE
                || ItemStack[i] == Global.ITEM_GOLD_ORE
                || ItemStack[i] == Global.ITEM_TITANIUM_ORE)
                )
                oreIndex = i;
        }
        if (coalIndex != -1 && oreIndex != -1)
        {
            var oreID = ItemStack[oreIndex];
            var indices = new List<int>() { coalIndex, oreIndex };
            DestroyIndices(indices);
            var ingotID = Global.ITEM_COPPER_INGOT;
            switch (oreID)
            {
                case Global.ITEM_COPPER_ORE: ingotID = Global.ITEM_COPPER_INGOT; break;
                case Global.ITEM_IRON_ORE: ingotID = Global.ITEM_IRON_INGOT; break;
                case Global.ITEM_GOLD_ORE: ingotID = Global.ITEM_GOLD_INGOT; break;
                case Global.ITEM_TITANIUM_ORE: ingotID = Global.ITEM_TITANIUM_INGOT; break;
            }
            Drop(ingotID, 1);
            return true;
        }
        return false;
    }

    private void DestroyIndices(List<int> indices)
    {
        indices.Sort();
        for (var i = indices.Count - 1; i >= 0; i--)
            ItemStack.RemoveAt(i);
    }

}
