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

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

    [Header("Listeners:")]
    [SerializeField] private GameEventListener onJobsBoardInteracted;
    [SerializeField] private GameEventListener onJobsBoardReleased;
    [SerializeField] private GameEventListener_ZoneData onGameSceneLoaded;
    [SerializeField] private GameEventListener onDifficultyChanged;
    [SerializeField] private GameEventListener onAvailableJobsUpdated;
    [SerializeField] private GameEventListener_JobInstance onJobCompleted;
    [SerializeField] private GameEventListener onMultiJobBonusRetrieved;

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

    JobRewardSettings jobRewardSettings;

    private int jobsCompletedCount = 0;

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

        SetupButtons();

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

        onJobCompleted.Response.AddListener(UpdateOnJobComplete);

        onMultiJobBonusRetrieved.Response.AddListener(UpdateOnBonusRetrieved);

        jobsCompletedCount = 0;
        multiJobBonusClaimButton.interactable = false;
        multiJobBonusClaimButton.onClick.AddListener(() => onMultiJobBonusRetrievedEvent.Raise());
    }

    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);
        }
    }

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

        foreach (JobCountMark jobCount in jobsCountUIParent.GetComponentsInChildren<JobCountMark>(true))
        {
            jobsCount.Add(jobCount);
            jobCount.SetCheckMarkState(false);
        }
    }

    private void UpdateOnJobComplete(JobInstance jobCompleted)
    {
        jobsCompletedCount++;
        if (jobsCompletedCount >= 3)
        {
            multiJobBonusClaimButton.interactable = true;
            jobsCompletedCount = 0;

            for (int i = 0; i < jobsCount.Count; i++)
            {
                jobsCount[i].SetCheckMarkState(true);
            }
            return;
        }
        for (int i = 0; i < jobsCompletedCount; i++)
        {
            jobsCount[i].SetCheckMarkState(true);
        }
        for (int i = jobsCompletedCount; i < jobsCount.Count; i++)
        {
            jobsCount[i].SetCheckMarkState(false);
        }
        multiJobBonusClaimButton.interactable = false;
    }

    private void UpdateOnBonusRetrieved()
    {
        for (int i = 0; i < jobsCount.Count; i++)
        {
            jobsCount[i].SetCheckMarkState(false);
        }
        jobsCompletedCount = 0;
    }

    private void UpdateJobsOnButtons()
    {
        foreach (JobListingButton button in zoneButtons)
        {
            if (button.GetZoneData() == null) continue;

            button.UpdateJobData(JobManager.Instance.availableJobs.Find(job => job.zoneName == button.GetZoneData().zoneName));
        }
    }
}
