using UnityEngine;
using System.Collections;

public class GameManager : Singleton<GameManager> 
{
    private GameObject _Team1 = null;
    private GameObject _Team2 = null;

    private bool _bGameOver = false;
    private bool _bCanRestart = false;

    public bool GameOver
    {
        get
        {
            return _bGameOver;
        }
    }

    public void Start()
    {
        
    }

    public void Update()
    {
        if (_bCanRestart)
        {
            if (Input.GetButtonDown("P1_Start") || Input.GetButtonDown("P2_Start") || Input.GetButtonDown("P3_Start") || Input.GetButtonDown("P4_Start"))
            {
                Application.LoadLevel("MainScene");

                StartMatch();
            }
        }
    }

    public void Resume()
    {
        if (_bGameOver)
        {
            return;
        }

        // Restore rigid bodies as dynamics.
        {
            Rigidbody2D[] bodies = GameObject.FindObjectsOfType<Rigidbody2D>();

            foreach (Rigidbody2D b in bodies)
            {
                b.isKinematic = false;
            }
        }

        // Re-activate animators and Movement Controllers

        {
            if (_Team1 != null)
            {
                Transform Volphante = _Team1.transform.FindChild("Volphante");

                if (Volphante != null)
                {
                    Animator animator = (Animator)Volphante.gameObject.GetComponent(typeof(Animator));

                    if (animator != null)
                    {
                        animator.enabled = true;
                    }
                }

                MovementController mc = (MovementController)_Team1.GetComponent(typeof(MovementController));

                if (mc != null)
                {
                    mc.enabled = true; ;
                }
            }

            if (_Team2 != null)
            {
                Transform Volphante = _Team2.transform.FindChild("Volphante");

                if (Volphante != null)
                {
                    Animator animator = (Animator)Volphante.gameObject.GetComponent(typeof(Animator));

                    if (animator != null)
                    {
                        animator.enabled = true;
                    }
                }

                MovementController mc = (MovementController)_Team2.GetComponent(typeof(MovementController));

                if (mc != null)
                {
                    mc.enabled = true; ;
                }

            }
        }
    }

    public void Stop()
    {
        // Set rigid bodies as kinematics.

        {
            Rigidbody2D[] bodies = GameObject.FindObjectsOfType<Rigidbody2D>();

            foreach (Rigidbody2D b in bodies)
            {
                b.velocity = new Vector2(0f, 0f);
                b.isKinematic = true;
            }
        }

        // Deactivate Animators and Movement Controllers

        {
            if (_Team1 != null)
            {
                Transform Volphante = _Team1.transform.FindChild("Volphante");

                if (Volphante != null)
                {
                    Animator animator = (Animator)Volphante.gameObject.GetComponent(typeof(Animator));

                    if (animator != null)
                    {
                        MegaShoot ms = (MegaShoot)_Team1.GetComponent(typeof(MegaShoot));

                        if (ms != null)
                        {
                            if (!ms.bPreparingShoot || !ms.bShooting)
                            {
                                Lifebar life = (Lifebar)_Team1.GetComponent(typeof(Lifebar));

                                if (life != null)
                                {
                                    if (!life.IsDead)
                                    {
                                        animator.enabled = false;
                                    }
                                    else
                                    {
                                        animator.enabled = true;
                                    }
                                }
                                else
                                {
                                    animator.enabled = false;
                                }
                            }
                        }
                    }
                }

                MovementController mc = (MovementController)_Team1.GetComponent(typeof(MovementController));

                if (mc != null)
                {
                    mc.enabled = false;
                }
            }

            if (_Team2 != null)
            {
                Transform Volphante = _Team2.transform.FindChild("Volphante");

                if (Volphante != null)
                {
                    Animator animator = (Animator)Volphante.gameObject.GetComponent(typeof(Animator));

                    if (animator != null)
                    {
                        MegaShoot ms = (MegaShoot)_Team2.GetComponent(typeof(MegaShoot));

                        if (ms != null)
                        {
                            if (!ms.bPreparingShoot || !ms.bShooting)
                            {
                                Lifebar life = (Lifebar)_Team2.GetComponent(typeof(Lifebar));

                                if (life != null)
                                {
                                    if (!life.IsDead)
                                    {
                                        animator.enabled = false;
                                    }
                                    else
                                    {
                                        animator.enabled = true;
                                    }
                                }
                                else
                                {
                                    animator.enabled = false;
                                }
                            }
                        }
                    }
                }

                MovementController mc = (MovementController)_Team2.GetComponent(typeof(MovementController));

                if (mc != null)
                {
                    mc.enabled = false;
                }
            }
        }

        // Destroy bullets.

        {
            GameObject bullets = GameObject.Find("Bullets");

            if (bullets != null)
            {
                Destroy(bullets);
            }
        }

    }

    public void StartMatch()
    {
        Debug.Log("GAME MANAGER INITIALIZATION...");

        _Team1 = GameObject.Find("Player1");
        _Team2 = GameObject.Find("Player2");

        if (_Team1 == null)
        {
            Debug.LogError("MISSING PLAYER1");
        }

        if (_Team2 == null)
        {
            Debug.LogError("MISSING PLAYER2");
        }

        _bGameOver = false;
        _bCanRestart = false;

        Debug.Log("GAME MANAGER INITIALIZED.");
    }

    public void EndMatch()
    {
        if (_Team1 != null && _Team2 != null)
        {
            Lifebar life1 = (Lifebar)_Team1.GetComponent(typeof(Lifebar));
            Lifebar life2 = (Lifebar)_Team2.GetComponent(typeof(Lifebar));

            if (life1 != null && life2 != null)
            {
                if (life1.Life <= 0f)
                {
                    _bGameOver = true;

                    Stop();

                    Invoke("Team2Wins", 2f);
                }
                else
                {
                    if (life2.Life <= 0f)
                    {
                        _bGameOver = true;

                        Stop();

                        Invoke("Team1Wins", 2f);
                    }
                }
            }
        }
    }

    private void Team1Wins()
    {
        GameObject screens = GameObject.Find("Screens");

        if (screens != null)
        {
            Transform gameOver = screens.transform.Find("GameOver1");
            
            if (gameOver != null)
            {
                gameOver.gameObject.SetActive(true);

                _bCanRestart = true;
            }
        }
    }

    private void Team2Wins()
    {
        GameObject screens = GameObject.Find("Screens");

        if (screens != null)
        {
            Transform gameOver = screens.transform.Find("GameOver2");

            if (gameOver != null)
            {
                gameOver.gameObject.SetActive(true);

                _bCanRestart = true;
            }
        }
    }

}
