using UnityEngine;
using System;
using System.Collections.Generic;
using Octopus.Simulator.Networks;
using Newtonsoft.Json;
using UnityEngine.UI;
using System.Collections;

namespace Octopus.Simulator
{
    public class LogicDataManager : MonoBehaviour
    {
        public SimulationData currentInfo;
        [SerializeField]
        Button logicPrefab;
        [SerializeField]
        RectTransform LogicWindow;

        Dictionary<Button, ILogicItem> logicButtonDict = new Dictionary<Button, ILogicItem>();
        public event Action<ILogicItem> OnLogicButtonClicked;
        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            RequestInfo();
        }

        void ClearLogicBTN()
        {
            foreach (var b in logicButtonDict)
            {
                Destroy(b.Key.gameObject);
            }
            logicButtonDict.Clear();
        }

        void RequestInfo()
        {
            ClearLogicBTN();
            WebManager.webManager.Request_Get($"{WebManager.webManager.apiConfig.history}/1", (flag, value) =>
            {
                if (flag)
                {
                    Debug.Log(value);
                    var info = JsonConvert.DeserializeObject<SimulationInfo>(value);
                    GetInfo(info.data);
                }
            });
        }

        void GetInfo(SimulationData info)
        {
            currentInfo = info;
            QueueVisulization(info.logicData.queues);
            ResourceVisulization(info.logicData.resources);
            ComponentVisulization(info.logicData.components);
        }

        void QueueVisulization(List<LogicQueue> queues)
        {
            int i = 0;
            foreach (var q in queues)
            {
                var l = Instantiate(logicPrefab, LogicWindow);
                var item = l.GetComponent<LogicItemButton>();
                item.Setup(q);
                logicButtonDict.Add(l, q);
                l.onClick.AddListener(() => OnLogicButtonClick(q));
                l.GetComponent<RectTransform>().anchoredPosition = new Vector2(-500, -i * 65 - 50);
                i++;
            }
        }

        void ResourceVisulization(List<LogicResource> resources)
        {
            int i = 0;
            foreach (var r in resources)
            {
                var l = Instantiate(logicPrefab, LogicWindow);
                var item = l.GetComponent<LogicItemButton>();
                item.Setup(r);
                logicButtonDict.Add(l, r);
                l.onClick.AddListener(() => OnLogicButtonClick(r));
                l.GetComponent<RectTransform>().anchoredPosition = new Vector2(-100, -i * 65 - 50);
                i++;
            }
        }

        void ComponentVisulization(List<LogicComponent> components)
        {
            int i = 0;
            foreach (var c in components)
            {
                var l = Instantiate(logicPrefab, LogicWindow);
                var item = l.GetComponent<LogicItemButton>();
                item.Setup(c);
                logicButtonDict.Add(l, c);
                l.onClick.AddListener(() => OnLogicButtonClick(c));
                l.GetComponent<RectTransform>().anchoredPosition = new Vector2(200, -i * 65 - 50);
                i++;
            }
        }

        void OnLogicButtonClick(ILogicItem item)
        {
            OnLogicButtonClicked?.Invoke(item);
        }
    }
}