using ExitGames.Client.Photon;
using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class JobInfoPanel : MonoBehaviour, IOnEventCallback
{
    [Header("Components:")]
    [SerializeField] private TMP_Text title;
    [SerializeField] private TMP_Text description;
    [SerializeField] private TMP_Text coinReward;
    [SerializeField] private TMP_Text experienceReward;
    [SerializeField] private TMP_Text reputationReward;
    [SerializeField] private GameObject infoPanel;
    [SerializeField] private Button closeButton;

    [Header("Listeners:")]
    [SerializeField] private GameEventListener_JobInstance onJobSelected;
    [SerializeField] private GameEventListener onJobsBoardReleased;
    [SerializeField] private GameEventListener_Player onPlayerVoted;

    JobInstance networkJobInstance;

    private void Awake()
    {
        onJobSelected.Response.AddListener((job) => SetupJobInfoPanel(job, true));
        onJobSelected.Response.AddListener(Send_Others_OnJobSelected);
        onJobsBoardReleased.Response.AddListener(() => ToggleInfoPanel(false));
        onPlayerVoted.Response.AddListener((player) =>
        {
            if (player != PhotonNetwork.LocalPlayer)
                ToggleInfoPanel(true);
        });

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


    private void SetupJobInfoPanel(JobInstance jobData, bool showPanel)
    {
        if (jobData.showJobTitlePrefix)
            title.text = $"Job Title: {jobData.title}";
        else
            title.text = $"{jobData.title}";

        description.text = jobData.description;

        coinReward.text = jobData.coinReward.ToString();
        experienceReward.text = jobData.experienceReward.ToString();
        reputationReward.text = jobData.reputationReward.ToString();

        if (showPanel)
            ToggleInfoPanel(true);
    }

    public void ToggleInfoPanel(bool visible)
    {
        infoPanel.SetActive(visible);
    }

    private void Send_Others_OnJobSelected(JobInstance jobInstance)
    {
        //if (PhotonNetwork.CurrentRoom.PlayerCount <= 1) return;

        networkJobInstance = jobInstance;

        string jsonJobData = JsonUtility.ToJson(networkJobInstance);
        Debug.Log($"JobData: {jsonJobData}");

        RaiseEventOptions raiseEventOptions = new RaiseEventOptions { Receivers = ReceiverGroup.Others };
        PhotonNetwork.RaiseEvent(GameConstants.NetworkEventCodes.JobSelection, jsonJobData, raiseEventOptions, SendOptions.SendReliable);
    }

    public void OnEvent(EventData photonEvent)
    {
        if (photonEvent.Code == GameConstants.NetworkEventCodes.JobSelection)
        {
            string jsonJobData = (string)photonEvent.CustomData;

            Debug.Log("Job selected: " + jsonJobData);

            SetupJobInfoPanel(JsonUtility.FromJson<JobInstance>(jsonJobData), false);
            JobManager.Instance.UpdateJobSelection(JsonUtility.FromJson<JobInstance>(jsonJobData));
        }
    }


    public void OnEnable()
    {
        PhotonNetwork.AddCallbackTarget(this);
    }

    public void OnDisable()
    {
        PhotonNetwork.RemoveCallbackTarget(this);
    }
}
