using System;
using Unity.BossRoom.ConnectionManagement;
using Unity.BossRoom.UnityServices.Lobbies;
using Unity.BossRoom.Utils;
using Unity.Multiplayer.Samples.Utilities;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using UnityEngine;
using UnityEngine.SceneManagement;
using VContainer;

namespace UUnity.BossRoom.ConnectionManagement
{
    /// <summary>
    /// Connection state corresponding to when the NetworkManager is shut down. From this state we can transition to the
    /// ClientConnecting sate, if starting as a client, or the StartingHost state, if starting as a host.
    /// </summary>
    class OfflineState : ConnectionState
    {
        [Inject]
        LobbyServiceFacade m_LobbyServiceFacade;
        [Inject]
        ProfileManager m_ProfileManager;
        [Inject]
        LocalLobby m_LocalLobby;

        const string k_MainMenuSceneName = "MainMenu";

        public override void Enter()
        {
            m_LobbyServiceFacade.EndTracking();
            m_ConnectionManager.NetworkManager.Shutdown();
            if (SceneManager.GetActiveScene().name != k_MainMenuSceneName)
            {
                SceneLoaderWrapper.Instance.LoadScene(k_MainMenuSceneName, useNetworkSceneManager: false);
            }
        }

        public override void Exit() { }

        public override void StartClientIP(string playerName, string ipaddress, int port)
        {
            var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName);
            m_ConnectionManager.m_ClientReconnecting.Configure(connectionMethod);
            m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(connectionMethod));
        }

        public override void StartClientLobby(string playerName)
        {
            var connectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName);
            m_ConnectionManager.m_ClientReconnecting.Configure(connectionMethod);
            m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting.Configure(connectionMethod));
        }

        public override void StartHostIP(string playerName, string ipaddress, int port)
        {
            var connectionMethod = new ConnectionMethodIP(ipaddress, (ushort)port, m_ConnectionManager, m_ProfileManager, playerName);
            m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod));
        }

        public override void StartHostLobby(string playerName)
        {
            var connectionMethod = new ConnectionMethodRelay(m_LobbyServiceFacade, m_LocalLobby, m_ConnectionManager, m_ProfileManager, playerName);
            m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost.Configure(connectionMethod));
        }
        
        public override void StartServerIP(string ip, int port)
        {
            // Log input parameters to ensure they are correct
            Debug.Log($"StartServerIP called with IP: {ip}, Port: {port}");

            // Check if NetworkManager.Singleton is null
            if (NetworkManager.Singleton == null)
            {
                Debug.Log("NetworkManager.Singleton is null. Ensure a NetworkManager exists in the scene.");
                return;
            }

            // Check if NetworkConfig or NetworkTransport is null
            if (NetworkManager.Singleton.NetworkConfig == null)
            {
                Debug.Log("NetworkManager.Singleton.NetworkConfig is null. Ensure NetworkConfig is properly configured.");
                return;
            }

            if (NetworkManager.Singleton.NetworkConfig.NetworkTransport == null)
            {
                Debug.Log("NetworkManager.Singleton.NetworkConfig.NetworkTransport is null. Ensure NetworkTransport is properly configured.");
                return;
            }

            // Attempt to cast the transport to UnityTransport
            var utp = NetworkManager.Singleton.NetworkConfig.NetworkTransport as UnityTransport;
            if (utp == null)
            {
                Debug.Log("Failed to cast NetworkTransport to UnityTransport. Ensure UnityTransport is set up as the transport.");
                return;
            }

            // Log before setting connection data
            Debug.Log($"Setting UnityTransport connection data with IP: {ip}, Port: {port}");
            utp.SetConnectionData(ip, (ushort)port);

            // Check if ConnectionManager or m_ServerStarting is null
            if (m_ConnectionManager == null)
            {
                Debug.Log("m_ConnectionManager is null. Ensure it is properly assigned.");
                return;
            }

            if (m_ConnectionManager.m_ServerStarting == null)
            {
                Debug.Log("m_ServerStarting is null. Ensure all states are properly initialized in ConnectionManager.");
                return;
            }

            // Log state change
            Debug.Log("Changing state to m_ServerStarting.");
            m_ConnectionManager.ChangeState(m_ConnectionManager.m_ServerStarting);
        }

    }
}