using Kryz.CharacterStats.Examples;
using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DropTable : MonoBehaviour
{
    [SerializeField] private GameObject interactableDropPrefab;
    [SerializeField] private GameObject coinPrefab;
    [SerializeField] private int coinAmount;
    [SerializeField] private int extraDropChance;

    [SerializeField] private GameEvent_Int onCoinDrop;

    public List<Drop> guaranteedDrops = new List<Drop>();
    public List<EquippableItem> extraDrops = new List<EquippableItem>();

    PhotonView photonView;
    GameObject instantiatedDrop;
    EquippableItemDrop itemDrop;
    GameObject instantiatedCoinDrop;
    CoinDrop coinDrop;
    int finalCoinAmount;

    Vector3 spawnPosition;

    private void Awake()
    {
        photonView = GetComponentInParent<PhotonView>();
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    public bool CalculateLootDrop()
    {
        return Random.Range(0, 100) < (extraDropChance + 10 * GameDifficultyController.Instance.GetCurrentDifficultyLevel());
    }

    public void DropLoot(bool lootDrop)
    {
        spawnPosition = this.transform.position;
        spawnPosition.y = 0f;

        instantiatedCoinDrop = Instantiate(coinPrefab, spawnPosition, this.transform.rotation);
        coinDrop = instantiatedCoinDrop.GetComponent<CoinDrop>();

        finalCoinAmount = coinAmount * GameDifficultyController.Instance.GetCurrentDifficultyLevel();

        coinDrop.Init(finalCoinAmount);

        onCoinDrop.Raise(finalCoinAmount);

        for (int i = 0; i < guaranteedDrops.Count; i++)
        {
            guaranteedDrops[i].ExecuteDrop(this.transform.parent);
        }

        if (!lootDrop) return;
        if (extraDrops.Count <= 0) return;

        spawnPosition = this.transform.position;
        spawnPosition.y = 0f;
        instantiatedDrop = Instantiate(interactableDropPrefab, spawnPosition, this.transform.rotation);
        itemDrop = instantiatedDrop.GetComponent<EquippableItemDrop>();
        itemDrop.itemDrop = extraDrops[Random.Range(0, extraDrops.Count)];

    }
}
