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

public class City : BaseObject
{
    [SyncVar]
	public bool hasMissile = true;

	public Missile missilePrefab;
	public CityPopup popupPrefab1;

	public City targetCity;

	public GameObject ruinPrefab;
	public GameObject missileStand;

	void Awake()
	{
		
	}
	
	void Update()
	{
	    if(!hasMissile)
        {
            missileStand.SetActive(false);
        }
	}

	void OnMouseUpAsButton()
	{
		Player.LocalPlayer.OnCityClick(this);
	}
		
	public void Damage(BaseObject damageSrc, Vector3 damageSourcePt, float damageAmount)
	{
        Debug.Log("cITY Damage");
		//if (!isServer)
		//	return;

		if (!alive)
			return;

		if (hasMissile)
		{
			Explode();
		}
		else
		{
			Die();
		}
	}

	public void Explode()
	{
		if (!alive)
			return;

		Die();

		Game.GetInstance().CreateBlast(this.gameObject, transform.position + new Vector3(0, -1, 0), 999, 999, true);
	}

    [Command]
    void CmdCityDie(int side)
    {
        if (side == 1)
        {
            ++Game.GetInstance().redTeamScore;
        }
        else if (side == 0)
        {
            ++Game.GetInstance().blueTeamScore;
        }
        Debug.Log("RpcCityDie");
    }

    public override void Die()
	{
        CmdCityDie(side);
		base.Die();

		GameObject obj = Instantiate<GameObject>(ruinPrefab);
		obj.transform.position = this.transform.position;
		obj.transform.rotation = this.transform.rotation;
	}
}
