using UnityEngine;
using System.Collections.Generic;

public class Region : MonoBehaviour {
	public static float width = 0.8f;
	public static float compressedWidth = 0.6f;
	public static float height = 0.695f;

	public float altitude = 0;
	public float temperature = 0;
	public float humidity = 0;

	public Biome biome;
	private Culture _culture = null;
	public Culture culture {get {return _culture;}}

	public HashSet<Region> nghs = new HashSet<Region>();

	private uint w;
	private uint h;

	public void setPos(uint w, uint h) {
		this.w = w;
		this.h = h;
	}

	public float distance(Region other) {
		//TODO fix hexa dist
		Vector2 d = new Vector2(this.w - other.w, this.h - other.h);
		return d.sqrMagnitude;
	}

	public void setCulture(Culture culture) {
		_culture = culture;
		_culture.regions.Add(this);
	}

	public void assignBiome() {
		if (temperature < 0.3f) {
			biome = Controller.ice;
			return;
		}
		if (altitude < 0.5f) {
			biome = Controller.sea;
			return;
		}

		float minDist = float.MaxValue;
		for (int i = 0; i < Controller.biomes.Count; i++) {
			float dist = Controller.biomes[i].distance(altitude, temperature, humidity);
			if (dist < minDist) {
				biome = Controller.biomes[i];
				minDist = dist;
			}
		}
	}

	public void updateSprite(Controller.MapMode mapMode) {
		switch (mapMode) {
			case Controller.MapMode.Terrain:
				gameObject.GetComponent<SpriteRenderer>().sprite = biome.sprite;
			break;
			case Controller.MapMode.Culture:
				if (culture == null) {
					gameObject.GetComponent<SpriteRenderer>().sprite = Helper.getHexaSprite(new Color(0, 0, 0));
				}
				else {
					gameObject.GetComponent<SpriteRenderer>().sprite = Helper.getHexaSprite(culture.color);
				}
			break;
		}
	}
}
