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

public class JobListingButton : MonoBehaviour
{
    [Header("Data:")]
    [SerializeField] private ZoneData zoneData;
    [Header("Components:")]
    [SerializeField] private TMP_Text zoneName;
    [SerializeField] private Button button;
    [SerializeField] private GameObject interactableBlocker;
    [Header("Events:")]
    [SerializeField] private GameEvent_JobData onJobSelected;


    [Header("Runtime Data:")]
    public JobData jobData = new JobData();


    public ZoneData GetZoneData()
    {
        return zoneData;
    }

    private void Awake()
    {
        if (button == null)
            button = GetComponent<Button>();
        if (zoneName == null)
            zoneName = GetComponentInChildren<TMP_Text>();

        if (zoneData == null)
        {
            button.interactable = false;
            interactableBlocker.SetActive(!button.interactable);
            this.enabled = false;
            return;
        }

        jobData.zoneData = zoneData;

        zoneName.text = jobData.zoneData.zoneName;

        button.onClick.AddListener(SendSelectedJobData);
    }

    private void Update()
    {
        if (button == null) return;
        if (interactableBlocker == null) return;

        interactableBlocker.SetActive(!button.interactable);
    }
    public void ToggleInteractable(bool interactable)
    {
        button.interactable = interactable;
        interactableBlocker.SetActive(!button.interactable);
    }

    public void UpdateJobData(JobData jobData)
    {
        this.jobData.experienceFinalReward = jobData.experienceFinalReward;
        this.jobData.coinFinalReward = jobData.coinFinalReward;
    }

    private void SendSelectedJobData()
    {
        onJobSelected.Raise(jobData);
    }


}
