using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

namespace Multiplayer
{

    public class PlayerServerManager
    {

        private ServerManager ServerManager;

        public PlayerServerManager(ServerManager ServerManager)
        {          
            this.ServerManager = ServerManager;
            RegisterHandlers();
        }

        private void RegisterHandlers()
        {
            ServerManager.Server.RegisterHandler(PlayerMsgID.Position, RecievePosition);
            ServerManager.Server.RegisterHandler(PlayerMsgID.QueryPosition, QueryPosition);
            ServerManager.Server.RegisterHandler(PlayerMsgID.Vegetable, PickUpVeg);
            ServerManager.Server.RegisterHandler(PlayerMsgID.GameWin, OnGameWin);
            ServerManager.Server.RegisterHandler(PlayerMsgID.GameOver, OnGameOver);
        }

        private void RecievePosition(NetworkMessage msg)
        {
            VectorMsg vectorMsg;
            if (!Utility.ReadMessage<VectorMsg>(msg, out vectorMsg))
                return;

            if (!ServerManager.AllPlayers.ContainsKey(vectorMsg.ID))
            {
                Debug.Log("Recieved position from unknown player");
                return;
            }

            Player player = ServerManager.AllPlayers[vectorMsg.ID];
            player.Position = vectorMsg.vector;

            foreach(Player otherPlayer in ServerManager.AllPlayers.Values)
            {
                if (otherPlayer.Equals(player))
                    continue;

                otherPlayer.Connection.Send(PlayerMsgID.Position, vectorMsg);
            }
        }

        private void QueryPosition(NetworkMessage msg)
        {
            PlayerMsg playerMsg;
            if (!Utility.ReadMessage<PlayerMsg>(msg, out playerMsg))
                return;

            if (!ServerManager.AllPlayers.ContainsKey(playerMsg.ID))
            {
                Debug.Log("Recieved query from unknown player");
                return;
            }

            Player player = ServerManager.AllPlayers[playerMsg.ID];

            foreach (Player otherPlayer in ServerManager.AllPlayers.Values)
            {
                if (otherPlayer.Equals(player))
                    continue;

                VectorMsg vectorMsg = new VectorMsg(otherPlayer.ID, otherPlayer.Position);
                player.Connection.Send(PlayerMsgID.Position, vectorMsg);
            }
        }

        private void OnGameWin(NetworkMessage msg)
        {
            foreach (Player player in ServerManager.AllPlayers.Values)
            {

                PlayerMsg winMsg = new PlayerMsg(player.ID);
                player.Connection.Send(PlayerMsgID.GameWin, winMsg);
            }
        }

        private void OnGameOver(NetworkMessage msg)
        {
            foreach (Player player in ServerManager.AllPlayers.Values)
            {

                PlayerMsg winMsg = new PlayerMsg(player.ID);
                player.Connection.Send(PlayerMsgID.GameOver, winMsg);
            }
        }

        private void PickUpVeg(NetworkMessage msg)
        {
            VegMsg vegMsg;
            if (!Utility.ReadMessage<VegMsg>(msg, out vegMsg))
                return;


            if (!ServerManager.AllPlayers.ContainsKey(vegMsg.ID))
            {
                Debug.Log("Recieved Veg from unknown player");
                return;
            }

            Player player = ServerManager.AllPlayers[vegMsg.ID];
 

            foreach (Player otherPlayer in ServerManager.AllPlayers.Values)
            {
                if (otherPlayer.Equals(player))
                    continue;

                otherPlayer.Connection.Send(PlayerMsgID.Vegetable, vegMsg);
            }

        }

    } 
}