using System;
using RebootKit.Engine.Network;
using UnityEngine;
using Logger = RebootKit.Engine.Foundation.Logger;

namespace RebootKit.Engine.Main {
    public abstract class Game : MonoBehaviour {
        static readonly Logger s_GameLogger = new Logger(nameof(Game));

        [SerializeField] NetworkPlayerController m_PlayerControllerPrefab;

        public NetworkPlayerController LocalPlayerController { get; internal set; }

        protected virtual void Awake() {
            LocalPlayerController = Instantiate(m_PlayerControllerPrefab, transform);
        }
        
        //
        // @MARK: Server
        //
        protected virtual void OnPlayerBecameReady(ulong clientID) {
        }

        public virtual void OnClientConnected(ulong clientID) {
            s_GameLogger.Info($"Client {clientID} connected");
        }
        
        public virtual void OnClientDisconnected(ulong clientID) {
            s_GameLogger.Info($"Client {clientID} disconnected");
        }

        //
        // @MARK: Chat
        //
        protected virtual void OnChatMessage(string message) {
            s_GameLogger.Info($"Chat: {message}");
        }

        //
        // @MARK: Player Ready State
        //
        internal void PlayerBecameReady(ulong clientID) {
            if (!RR.IsServer()) {
                s_GameLogger.Error("PlayerBecameReady called on client, but this should only be called on the server.");
                return;
            }

            OnPlayerBecameReady(clientID);
        }
    }
}