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

public class FirePlace : InteractableItem
{


	public InteractableItemSlider wood;
	public InteractableItemSlider stones;

	


	public GameObject fireLight;
	public GameObject[] fireParticles;

	public bool startFire = false;
	public bool fireStarted = false;

	public NavMeshObstacle obstacle;

	void Start()
	{
		obstacle.enabled = false;
	}

	void Update()
	{
		ConstructFireplace(wood.amount, stones.amount);
		if (wood.amount == wood.prefabs.Length && stones.amount == stones.prefabs.Length)
		{
			StartCoroutine("LightFire");
			obstacle.enabled = true;

		}
		else if (fireStarted)
		{
			ChangeIntensity();
		}
	}

	public void ConstructFireplace(int woodAmmount, int stoneAmmount)
	{

		for (int i = 0; i < woodAmmount; i++)
		{
			if (woodAmmount > wood.prefabs.Length) break;
			wood.prefabs[i].SetActive(true);
		}
		for (int i = woodAmmount; i < wood.prefabs.Length; i++)
		{
			wood.prefabs[i].SetActive(false);
		}
		for (int i = 0; i < stoneAmmount; i++)
		{
			stones.prefabs[i].SetActive(true);
		}
		for (int i = stoneAmmount; i < stones.prefabs.Length; i++)
		{
			stones.prefabs[i].SetActive(false);
		}
	}

	public void ChangeIntensity()
	{
		if (wood.amount != 0)
		{
			fireLight.SetActive(true);
			float temp = (float) wood.prefabs.Length / wood.amount;
			Debug.Log((int)temp);
			if ((int) temp == 1)
			{
				fireParticles[0].SetActive(true);
				fireParticles[1].SetActive(false);
				fireParticles[2].SetActive(false);
			}
			if ((int)temp == 2)
			{
				fireParticles[0].SetActive(false);
				fireParticles[1].SetActive(true);
				fireParticles[2].SetActive(false);
			}
			if ((int)temp == 3)
			{
				fireParticles[0].SetActive(false);
				fireParticles[1].SetActive(false);
				fireParticles[2].SetActive(true);
			}
			GameManager.instance.lightManager.lightIntensifier = temp;
		}
		else
		{
			fireLight.SetActive(false);
		}
	}

	private IEnumerator LightFire()
	{
		//GameManager.instance.lightManager.lightIntensifier = Mathf.Lerp(6f, 1f, 4f * Time.deltaTime);
		yield return new WaitForSeconds(1);
		fireLight.SetActive(true);
		
		fireParticles[0].SetActive(true);

		
		fireStarted = true;
	}

	public override bool IsItemNeeded(Item item)
	{
		if (!canAcceptItems) return false;
		int currAmount;
		if (item.Name == wood.name)
		{
			currAmount = wood.amount;
		}
		else if(item.Name == stones.name)
		{
			currAmount = stones.amount;
		}
		else
		{
			return false;
		}


		foreach (var i in GameManager.instance.itemRegister)
		{
			if(i.Name == craftableName)
			{
				foreach (var r in i.Recipe)
				{
					Debug.Log(r);
					if (r.Split(':')[0] == item.Name)
					{
						if (int.Parse(r.Split(':')[1]) > currAmount)
						{
							return true;
						}
					}
				}
			}
		}
		return false;
	}


	public override void TakeItem(Item item)
	{
		if(item.Name == wood.name)
		{
			wood.amount++;
		}
		else
		{
			stones.amount++;
		}
	}
}
