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

public class Lootbox : MonoBehaviour
{
    public List<GameObject> gunList;
    public List<WeaponInfo> weaponInfoScriptList;
    public GameObject buyEffect;
    public GameObject costDisplay;
    public float cost;
    private GameObject player;
    public WeaponInfo weapon;

    [Header("Rarity modifiers")]
    public float rudimentaryModifier;
    public float basicModifier;
    public float modernModifier;
    public float advancedModifier;
    public float futuristicModifier;
    private float maxWeight;

    private const string _Basic = "Basic";
    private const string _Rudimentary = "Rudimentary";
    private const string _Modern = "Modern";
    private const string _Advanced = "Advanced";
    private const string _Futuristic = "Futuristic";
    private string rarity;
    
    private void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        gunList = FindObjectOfType<LoadoutManager>().gunList;

        for (int i = 0; i < gunList.Count; i++)
        {
            weaponInfoScriptList.Add(gunList[i].GetComponent<WeaponInfo>());
        }

        for (int i = gunList.Count - 1; i >= 0; i--)
        {
            if (gunList[i].GetComponent<WeaponInfo>().isUnlocked == true)
            {
                gunList.RemoveAt(i);
            }
        }

        // float weightSum = 0f;
        // for (int i = 0; i < gunList.Count; ++i)
        // {
        //     weightSum += gunList[i].GetComponent<WeaponInfo>().rarityFloat;
        // }
        var sum = GetRadomSUM(gunList, maxRarityToSubtract: 30f /* the maximum value which 1 random can generate */);

        int indexOfTheRandomSUM = sum.Key;
        float valueOfTheRandomSUM = sum.Value;

        weapon = gunList[indexOfTheRandomSUM].GetComponent<WeaponInfo>();
    }

    private void Update()
    {
        float distance = Vector2.Distance(transform.position, player.transform.position);

        if (distance < 3.5f)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                if (player.GetComponent<MoneyManager>().money >= cost)
                {
                    player.GetComponent<MoneyManager>().money -= cost;
                    weapon.GetComponent<WeaponInfo>().isUnlocked = true;
                    weapon.GetComponent<WeaponInfo>().ChangeSprites();
                    Destroy(gameObject);
                }
            }
        }
    }

    public KeyValuePair<int, float> GetRadomSUM(List<GameObject> gunList, float maxRarityToSubtract, bool alwaysReturnAnItem = true)
    {
        Dictionary<int, float> randoms = new Dictionary<int, float>();
        for (int elements = 0; elements < gunList.Count; elements++)
        {
            float weight = gunList[elements].GetComponent<WeaponInfo>().rarityFloat;
            randoms.Add(elements, weight);
            maxWeight += weight;
        }

        bool _ = false;
        if (_ == false)
        {
            maxWeight = Random.Range(0, maxWeight);
            Debug.Log("wtf");
            _ = true;
        }

        for (int r = 0; r < randoms.Count; r++)
        {
            float weight = randoms[r];
            /*rarity = weaponInfoScriptList[r].rarity;

            switch (rarity)
            {
                case _Rudimentary:
                    weight -= rudimentaryModifier;
                    Debug.Log("rudimentary removed");
                    break;

                case _Basic:
                    weight -= basicModifier;
                    Debug.Log("basic removed");
                    break;

                case _Modern:
                    weight -= modernModifier;
                    Debug.Log("modern removed");
                    break;

                case _Advanced:
                    weight -= advancedModifier;
                    Debug.Log("advanced remobed");
                    break;

                case _Futuristic:
                    weight -= futuristicModifier;
                    Debug.Log("futuristic removed");
                    break;

                default:
                    Debug.Log("no rarity");
                    break;
            }*/

            Debug.Log(weight);
            maxWeight -= weight;
            Debug.Log("current weight is " + maxWeight);

            if (maxWeight < 0)
            {
                return new KeyValuePair<int, float>(r, maxWeight);
            }
        }

        if (alwaysReturnAnItem)
        {
            var bestItem = randoms.ToList().OrderBy(x => x.Value).FirstOrDefault();
            return bestItem;
        }

        return default;
    }
}