using UnityEngine;
using System.Collections;
using System;


public class Player : MonoBehaviour
{
    public int points = 0;
    public float time = 0.0f;
    public bool isActive = false;
    public LevelRepeater levelRepeater;
    public float countdownTime = 1.5f;
    public GameObject shield;
    public float shieldRegenTime = 1.0f;

    public GameObject logoImage;
    public GameObject pressAnyKeyImage;

    public GameObject oneImage;
    public GameObject twoImage;
    public GameObject threeImage;
    public GameObject goImage;

    public GameObject scoreImage;
    public TextMesh gameOverScoreText;
    public GameObject timeImage;
    public TextMesh gameOverTimeText;

    public TextMesh scoreText;
    public TextMesh timeText;

    private GameState m_state;
    private float m_countdownTimer;
    private Vector3 m_startPosition;
    private Quaternion m_startRotation;
    private bool m_bShieldActive = true;
    private float m_shieldTimer = 0.0f;

    void Start()
    {
        m_startPosition = transform.position;
        m_startRotation = transform.rotation;
        m_state = GameState.StartScreen;
        DeactivateStates();
        StartScreenActivate();        
    }

    public void Update()
    {
        switch(m_state)
        {
            case GameState.Countdown:
                m_countdownTimer += Time.deltaTime;
                if (m_countdownTimer < countdownTime)
                {
                    threeImage.renderer.enabled = true;
                    twoImage.renderer.enabled = false;
                    oneImage.renderer.enabled = false;
                    goImage.renderer.enabled = false;
                }
                else if (m_countdownTimer < countdownTime * 2)
                {
                    threeImage.renderer.enabled = false;
                    twoImage.renderer.enabled = true;
                    oneImage.renderer.enabled = false;
                    goImage.renderer.enabled = false;
                }
                else if (m_countdownTimer < countdownTime * 3)
                {
                    threeImage.renderer.enabled = false;
                    twoImage.renderer.enabled = false;
                    oneImage.renderer.enabled = true;
                    goImage.renderer.enabled = false;
                }
                else if (m_countdownTimer < countdownTime * 4)
                {
                    threeImage.renderer.enabled = false;
                    twoImage.renderer.enabled = false;
                    oneImage.renderer.enabled = false;
                    goImage.renderer.enabled = true;
                }
                else
                {
                    DeactivateStates();
                    PlayingActivate();
                }

                break;

            case GameState.Playing:
                if (!isActive)
                    isActive = true;

                if(!m_bShieldActive)
                {
                    m_shieldTimer += Time.deltaTime;
                    if(m_shieldTimer > shieldRegenTime)
                    {
                        m_bShieldActive = true;
                        shield.renderer.enabled = true;
                    }
                }

                time += Time.deltaTime;
                scoreText.text = points.ToString();
                timeText.text = TimeSpan.FromSeconds((double)time).ToString().TrimEnd('0');
                break;

            case GameState.GameOver:
            case GameState.StartScreen:
                if (Input.anyKeyDown)
                {
                    DeactivateStates();
                    StartGame();
                }
                break;
        }        
    }



    private void StartGame()
    {
        points = 0;
        time = 0.0f;
        levelRepeater.Reset();
        transform.position = m_startPosition;
        transform.rotation = m_startRotation;
        m_state = GameState.Countdown;
        m_countdownTimer = 0.0f;
    }

    public void OnDeath()
    {
        rigidbody.Sleep();
        isActive = false;
        DeactivateStates();
        GameOverActivate();
    }

    public void OnHit()
    {
        if (m_bShieldActive)
        {
            m_bShieldActive = false;
            m_shieldTimer = 0.0f;
            shield.renderer.enabled = false;
        }
        else
            OnDeath();
    }

    private void StartScreenActivate()
    {
        logoImage.renderer.enabled = true;
        pressAnyKeyImage.renderer.enabled = true;
    }

    private void GameOverActivate()
    {
        m_state = GameState.GameOver;
        scoreImage.renderer.enabled = true;
        gameOverScoreText.renderer.enabled = true;
        gameOverScoreText.text = points.ToString();
        timeImage.renderer.enabled = true;
        gameOverTimeText.renderer.enabled = true;
        gameOverTimeText.text = TimeSpan.FromSeconds((double)time).ToString().TrimEnd('0');
        pressAnyKeyImage.renderer.enabled = true;
        //timeText.text = ((int)time).ToString() + "s";
    }

    private void PlayingActivate()
    {
        m_state = GameState.Playing;
        scoreText.renderer.enabled = true;
        timeText.renderer.enabled = true;
        isActive = true;
        rigidbody.WakeUp();
        m_bShieldActive = true;
        shield.renderer.enabled = true;
    }

    private void DeactivateStates()
    {
        scoreImage.renderer.enabled = false;
        gameOverScoreText.renderer.enabled = false;
        timeImage.renderer.enabled = false;
        gameOverTimeText.renderer.enabled = false;
        logoImage.renderer.enabled = false;
        pressAnyKeyImage.renderer.enabled = false;
        scoreText.renderer.enabled = false;
        timeText.renderer.enabled = false;
        threeImage.renderer.enabled = false;
        twoImage.renderer.enabled = false;
        oneImage.renderer.enabled = false;
        goImage.renderer.enabled = false;
    }

    private enum GameState
    {
        StartScreen = 0,
        Countdown,
        Playing,
        GameOver
    }

    private static Player s_Instance = null;
 
    // This defines a static instance property that attempts to find the manager object in the scene and
    // returns it to the caller.
    public static Player instance {
        get {
            if (s_Instance == null) {
                // This is where the magic happens.
                //  FindObjectOfType(...) returns the first AManager object in the scene.
                s_Instance =  FindObjectOfType(typeof (Player)) as Player;
            }
            
            // If it is still null, create a new instance
            if (s_Instance == null) {
                GameObject obj = new GameObject("AManager");
                s_Instance = obj.AddComponent(typeof (Player)) as Player;
                Debug.Log ("Could not locate an Player object. Player was Generated Automaticly.");
            }
            
            return s_Instance;
        }
    }
    
    // Ensure that the instance is destroyed when the game is stopped in the editor.
    void OnApplicationQuit() {
        s_Instance = null;
    }
}