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

public class Game : NetworkBehaviour
{
	#region singleton

	static Game instance;

	public static Game GetInstance()
	{
		if (instance == null)
			instance = FindObjectOfType<Game>();
		return instance;
	}

	#endregion

	public int localPlayerSide = 0;
	public int remotePlayerSide = 1;

	public Blast blastPrefab;
	public GameObject boomPrefab;

	#region game force management

	public GameForce[] gameForces = new GameForce[2];
	
	public GameForce GetGameForceBySide(int side)
	{
		return gameForces[side];
	}

	public GameForce GetGameForceLocal()
	{
		return GetGameForceBySide(localPlayerSide);
	}

	public GameForce GetGameForceRemote()
	{
		return GetGameForceBySide(remotePlayerSide);
	}

	#endregion

	public float oneRoundTime = 120;

	[SyncVar]
    public float timeRemaing = 60;
    [SyncVar]
    public int redTeamScore = 0;
    [SyncVar]
    public int blueTeamScore = 0;
    public bool isGameStart = false;

	bool isGameStartLast = false;

	void Awake()
	{
		//Init(0);
	}

	void Update()
	{
		if (!isServer)
		{
			return;
		}

        if(isGameStart)
        {
            timeRemaing -= Time.deltaTime;
            if (timeRemaing <= 0)
            {
                timeRemaing = 0;
				isGameStart = false;
				RpcGameOver();
            }
        }

		isGameStartLast = isGameStart;
	}
	
	[ClientRpc]
    void RpcGameOver()
    {
        string result = "Win!";
        bool isWin = true;
        if (localPlayerSide == 0)
        {
            if (redTeamScore < blueTeamScore)
            {
                result = "Lost!";
                isWin = false;
            }
        }else if(localPlayerSide == 1)
        {
            if (redTeamScore > blueTeamScore)
            {
                result = "Lost!";
                isWin = false;
            }
        }
        InGamePanel.GetInstance().result.gameObject.SetActive(true);
        InGamePanel.GetInstance().SetResult(isWin);

        Debug.Log("GameOver! you" + result);
    }
	
	public void Reset()
    {
        timeRemaing = oneRoundTime;
        redTeamScore = 0;
        blueTeamScore = 0;
    }

	public void CreateBlast(GameObject ignoreObj, Vector3 pos, float damageToCity, float damageToPlayer, bool vfx)
	{
		Debug.Log("CreateBlast()");

		GameObject obj = Instantiate<GameObject>(blastPrefab.gameObject);
		obj.GetComponent<Blast>().Init(pos, damageToCity, damageToPlayer, ignoreObj, vfx);

		GameObject boomobj = Instantiate<GameObject>(boomPrefab);
		boomobj.transform.position = pos + new Vector3(0, 1, 0);
        
        if(isServer)
        {
            RpcCreateBlast(pos);
        }
    }

    [ClientRpc]
    void RpcCreateBlast(Vector3 pos)
    {
		Debug.Log("RpcCreateBlast()");
		if (localPlayerSide == 0)
			return;

		GameObject obj = Instantiate<GameObject>(blastPrefab.gameObject);
		obj.GetComponent<Blast>().Init(pos, 0, 0, null, true);

		GameObject boomobj = Instantiate<GameObject>(boomPrefab);
		boomobj.transform.position = pos;
    }
}
