﻿using AsmodeeDigital.Common.Plugin.Domain.Data;
using AsmodeeDigital.Common.Plugin.Manager.Random;
using AsmodeeDigital.Common.Plugin.Manager.Scene;
using AsmodeeDigital.PlayReal.Plugin.Network;
using UnityEngine;

namespace AsmodeeDigital.PlayReal.Plugin.Manager
{
    /// <summary>
    /// Manager of the PlayReal plugin
    /// </summary>
    public class PlayRealManager : MonoBehaviour
    {
        /// <summary>
        /// Singleton
        /// </summary>
        public static PlayRealManager Instance;

        /// <summary>
        /// Instance of the communication component with DoW server
        /// </summary>
        public ServerConnection ServerConnection;

        /// <summary>
        /// Instance of persistent data
        /// </summary>
        public Manager.Persistence.Persistence Persistence;

        /// <summary>
        /// Parameters of the connection with DoW server
        /// </summary>
        public DoWNetworkParameters NetworkParameters;

        /// <summary>
        /// Enabled the ServerConnection log
        /// </summary>
        public bool LogServerConnectionEnabled = true;

        /// <summary>
        /// Enabled the logic log
        /// </summary>
        public bool LogLogicEnabled = true;

        private void OnEnable()
        {
            if (Instance == null)
            {
                Instance = this;

                ServerConnection = new ServerConnection(NetworkParameters);
                Persistence = new Manager.Persistence.Persistence();

                ServerConnection.LogEnabled = LogServerConnectionEnabled;

                //--- Initialize all none MonoBehaviours managers
                SceneManager.Init(new SceneManagerUnity());
                RandomManager.Init(new RandomManagerUnity());
                //---
            }
        }

        private void OnApplicationQuit()
        {
            ServerConnection.Disconnect();
            ServerConnection.Dispose();
            ServerConnection = null;
        }
    }
}
