using UnityEngine;
using System.Collections;

public class RitualArea : Area {


	public GameObject goBonfire;
	SpriteRenderer srBonfire;
	public Light bonfireLight;
	public GameObject goSparks;
	public GameObject goBurnedBody;

	bool bonfireOn = false;
	bool burnedBodyOn = false;

	float targetBonfireIntensity = 0;

	protected override void Start () {
		base.Start();
		srBonfire = goBonfire.GetComponent<SpriteRenderer>();
		goSparks.SetActive(false);
		bonfireLight.intensity = 0;
		goBurnedBody.SetActive(false);
		var c = Color.white;
		c.a = 0;
		srBonfire.color = c;
	}

	public bool BonfireOn {
		get { return bonfireOn; }
		set {
			if (bonfireOn == value)
				return;
			bonfireOn = value;
			goSparks.SetActive(value);
			targetBonfireIntensity = value ? 1f : 0;
			Game.instance.OnBonfireChange();
			if (!value)
				BurnedBodyOn = false;
		}
	}

	public bool BurnedBodyOn {
		get { return burnedBodyOn; }
		set {
			if (burnedBodyOn == value)
				return;
			burnedBodyOn = value;
			goBurnedBody.SetActive(burnedBodyOn);
		}
	}

	void Update () {
		if (bonfireLight.intensity != targetBonfireIntensity) {
			bonfireLight.intensity += Game.kSunsetDuration * Time.deltaTime * (targetBonfireIntensity > bonfireLight.intensity ? 1 : -1);
			bonfireLight.intensity = Mathf.Clamp01(bonfireLight.intensity);
			var c = srBonfire.color;
			c.a = bonfireLight.intensity;
			srBonfire.color = c;
		}
	}

	int lastSacrificeDay;
	public void WorkTick () {
		if (totalWorkForce >= 3 && BonfireOn && lastSacrificeDay < Game.instance.dayNum) {
			// perform the sacrifice
			lastSacrificeDay = Game.instance.dayNum;
			Human sacrifice = Game.instance.allHumans[0];
			foreach (Human h in Game.instance.allHumans) {
				if (h.currArea != this) {
					sacrifice = h;
					break;
				}
			}

			BurnedBodyOn = true;
			Game.instance.Kill(sacrifice);
			Game.instance.waterLevel = 0; // just reset it for now

			Game.PlaySfx("chant2");
		}
	}
}
