using System;
using Unity.VisualScripting;
using UnityEngine;

namespace Cogwheel
{
    public class CogwheelInitializer : MonoBehaviour
    {

        private ICommandsManager _commandsManager;
        private CogwheelConsole _console;
        private CogwheelSettings _settings;
        
        private void Start()
        {
            DontDestroyOnLoad(this);
            if (!LoadSettings()) return;
            _console = GetComponentInChildren<CogwheelConsole>();
            _commandsManager = new CommandsManager(_console);
            COGWHEEL.Start(_settings, _commandsManager, _console);
        }

        private bool LoadSettings()
        {
            var settings = Resources.Load<CogwheelSettings>("CogwheelSettings");
            if (settings is null)
            {
                Debug.LogError("CogwheelSettings not found. Make sure you have a CogwheelSettings asset in a Resources folder.");
                return false;
            }
            
            _settings = settings;
            return true;
        }
    }
}