using UnityEngine;
using System.Collections.Generic;

public class Culture {
	public Color color;
	public Culture parent = null;

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

	public Culture() {
		color = new Color(Helper.getRandom(), Helper.getRandom(), Helper.getRandom());
		Controller.cultures.Add(this);
	}

	public Culture(Culture parent) {
		this.parent = parent;
		color = Helper.getColorVariation(parent.color);
		Controller.newCultures.Add(this);
	}

	public void split() {
		Debug.Assert(regions.Count > 2);

		Culture c0 = new Culture(this);
		Culture c1 = new Culture(this);

		Region r0 = null;
		Region r1 = null;

		//Selects region#0 and region#1 as the new starts
		foreach(Region region in regions) {
			if (r0 == null) {
				r0 = region;
			}
			else if (r1 == null) {
				r1 = region;
			}
			else {
				break;
			}
		}

		foreach(Region region in regions) {
			if (r0.distance(region) < r1.distance(region)) {
				region.setCulture(c0);
			}
			else if (r1 == null) {
				region.setCulture(c1);
			}
		}

		regions.Clear();
	}
}