using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public sealed class WinScreenController : MonoBehaviour
{
    [SerializeField]
    private Profile profile = null;
    [SerializeField]
    private Button newGameButton = null;
    [SerializeField]
    private Button exitButton = null;
    [SerializeField]
    private Text statusText = null;
    [SerializeField]
    private string menuSceneName = "game";

    private void StartNewGame()
    {
        profile.ClearPlayer();
        SceneManager.LoadScene(menuSceneName);
    }

    private void ExitGame()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#endif
        Application.Quit();
    }


    private void Start()
    {
        newGameButton.onClick.AddListener(StartNewGame);
        exitButton.onClick.AddListener(ExitGame);

        SetStatus(profile.SavedPlayer);
    }

    private void OnDestroy()
    {
        newGameButton.onClick.AddListener(StartNewGame);
        exitButton.onClick.AddListener(ExitGame);
    }

    private void SetStatus(Player player)
    {
        if (player == null)
        {
            statusText.text = string.Empty;
            return;
        }
        var stantion = player.CurrentPoint.Stantion;
        statusText.text =
            $"pilot: {player.Name}\n{stantion.Name} ({stantion.TypeId})\ncredits: {player.Credits}\n" +
            $"There is another wonderfull statistics must be here, but i have no time.";
    }
}
