using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class God : MonoBehaviour
{
	public ResourceManager.GodPersonalities personality { get; private set; }

	public int currentAnger { get; private set; }
	public int angerMax { get; private set; }
	public int currentAppeasement { get; private set; }
	public int appeasementMax { get; private set; }

	public bool isAppeased { get; private set; }

	public Vector2 offscreenPosition;
	public float godMoveAnimateSpeed;
	public Vector2 onscreenPosition { get; private set; }	//Take from Editor position at launch

	public Color targetColour;
	public float colourChangeSpeed;

	public float bobSpeed;
	public float bobMaxAngle;
	float rotationOffset;
	bool IsBurning;

    // Position of the building chosen to wrath
    Vector2 TargetPosition;
    public bool IsProcessingSacrifice = false;
    GameObject VengeanceIcon;
	GameObject BuildingToHurtOrHeal;
    float CurrentVengeanceSpeed;

	float targetAppeasementFill;
	float targetAngerFill;

	void Reset()
	{
		godMoveAnimateSpeed = 3.0f;
		bobSpeed = 1.0f;
		bobMaxAngle = 5.0f;
	}

	void Awake()
	{
		onscreenPosition = GetComponent<RectTransform>().anchoredPosition;
		targetColour = Color.white;
	}

	void Start()
	{
		rotationOffset = Random.Range(-bobMaxAngle, bobMaxAngle);
	}

    public void setPersonality(ResourceManager.GodPersonalities newPersonality)
	{
		currentAnger = currentAppeasement = 0;
		targetAppeasementFill = targetAngerFill = 0.0f;
		isAppeased = false;
		personality = newPersonality;
		string displayText = ResourceManager.GodPersonalityDisplayStrings[(int)personality];
		transform.FindChild("PersonalityText").GetComponent<Text>().text = displayText;
	}

	public void setNewAngerAndAppeasement(int newAnger, int newAppeasement)
	{
		angerMax = newAnger;
		appeasementMax = newAppeasement;
		currentAnger = currentAppeasement = 0;
		updateAngerLabel();
	}

	public void increaseAnger()
	{
		currentAnger++;
		updateAngerLabel();
	}

	public void increaseAppeasement()
	{
		currentAppeasement++;
		updateAngerLabel();
	}

	void updateAngerLabel()
	{
		targetAngerFill = (float)currentAnger / (float)angerMax;
		targetAppeasementFill = (float)currentAppeasement / (float)appeasementMax;
	}

	void Update()
	{
		//Handle sacrifice stuff
		if (IsProcessingSacrifice)
		{
			Vector2 CurrentVengeancePos = VengeanceIcon.GetComponent<RectTransform>().anchoredPosition;

			Vector2 Direction = (TargetPosition - CurrentVengeancePos);
			if (Direction.magnitude > CurrentVengeanceSpeed)
			{
				CurrentVengeancePos += Direction.normalized * CurrentVengeanceSpeed;
				VengeanceIcon.GetComponent<RectTransform>().anchoredPosition = CurrentVengeancePos;
				++CurrentVengeanceSpeed;
			}
			else
			{
				// Vengeance has reached the building, apply it!
				VengeanceIcon.GetComponent<Image>().enabled = false;
				BuildingToHurtOrHeal.GetComponent<Building>().isOnFire = IsBurning;
				IsProcessingSacrifice = false;
			}
		}

		//Handle colour update
		Image img = GetComponent<Image>();
		img.color = Color.Lerp(img.color, targetColour, Time.deltaTime * colourChangeSpeed);

		//Handle God bobbing
		Vector3 angs = transform.eulerAngles;
		angs.z = Mathf.Sin(Time.time * bobSpeed + rotationOffset) * bobMaxAngle;
		transform.eulerAngles = angs;

		//Handle icon filling animations
		Image angerIcon = transform.FindChild("AngerIcon").GetComponent<Image>();
		angerIcon.fillAmount = Mathf.Lerp(angerIcon.fillAmount, targetAngerFill, Time.deltaTime * 3.0f);
		Image appeasementIcon = transform.FindChild("AppeasementIcon").GetComponent<Image>();
		appeasementIcon.fillAmount = Mathf.Lerp(appeasementIcon.fillAmount, targetAppeasementFill, Time.deltaTime * 3.0f);
	}

	public void OfferSacrifice(ResourceManager.BuildingResources offeredSacrifice)
	{
		isAppeased = ResourceManager.isGodAppeased(personality, offeredSacrifice);

		if (isAppeased)
        {
			// Blesses the population, cleaning the world of the two effects this god can negate
			Debug.Log(personality.ToString()+" is appeased!");
			increaseAppeasement();
			if (currentAppeasement >= appeasementMax) { BlessVillage(); }
        }
        else
        {
			// Picks a random building and applies the negative effect for this god
			Debug.Log(personality.ToString()+" is ANGRY!");
			increaseAnger();
			if (currentAnger >= angerMax) { ExecuteVengence(); }
        }
	}

	public void ExecuteVengence()
	{
		// Choose a random building that's currently OK and apply a negative buff
		Building randomBuilding = BuildingManager.instance.GetRandomBuilding(false);
		if (randomBuilding == null) { BuildingToHurtOrHeal = null; }
		else { BuildingToHurtOrHeal = randomBuilding.gameObject; }
		TargetPosition = BuildingToHurtOrHeal.GetComponent<RectTransform>().anchoredPosition;
		VengeanceIcon = GameObject.Find("ResourceIcon");
		VengeanceIcon.GetComponent<Image>().enabled = true;
		VengeanceIcon.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
		CurrentVengeanceSpeed = 5;
		IsProcessingSacrifice = true;
		IsBurning = true;

		AudioController.instance.playSFX(AudioController.audioClips.scream);
	}

	public void BlessVillage()
	{
		// Put out a building which is on fire
		BuildingToHurtOrHeal = BuildingManager.instance.GetRandomBuilding(true).gameObject;
		if (BuildingToHurtOrHeal)
		{
			TargetPosition = BuildingToHurtOrHeal.GetComponent<RectTransform>().anchoredPosition;
			VengeanceIcon = GameObject.Find("HeartIcon");
			VengeanceIcon.GetComponent<Image>().enabled = true;
			VengeanceIcon.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
			CurrentVengeanceSpeed = 5;
			IsProcessingSacrifice = true;
			IsBurning = false;
		}

		AudioController.instance.playSFX(AudioController.audioClips.cheer);
	}
}
