using Photon.Pun;
using Photon.Realtime;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PartyFramesController : MonoBehaviour
{
    [SerializeField] private GameObject partyFrameButtonPrefab;
    [SerializeField] private GameObject partyFrameVotePrefab;
    [SerializeField] private Transform parentGrid;
    [SerializeField] private Transform voteParentGrid;
    [SerializeField] private GameEventListener_Player onPlayerJoinedParty;
    [SerializeField] private GameEventListener_Player onPlayerLeftParty;
    [SerializeField] private GameEventListener_Player onPlayerVoted;
    [SerializeField] private GameEventListener_Player onPlayerVoteCanceled;
    [SerializeField] private GameEventListener_PhotonView onPlayerSpawned;
    [SerializeField] private List<Sprite> classImages = new List<Sprite>();
    [SerializeField] private List<GameTag> classTags = new List<GameTag>();

    public Dictionary<Player, PartyFrameButton> partyFrameButtons = new Dictionary<Player, PartyFrameButton>();
    public Dictionary<Player, PartyVoteFrame> partyVoteFrames = new Dictionary<Player, PartyVoteFrame>();

    GameObject buttonGO;
    PartyFrameButton button;
    PartyVoteFrame frame;
    RiftPlayer riftPlayer;

    private void Awake()
    {
        onPlayerJoinedParty.Response.AddListener(AddNewPartyMemberFrame);
        onPlayerLeftParty.Response.AddListener(RemovePartyMemberFrame);
        onPlayerSpawned.Response.AddListener(UpdatePartyMemberFrame);
        onPlayerVoted.Response.AddListener(UpdateVoteCheckmark);
        onPlayerVoteCanceled.Response.AddListener(CancelVoteCheckmark);
    }

    public void AddNewPartyMemberFrame(Player player)
    {
        if (partyFrameButtons.ContainsKey(player)) return;

        buttonGO = Instantiate(partyFrameButtonPrefab, parentGrid);
        button = buttonGO.GetComponent<PartyFrameButton>();
        button.playerName.text = player.NickName;
        partyFrameButtons.Add(player, button);

        AddVoteAvatarFrame(player);
    }
    private void AddVoteAvatarFrame(Player player)
    {
        buttonGO = Instantiate(partyFrameVotePrefab, voteParentGrid);
        frame = buttonGO.GetComponent<PartyVoteFrame>();
        frame.playerName.text = player.NickName;
        partyVoteFrames.Add(player, frame);
    }

    public void RemovePartyMemberFrame(Player player)
    {
        if(partyFrameButtons.ContainsKey(player))
        {
            Destroy(partyFrameButtons[player].gameObject);
            partyFrameButtons.Remove(player);
        }
        if(partyVoteFrames.ContainsKey(player))
        {
            Destroy(partyVoteFrames[player].gameObject);
            partyVoteFrames.Remove(player);
        }
    }
    public void UpdatePartyMemberFrame(PhotonView photonView)
    {
        if(partyFrameButtons.ContainsKey(photonView.Owner))
        {
            riftPlayer = photonView.GetComponentInParent<RiftPlayer>();
            if (riftPlayer == null)
            {
                Debug.Log("Sujou");
                return;
            }
            Debug.Log("Player Class: " + riftPlayer.classTag.name);

            for (int i = 0; i < classTags.Count; i++)
            {
                if (riftPlayer.classTag == classTags[i])
                {
                    partyFrameButtons[photonView.Owner].button.image.sprite = classImages[i];
                    partyVoteFrames[photonView.Owner].avatarIcon.sprite = classImages[i];
                    partyVoteFrames[photonView.Owner].votedCheck.gameObject.SetActive(false);
                }
            }
        }
        else
        {
            AddNewPartyMemberFrame(photonView.Owner);
            UpdatePartyMemberFrame(photonView);
        }
    }

    private void UpdateVoteCheckmark(Player player)
    {
        if(partyVoteFrames.ContainsKey(player))
        {
            partyVoteFrames[player].votedCheck.gameObject.SetActive(true);
        }
    }
    private void CancelVoteCheckmark(Player player)
    {
        if (partyVoteFrames.ContainsKey(player))
        {
            partyVoteFrames[player].votedCheck.gameObject.SetActive(false);
        }
    }
}
