using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using ScrambleKit.Extensions;

public class ItemPlacer : MonoBehaviour 
{
    public string Name;
    public ChunkLibrary Items;
    public int ItemCount = 1;
    public float BonusChance = 0.25f;

    private List<Transform> spawnSlots = new List<Transform>();

    public void PlaceItems()
    {
        spawnSlots.Clear();
        spawnSlots.AddRange(GameObject.FindGameObjectsWithTag("SpawnSlot").Select(go => go.transform));

        int bonus = (Random.Range(0f, 1f) < BonusChance) ? 1 : 0;
        if (Score.Depth >= 7)
            bonus++;

        for (int i = 0; i < ItemCount + bonus; i++)
            PlaceItem();
    }

    private void PlaceItem()
    {
        Transform slot = spawnSlots.DrawFromAtRandom();
        spawnSlots.Remove(slot);

        Instantiate(Items.ChunkPrefabs.DrawFromAtRandom(), slot.position, Quaternion.identity);
        Destroy(slot.gameObject);
    }
}
