using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

[RequireComponent(typeof(Enemy))]
class EnemyDrop : MonoBehaviour
{
    // [chance of drop, dropped item]
    [SerializeField]
    List<IndexValuePair<int, Item>> DropList = new List<IndexValuePair<int, Item>>();


    public void Drop()
    {
        if (DropList.Count == 0)
            return;

        // 1. Decide if element will be dropped
        for(int i=0; i < DropList.Count; i++)
        {
            if (UnityEngine.Random.Range(0.0f, 1.0f) <= DropList[i].Key / 100f)
            {
                var sceneGui = GameObject.FindGameObjectWithTag("ItemCollection");

                if (sceneGui == null)
                {
                    throw new Exception("GUI not found on scene!!!");
                    break;
                }

                var x = gameObject.transform.position.x + UnityEngine.Random.Range(-0.8f, 0.8f);
                var y = gameObject.transform.position.y + UnityEngine.Random.Range(-0.8f, 0.8f);

                var newItem = Instantiate(DropList[i].Value.ItemModel, new Vector3(x, y, 10), Quaternion.identity, sceneGui.transform);
                newItem.transform.localScale = new Vector3(1, 1, 1);

                newItem.name = DropList[i].Value.name;


                SceneEquippableItemManager.Instance.AddDynamicItem(newItem);

                //newItem.GetComponent<PickableController>().itemName = DropList[i].Value.ItemModel.name;
            }

        }

        // 2. Build on map

        // 2.1 Generate coords
    }
}