using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;

public class MainMenuManager : MonoBehaviour
{
    [SerializeField] private GameObject _mainMenuRoot = default;
    [SerializeField] private GameObject _creditsRoot = default;
    [SerializeField] private CameraControls _cameraControls = default;
    [SerializeField] private InputActionAsset _playerControls = default;

    [Header("Settings")]
    [SerializeField] private int _cameraGoToOnMenu = 1;

    [Header("Refs")]
    [SerializeField] private GameObject _mainMenuFirstButton = default;
    [SerializeField] private GameObject _creditsMenuFirstButton = default;
    [SerializeField] private GameObject _playButton = default;
    [SerializeField] private GameObject _resumeButton = default;

    private InputAction openMenuAction;
    private int previousCameraIndex;

    private void Start()
    {
        var playerControlsActionMap = _playerControls.FindActionMap("Player");
        openMenuAction = playerControlsActionMap.FindAction("OpenMenu");
    }

    public void PlayGame()
    {
        _playButton.SetActive(false);
        _resumeButton.SetActive(true);
        _mainMenuRoot.SetActive(false);
        openMenuAction.performed += InputShowMainMenu;
    }

    public void ResumeGame()
    {
        Debug.Log(previousCameraIndex);
        _cameraControls.ChangeCamera(previousCameraIndex);
        _mainMenuRoot.SetActive(false);
        openMenuAction.performed += InputShowMainMenu;
    }

    public void ResetGame()
    {
        SceneManager.LoadScene("MainScene");
    }

    public void ShowCredits()
    {
        _mainMenuRoot.SetActive(false);
        _creditsRoot.SetActive(true);
        openMenuAction.performed += InputShowMainMenu;
        EventSystem.current.SetSelectedGameObject(_creditsMenuFirstButton);
    }

    private void InputShowMainMenu(InputAction.CallbackContext context)
    {
        ShowMainMenu();
    }

    public void ShowMainMenu()
    {
        previousCameraIndex = _cameraControls.CurrentCameraIndex;
        Debug.Log(previousCameraIndex);
        _cameraControls.ChangeCamera(_cameraGoToOnMenu);
        _mainMenuRoot.SetActive(true);
        _creditsRoot.SetActive(false);
        openMenuAction.performed -= InputShowMainMenu;
        EventSystem.current.SetSelectedGameObject(_mainMenuFirstButton);
    }

    public void QuitGame()
    {
        Application.Quit();
    }

}
