using Managers;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UI.Employee
{
    public class EmployeePanel : MonoBehaviour
    {
        [SerializeField] private GameObject panel;
        [SerializeField] private EmployeeHiredDetails ourWorkersDetails;
        [SerializeField] private EmployeeHireDetails hireReadyDetails;
        [Header("Generation")]
        [SerializeField] private GameObject workerRow;
        [SerializeField] private Transform ourWorkerContainer;
        [SerializeField] private Transform hireWorkerContainer;

        GameManager manager;

        void Start()
        {
            manager = GameManager.Instance;
        }

        public void ShowPanel(bool v)
        {
            Sound.SoundController.Instance.Play(SoundType.UIClick);
            panel.SetActive(v);

            if (v)
                Generate();

            if (!v)
            {
                ourWorkersDetails.Show(false);
                hireReadyDetails.Show(false);
            }
        }

        private void Generate()
        {
            GenerateOurWorkers();
            GenerateHireWorkers();
        }

        private void GenerateOurWorkers()
        {
            ResetContainer(ourWorkerContainer);

            var employees = manager.OurEmployees;

            foreach (var item in employees)
            {
                var row = Instantiate(workerRow, ourWorkerContainer, false);
                row.GetComponent<EmployeeInfoButton>().Init(item.Data, item.Icon, delegate
                {
                    Sound.SoundController.Instance.Play(SoundType.UIClick);
                    hireReadyDetails.Show(false);
                    ourWorkersDetails.Show();

                    ourWorkersDetails.Init(
                        item.Data, item.Icon, delegate
                        {
                            Sound.SoundController.Instance.Play(SoundType.UIClick);
                            manager.ReleaseOurWorker(item);
                            ourWorkersDetails.Show(false);
                            GenerateOurWorkers();
                        }, delegate
                        {
                            Sound.SoundController.Instance.Play(SoundType.UIClick);
                            manager.UpgradeEmployeeTime(item);
                            ourWorkersDetails.UpdateData(item.Data);
                        }, delegate
                        {
                            Sound.SoundController.Instance.Play(SoundType.UIClick);
                            manager.UpgradeEmployeeEvents(item);
                            ourWorkersDetails.UpdateData(item.Data);
                        }
                        );
                }
                );
            }
        }

        private void GenerateHireWorkers()
        {
            ResetContainer(hireWorkerContainer);

            var employees = manager.HireEmployees;

            foreach (var item in employees)
            {
                var row = Instantiate(workerRow, hireWorkerContainer, false);
                row.GetComponent<EmployeeInfoButton>().Init(item.Data, item.Icon, delegate
                {
                    Sound.SoundController.Instance.Play(SoundType.UIClick);
                    ourWorkersDetails.Show(false);
                    hireReadyDetails.Show();
                    hireReadyDetails.Init(
                    item.Data, item.Icon, delegate
                    {
                        manager.ReleaseOurWorker(item);
                        hireReadyDetails.Show(false);
                        GenerateHireWorkers();
                        Sound.SoundController.Instance.Play(SoundType.UIClick);
                    },
                    delegate
                    {
                        manager.HireEmployee(item);
                        hireReadyDetails.Show(false);
                        Generate();
                        Sound.SoundController.Instance.Play(SoundType.UIClick);
                    }
                    );
                });
            }
        }

        private void ResetContainer(Transform container)
        {
            foreach (Transform item in container)
                Destroy(item.gameObject);
        }

    }
}