﻿using AsmodeeDigital.PlayReal.Plugin.Domain.Players;
using AsmodeeDigital.PlayReal.Plugin.Manager;
using AsmodeeDigital.PlayReal.Plugin.Utils;
using com.daysofwonder;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace AsmodeeDigital.PlayReal.Samples.UI.LobbyGame
{
    public class LobbyPlayerSlot : MonoBehaviour
    {
        [Serializable]
        public class UI
        {
            public DropSlot Slot;
            public Image Portrait;
            public InputField PlayerNameInputField;
            public Text PlayerName;
            public Toggle FirstPlayer;
            public Image AcceptIcon;
            public Image DeclineIcon;
        }

        public UI ui;

        public Player OnlinePlayer;
        public Color DefaultSlotColor;

        public LobbyPlayerSlotState State = LobbyPlayerSlotState.Empty;

        public void SetCurrentPlayerInSlot()
        {
            State = LobbyPlayerSlotState.ConnectedPlayer;

            this.gameObject.SetActive(true);

            Player player = PlayRealManager.Instance.Persistence.ConnectedPlayer;
            AvatarPortraitLoader.LoadPlayerAvatar(player, ui.Portrait);

            ui.PlayerNameInputField.gameObject.SetActive(false);
            ui.PlayerName.gameObject.SetActive(true);
            ui.Portrait.color = Color.white;

            ui.PlayerName.text = player.name;
        }

        public void SetPlayerFromDraggable()
        {
            this.gameObject.SetActive(true);

            if (ui.Slot.CurrentDraggable.CompareTag("DraggableLocalFriend"))
            {
                State = LobbyPlayerSlotState.LocalPlayer;

                ui.PlayerNameInputField.text = string.Empty;
                ui.PlayerNameInputField.gameObject.SetActive(true);
                ui.PlayerNameInputField.readOnly = false;
                ui.PlayerName.gameObject.SetActive(false);
            }
            else if (ui.Slot.CurrentDraggable.CompareTag("DraggableOnlineFriend"))
            {
                State = LobbyPlayerSlotState.OnlinePlayer;

                OnlinePlayer = ui.Slot.CurrentDraggable.Data as Player;

                ui.PlayerNameInputField.gameObject.SetActive(false);
                ui.PlayerName.gameObject.SetActive(true);
                ui.PlayerName.text = OnlinePlayer.name;
            }
            else
            {
                ui.PlayerNameInputField.gameObject.SetActive(false);
                ui.PlayerName.gameObject.SetActive(true);

                if (ui.Slot.CurrentDraggable.CompareTag("DraggableAIEasy"))
                {
                    State = LobbyPlayerSlotState.BotEasy;
                    ui.PlayerName.text = "AI Easy";
                }
                else if (ui.Slot.CurrentDraggable.CompareTag("DraggableAINormal"))
                {
                    State = LobbyPlayerSlotState.BotNormal;
                    ui.PlayerName.text = "AI Normal";
                }
                else if (ui.Slot.CurrentDraggable.CompareTag("DraggableAIHard"))
                {
                    State = LobbyPlayerSlotState.BotHard;
                    ui.PlayerName.text = "AI Hard";
                }
            }
        }

        public void SetPlayer(Player player)
        {
            State = LobbyPlayerSlotState.OnlinePlayer;

            OnlinePlayer = player;

            this.gameObject.SetActive(true);
            ui.PlayerNameInputField.gameObject.SetActive(false);
            ui.PlayerName.gameObject.SetActive(true);
            ui.PlayerName.text = OnlinePlayer.name;
            ui.Portrait.color = Color.white;

            AvatarPortraitLoader.LoadPlayerAvatar(player, ui.Portrait);
        }

        public void HideSlot()
        {
            State = LobbyPlayerSlotState.Hidden;

            this.gameObject.SetActive(false);

            ui.Portrait.sprite = null;
            ui.PlayerName.gameObject.SetActive(false);
            ui.PlayerNameInputField.gameObject.SetActive(false);
            ui.FirstPlayer.isOn = false;

            if (ui.Slot.CurrentDraggable != null)
                Destroy(ui.Slot.CurrentDraggable.gameObject);

            OnlinePlayer = null;
        }

        public void SetEmptySlot()
        {
            State = LobbyPlayerSlotState.Empty;

            this.gameObject.SetActive(true);

            ui.Portrait.sprite = null;
            ui.PlayerName.gameObject.SetActive(false);
            ui.PlayerNameInputField.gameObject.SetActive(false);
            ui.Portrait.color = DefaultSlotColor;
            ui.FirstPlayer.isOn = false;

            if (ui.AcceptIcon != null)
                ui.AcceptIcon.gameObject.SetActive(false);

            if (ui.DeclineIcon != null)
                ui.DeclineIcon.gameObject.SetActive(false);

            if (ui.Slot.CurrentDraggable != null)
                Destroy(ui.Slot.CurrentDraggable.gameObject);

            OnlinePlayer = null;
        }

        public IPlayerSeat GetPlayerSeat()
        {
            IPlayerSeat playerSeat = null;

            switch (State)
            {
                case LobbyPlayerSlotState.Empty:
                    break;
                case LobbyPlayerSlotState.ConnectedPlayer:
                    playerSeat = new LocalPlayerSeat(PlayRealManager.Instance.Persistence.ConnectedPlayer);

                    break;
                case LobbyPlayerSlotState.LocalPlayer:
                    playerSeat = new LocalPlayerSeat(0, ui.PlayerName.text);
                    break;
                case LobbyPlayerSlotState.OnlinePlayer:
                    playerSeat = new DistantPlayerSeat(OnlinePlayer, false);
                    break;
                case LobbyPlayerSlotState.BotEasy:
                    playerSeat = new RobotPlayerSeat(0, "Easy bot");

                    break;
                case LobbyPlayerSlotState.BotNormal:
                    playerSeat = new RobotPlayerSeat(0, "Normal bot");

                    break;
                case LobbyPlayerSlotState.BotHard:
                    playerSeat = new RobotPlayerSeat(0, "Hard bot");

                    break;
                default:
                    break;
            }

            return playerSeat;
        }

        public bool IsInvited()
        {
            return State != LobbyPlayerSlotState.Hidden && State != LobbyPlayerSlotState.Empty && State != LobbyPlayerSlotState.ConnectedPlayer;
        }
    }

    public enum LobbyPlayerSlotState
    {
        Hidden,
        Empty,
        ConnectedPlayer,
        LocalPlayer,
        OnlinePlayer,
        BotEasy,
        BotNormal,
        BotHard,
    }
}
