using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public GameObject junkObject, icon;
    public Inventory inventory;
    public InventorySlot[] inventorySlots;
    public Ship ship;
    public Engines engine;
    public List<SpaceObject> spaceObjects;
    public GameObject ejectForwardButton, ejectBackwardButton;
    public BlackHole blackHole;
    public CameraControl cameraControl;
    public EndPanel endPanel;
    public Text blackHoleDistanceText;
    private float blackHoleDistance;
    private bool hasEnded;
    public CanvasGroup escapePanel;

    public Sprite iconShip, iconJunk, iconRepair, iconCat, iconFuel;

    public float blackHoleGravity, gravityMultipler;

    private void Awake()
    {
        instance = this;
        spaceObjects = new List<SpaceObject>();
        ejectForwardButton.SetActive(false);
        ejectBackwardButton.SetActive(false);
    }

    private void Update()
    {
        DetermineBlackHoleDistance();
        CheckWinConditions();
    }

    private void DetermineBlackHoleDistance()
    {
        blackHoleDistance =  Vector2.Distance(ship.transform.position, blackHole.transform.position);
        float effectiveBlackHoleDistance = blackHoleDistance - 200f;
        if (blackHoleDistance < 200f) blackHoleDistance = 200f;
        blackHoleDistanceText.text = "Black hole distance = " + (effectiveBlackHoleDistance * 0.1f).ToString("0");
    }

    private void CheckWinConditions()
    {
        if (hasEnded) return;

        if (blackHoleDistance > 2200f)
        {
            foreach (SpaceObject item in inventory.items)
            {
                if (item is Cat)
                {
                    endPanel.WinWithCat();
                    hasEnded = true;
                    return;
                }
            }
            endPanel.WinNoCat();
            hasEnded = true;
        }
    }

    public void Restart()
    {
        SceneManager.LoadScene("MenuScene");
    }

    public void MainMenu()
    {
        SceneManager.LoadScene("MenuScene");
    }
}
