using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Logic {
    [Serializable]
    public sealed class Item {
        [JsonProperty("id")]
        public string id;
        [JsonProperty("val")]
        public int value;

        public Item(string itemId, int itemValue = 0) {
            id = itemId;
            value = itemValue;
        }
    }

    [Serializable]
    public class Container {
        [SerializeField]
        [JsonProperty("items_list")]
        private List<Item> items = new List<Item>();

        public int this[string itemId] {
            get { return GetItem(itemId); }
            set { SetItem(itemId, value); }
        }

        const string randomId = "random";

        public void SetItems(Dictionary<string, int> items) {
            if (items != null) {
                foreach (var item in items) {
                    SetItem(item.Key, item.Value);
                }
            }
        }

        public int GetItem(string itemId) {
            if (itemId == String.Empty) {
                return 0;
            }
            if (itemId == randomId) {
                return UnityEngine.Random.Range(0, 100);
            }
            foreach (var item in items) {
                if (item.id == itemId) {
                    return item.value;
                }
            }
            return 0;
        }

        public void SetItem(string itemId, int count = 0) {
            foreach (var item in items) {
                if (item.id == itemId) {
                    item.value = count;
                    return;
                }
            }
            items.Add(new Item(itemId, count));
        }

        internal void SetItems(object defaultPlanetarParams) {
            throw new NotImplementedException();
        }
    }
}
