using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WorldJobsListingUIController : MonoBehaviour
{
    [Header("Components:")]
    [SerializeField] private GameObject jobsListingUI;
    [SerializeField] private Button closeButton;
    [Header("Events:")]
    [SerializeField] private GameEvent onJobsListingUIClosed;

    [Header("Listeners:")]
    [SerializeField] private GameEventListener onJobsBoardInteracted;
    [SerializeField] private GameEventListener onJobsBoardReleased;
    [SerializeField] private GameEventListener_ZoneData onGameSceneLoaded;

    private List<JobListingButton> zoneButtons = new List<JobListingButton>();

    private void Awake()
    {
        onJobsBoardInteracted.Response.AddListener(() => ToggleUIPanel(true));
        onJobsBoardReleased.Response.AddListener(() => ToggleUIPanel(false));
        onGameSceneLoaded.Response.AddListener(DisableZoneButton);

        foreach (JobListingButton button in jobsListingUI.GetComponentsInChildren<JobListingButton>(true))
        {
            zoneButtons.Add(button);
        }

        closeButton.onClick.AddListener(() => ToggleUIPanel(false));
    }

    private void ToggleUIPanel(bool visible)
    {
        jobsListingUI.SetActive(visible);

        if(!visible)
        onJobsListingUIClosed.Raise();
    }
    private void DisableZoneButton(ZoneData zoneData)
    {
        ZoneData buttonData;
        for (int i = 0; i < zoneButtons.Count; i++)
        {
            buttonData = zoneButtons[i].GetZoneData();
            if (buttonData == null) continue;

            if (buttonData.levelName == zoneData.levelName)
                zoneButtons[i].ToggleInteractable(false);
            else
                zoneButtons[i].ToggleInteractable(true);
        }
    }
}
