﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class LootRandomizer : MonoBehaviour {

	public List<GameObject> lootChests;
    public static int chestIndex = 0;

    // Use this for initialization
    void Start () {
        //chestIndex = 0;
        lootChests = new List<GameObject> ();
		lootChests.Clear ();
		GetChests (transform);
        GameGenerator.SetRandomToOldSeed();
        RandomizeLootChests();
	}

	void GetChests (Transform source) {
		foreach (Transform child in source) {
            LootChestScript lootChestScript = child.GetComponent<LootChestScript>();
			if (lootChestScript) {
                lootChestScript.chestIndex = chestIndex++;
				child.gameObject.SetActive (false);
				lootChests.Add (child.gameObject);
			} else if (child.childCount > 0) {
				GetChests (child);
			}
		}
	}

	void RandomizeLootChests () {
		int totalChests = Random.Range (5, 16);

		if (lootChests.Count >= 7) {
			for (int i = 0; i < totalChests; i++) {
				int selected = Random.Range (0, lootChests.Count);
				lootChests [selected].SetActive (true);
				lootChests.Remove (lootChests [selected]);
				if (lootChests.Count == 0) {
					break;
				}
			}
		} else {
			for (int i = 0; i < lootChests.Count; i++) {
				lootChests [i].SetActive (true);
			}
		}
	}
}
