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_JobInstance onJobSelected;


    [Header("Runtime Data:")]
    public JobInstance jobInstance = new JobInstance();


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

        jobInstance.SetupZone(zoneData.zoneName, zoneData.levelName, zoneData.isHostileZone);

        zoneName.text = jobInstance.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(JobInstance jobInstance)
    {
        this.jobInstance = jobInstance;
        this.jobInstance.coinReward = jobInstance.coinReward;
        this.jobInstance.experienceReward = jobInstance.experienceReward;
        this.jobInstance.reputationReward = jobInstance.reputationReward;
    }

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

#if UNITY_EDITOR
    private void OnValidate()
    {
        if (zoneData != null)
            this.gameObject.name = zoneData.name + "-Button";
    }
#endif
}
