using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    public GameObject motherShip;
    
    public int playersAlive = 2;

    private int aliensKilled = 0;

    public GameObject GOUI;
    public GameObject GameUI;

    public bool gamePaused = false;

    public GameObject end;

    public GameObject p1;
    public GameObject p2;

    public void KillPlayer() {
        Debug.Log("Killing player");

        playersAlive--;

        if (playersAlive <= 0)
            GameOver();
    }

    public void KillAlien() {
        aliensKilled++;
    }

    public int GetAliensKilled() {
        return aliensKilled;
    }

    public void FinishShip() {
        Success();
    }

    public GameObject GetMotherShip() {
        return motherShip;
    }

    void Success() {
        Debug.Log("Success");

        MotherShip ms = motherShip.GetComponent<MotherShip>();
        GOUI.GetComponent<GameOverUI>().ShowText(true, ms.GetRepairs(), ms.GetMaxRepairs(), aliensKilled);

        End(true);
    }

    void GameOver() {
        Debug.Log("GameOver");

        MotherShip ms = motherShip.GetComponent<MotherShip>();
        GOUI.GetComponent<GameOverUI>().ShowText(false, ms.GetRepairs(), ms.GetMaxRepairs(), aliensKilled);

        End(false);
    }

    void End(bool win) {
        GOUI.SetActive(true);
        GameUI.SetActive(false);
        gamePaused = true;

        if (win) {
            p1.SetActive(false);
            p2.SetActive(false); 

            motherShip.SetActive(false);
            end.SetActive(true);
            end.GetComponent<Animator>().SetBool("isEnd", true);
        }
    }

    public bool IsGamePaused() {
        return gamePaused;
    }
}
