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

public class Recipe : MonoBehaviour {

    Player localPlayer;
    int PlayerCount;
    public Vegetable[] Veggies;
    public Frame[] Frames;

    public int[] Qtys;
    public int[] heldQtys;

    public GameObject RecipeCanvas;

    public Image Inventory;

	// Use this for initialization
	void Start () {
		for (int i = 0; i < PlayerCount; i++)
        {
            int rand = UnityEngine.Random.Range(0, Qtys.Length - 1);
            Qtys[rand] += 1;
        }
        for (int i = 0; i < 4; i++) //Set the recipe UI
        {
            Frames[i].VegetableSpot.sprite = Veggies[i].Image;
            Frames[i].Qty.text = Qtys[i] + "";
        }
        localPlayer = Multiplayer.PlayersManager.Instance.LocalPlayer.GetComponent<Player>();
        PlayerCount = Multiplayer.PlayersManager.Instance.RemotePlayers.Count + 1;


    }

    //Button calls
    public void OpenRecipe()
    {
        RecipeCanvas.SetActive(true);
    }

    public void CloseRecipe()
    {
        RecipeCanvas.SetActive(false);
    }

    //Veggie interactions
    public void pickupItem(Vegetable newVeggie)
    {
        localPlayer.heldVeggie = newVeggie;
        Inventory.gameObject.SetActive(true);
        Inventory.sprite = newVeggie.Image;
    }

    public void dropItem()
    {
        localPlayer.heldVeggie = null;
        Inventory.gameObject.SetActive(false);
    }
    public void friendlyPickup(string VeggieName, int change)
    {
        StartCoroutine(ItemLookup(VeggieName, change));
    }

    IEnumerator ItemLookup(string VeggieName, int change)
    {
        for (int i = 0; i < Veggies.Length; i++)
        {
            if(Veggies[i].Name == VeggieName)
            {
                heldQtys[i] += change;
            }
        }
        yield return new WaitForEndOfFrame();
    }

    public bool CheckVictory()
    {
        bool winning = true;
        for (int i = 0; i < Veggies.Length; i++)
        {
            if (Qtys[i] != heldQtys[i])
            {
                winning = false;
            }
        }
        return winning;
    }
}



[Serializable]
public class Vegetable
{
    public string Name;
    public Sprite Image;
}

[Serializable]
public class Frame
{
    public Image VegetableSpot;
    public TextMeshProUGUI Qty;
}
