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

public class ObjectSelector<T> {
	private T priorityObjectId;
	private List<ObjectInfo<T>> randomObjects = new List<ObjectInfo<T>>(10);
	private float totalChance = 0f;
	private float randomObjectChance = 1f;
	private bool hasPriorityObject = false;

	public void Clear() {
		//this.priorityObjects.Clear ();
		this.priorityObjectId = default(T);
		this.randomObjects.Clear ();
		this.totalChance = 0f;
		this.randomObjectChance = 1f;
		this.hasPriorityObject = false;
	}

	public void AddPriorityObject(T id) {
		if (this.hasPriorityObject == false) {
			this.priorityObjectId = id;
			this.hasPriorityObject = true;
		}
	}

	public void AddRandomObject(T id, float chance) {
		this.randomObjects.Add(new ObjectInfo<T>() {
			id = id,
			chance = this.totalChance + chance,
		});
		this.totalChance += chance;
	}

	public void SetChanceForRandomObject(float chance) {
		this.randomObjectChance = chance;
	}

	public T GetRandomObject(C5.C5Random random) {
		if (this.hasPriorityObject == true) {
			return this.priorityObjectId;
		} else if (this.randomObjectChance >= 1f || random.NextFloat () < this.randomObjectChance) {
			var num = random.NextFloat (this.totalChance);
			for (int i = 0; i < this.randomObjects.Count; i++) {
				var obj = this.randomObjects [i];
				if (num < obj.chance) {
					return obj.id;
				}
			}
		}

		return default(T);
	}

	private class ObjectInfo<T> {
		public T id;
		public float chance;
	}
}
