using Nox.Core;
using Nox.Game;
using Jovian.SaveSystem;
using UnityEngine;
using UnityEngine.AddressableAssets;
using PlayMode = Nox.Core.PlayMode;

namespace Nox.UI {
    /// <summary>
    /// Main menu interface class which is responsible for creating the main menu view and handling the main menu events
    /// </summary>
    public class MainMenuView : IGameLifecycle {
        private readonly MenuPrefabsContainer menuPrefabsContainer;
        private readonly MenuGameStateData menuGameStateData;
        private readonly ISaveSystem saveSystem;
        private MainMenuReference mainMenuReference;
        public MainMenuView(MenuPrefabsContainer menuPrefabsContainer, MenuGameStateData menuGameStateData, ISaveSystem saveSystem) {
            this.menuPrefabsContainer = menuPrefabsContainer;
            this.menuGameStateData = menuGameStateData;
            this.saveSystem = saveSystem;
        }
        public void Initialize() {
            if(mainMenuReference == null) {
                mainMenuReference = Addressables.InstantiateAsync(menuPrefabsContainer.mainMenuReference).WaitForCompletion().GetComponent<MainMenuReference>();
            }
            mainMenuReference.exitButton.onClick.AddListener(Application.Quit);
            mainMenuReference.optionsButton.onClick.AddListener(() => {/* options logic here */});
            mainMenuReference.newGameButton.onClick.AddListener(() => menuGameStateData.startGameRequests?.Invoke(PlayMode.Adventure));

            bool hasSave = saveSystem.HasAnySaves();
            mainMenuReference.continueButton.gameObject.SetActive(hasSave);
            if(hasSave) {
                mainMenuReference.continueButton.onClick.AddListener(() => menuGameStateData.continueGameRequest.Invoke());
            }
        }
        public void Tick() { }
        public void Dispose() {
        }
    }
}
