using UnityEngine;
using System.Collections.Generic;

public class Blast : MonoBehaviour
{
	public float destroyCountdown = 3.5f;
	
	public Dictionary<Collider2D, bool> damagedObjects = new Dictionary<Collider2D, bool>();

	public GameObject firstTarget;

	public float dmgcity;
	public float dmgplayer;
	
	void Start()
	{
		Camera.main.GetComponent<CameraShake>().shakeIntensity = 1.7f;
	}

	public void Init(Vector3 pos, float dmgcity, float dmgplayer, GameObject firstTarget, bool vfx)
	{
		this.dmgcity = dmgcity;
		this.dmgplayer = dmgplayer;
		this.transform.position = pos;
		this.firstTarget = firstTarget;
		damagedObjects.Clear();

		if (!vfx)
		{
			GetComponent<SpriteRenderer>().enabled = false;
		}
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (damagedObjects.ContainsKey(other))
			return;

		if (other.GetComponent<City>() != null && dmgcity > 0.01f  && other.GetComponent<DelayedDamage>() == null)
		{
			if (other.gameObject == firstTarget)
			{
				damagedObjects.Add(other, true);

				other.GetComponent<City>().Damage(null, transform.position, dmgcity);
			}
			else
			{
				damagedObjects.Add(other, true);

				DelayedDamage dd = other.gameObject.AddComponent<DelayedDamage>();
				dd.damageSrc = null;
				dd.damageSourcePt = transform.position;
				dd.damageAmount = dmgcity;
			}
		}
		else if (other.GetComponent<Player>() != null && dmgplayer > 0.01f && other.GetComponent<DelayedDamage>() == null)
		{
			damagedObjects.Add(other, true);

			other.GetComponent<Player>().Damage(null, transform.position, dmgplayer);
		}
	}

	void Update()
	{
		destroyCountdown -= Time.smoothDeltaTime;
		if (destroyCountdown < 0)
		{
			Destroy(gameObject);
		}
	}
}
