﻿using AsmodeeDigital.Common.Samples.UI;
using AsmodeeDigital.PlayReal.Samples.Logic;
using AsmodeeDigital.PlayReal.Samples.Views;
using com.daysofwonder;
using System;
using UnityEngine.UI;

namespace AsmodeeDigital.PlayReal.Samples.UI.LobbyPlayers
{
    public class PopupBuddy : Popup
    {
        /// <summary>
        /// UI references of PopupBuddy
        /// </summary>
        [Serializable]
        public class UIPopupBuddy
        {
            public Button AddToBuddyListButton;
            public Button AddToIgnoreListButton;
            public Button RemoveFromBuddyListButton;
            public Button RemoveFromIgnoreListButton;
        }

        public UIPopupBuddy uiPopupBuddy;

        private int _dowId;

        public void Show(int DoWId)
        {
            _dowId = DoWId;

            this.Show();

            IPlayer player = LobbyPlayersView.Instance.GetBuddy(DoWId);

            if (player == null)
            {
                Hide();
                return;
            }

            ui.Title.text = player.name;

            BuddyStatus buddyStatus = LobbyPlayersView.Instance.GetBuddyStatus(DoWId);

            switch (buddyStatus)
            {
                case BuddyStatus.None:
                    uiPopupBuddy.AddToBuddyListButton.gameObject.SetActive(true);
                    uiPopupBuddy.AddToIgnoreListButton.gameObject.SetActive(true);
                    uiPopupBuddy.RemoveFromBuddyListButton.gameObject.SetActive(false);
                    uiPopupBuddy.RemoveFromIgnoreListButton.gameObject.SetActive(false);
                    break;
                case BuddyStatus.Friend:
                    uiPopupBuddy.AddToBuddyListButton.gameObject.SetActive(false);
                    uiPopupBuddy.AddToIgnoreListButton.gameObject.SetActive(false);
                    uiPopupBuddy.RemoveFromBuddyListButton.gameObject.SetActive(true);
                    uiPopupBuddy.RemoveFromIgnoreListButton.gameObject.SetActive(false);
                    break;
                case BuddyStatus.Ignored:
                    uiPopupBuddy.AddToBuddyListButton.gameObject.SetActive(false);
                    uiPopupBuddy.AddToIgnoreListButton.gameObject.SetActive(false);
                    uiPopupBuddy.RemoveFromBuddyListButton.gameObject.SetActive(false);
                    uiPopupBuddy.RemoveFromIgnoreListButton.gameObject.SetActive(true);
                    break;
                default:
                    break;
            }
        }

        public override void Hide()
        {
            LobbyPlayersView.Instance.ui.Blocker.gameObject.SetActive(false);
            this.gameObject.SetActive(false);
        }

        public void AddToBuddyList()
        {
            LobbyPlayersView.Instance.AddToBuddyList(_dowId);
            Hide();
        }

        public void AddToIgnoreList()
        {
            LobbyPlayersView.Instance.AddToIgnoreList(_dowId);
            Hide();
        }

        public void RemoveFromBuddyList()
        {
            LobbyPlayersView.Instance.RemoveFromBuddyList(_dowId);
            Hide();
        }

        public void RemoveFromIgnoreList()
        {
            LobbyPlayersView.Instance.RemoveFromIgnoreList(_dowId);
            Hide();
        }
    }
}