using Darkmatter.Core;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using VContainer;

namespace Darkmatter.Presentation
{
    public class DeathScreenController : IDeathScreenController
    {
        private DeathScreenView deathScreenView;

        [Inject] private IScoreService IscoreService;
        [Inject] private IInputReader IinputReader;
        [Inject] private IAudioController IaudioController;
        [Inject] private IGameSession IgameSession;
        [Inject] private IGameScreenController IgameScreenController;
        [Inject] private ILeaderBoardController IleaderBoardController;
        public DeathScreenController(DeathScreenView _deathScreenView)
        {
            deathScreenView = _deathScreenView;
            deathScreenView.restartBtn.onClick.AddListener(OnRestartButtonClicked);
            deathScreenView.exitBtn.onClick.AddListener(OnExitButtonClicked);
            deathScreenView.leaderBoardBtn.onClick.AddListener(OnLeaderBoardBtnClicked);
        }

        private void OnLeaderBoardBtnClicked()
        {
            IaudioController.PlayBtnPressedSound();
            IleaderBoardController.ShowLeaderBoardAsync();
        }

        public void ShowDeathScreen()
        {
            IgameScreenController.HideGameScreen();
            IleaderBoardController.UpdateLeaderBoardScore(IscoreService.score); //update leaderboard with current score when player dies
            deathScreenView.Show(IscoreService.score, IscoreService.highScore);
        }

        private void OnExitButtonClicked()
        {
            Debug.Log("Exit Button Clicked");
            IinputReader.UnlockInput();
            IaudioController.PlayBtnPressedSound();
            IgameSession.showStartScreen = true;
            SceneManager.LoadScene(0);
        }

        private void OnRestartButtonClicked()
        {
            Debug.Log("Restart Button Clicked");
            IinputReader.UnlockInput();
            IaudioController.PlayBtnPressedSound();
            IgameSession.showStartScreen = false;
            SceneManager.LoadScene(0);
        }
    }
}
