using UnityEngine;
using System.Collections;

public class SequenceFlasher : MonoBehaviour {

    public Create_Crystals CrystalDB = null;
    public int CrystalCnt = 0;

    
    public int RandomizeLen = 0;
    public int[] RandomizeList = null;

    public OrderRandomized OrderRand = null;

    public bool SequenceOngoing = false;

    public float current_time = 0.0f;
    public float speed_multiply = 1.0f;

    public Material[] ActiveMaterials;
    public Material[] ActiveMaterialsRed;
    public Material[] ActiveMaterialsOff;


    private GameObject obj_tmp = null;

    private SequenceChecker CrystalChk = null;

    private Material[] MaterialBu;
    private Color CrystalBuColor;

    public bool CrystalReset = false;
    

	// Use this for initialization
	void Start () {
        OrderRand  = this.GetComponent<OrderRandomized>();
        CrystalDB  = this.GetComponent<Create_Crystals>();
        CrystalChk = this.GetComponent<SequenceChecker>();

        MaterialBu = CrystalDB.crystal_list[0].GetComponentInChildren<MeshRenderer>().materials;
        CrystalBuColor = CrystalDB.crystal_list[0].GetComponentInChildren<Light>().color;
	}
	
	// Update is called once per frame
	void FixedUpdate () {

        if (CrystalReset)
        {
            ResetCrystals();
            CrystalReset = false;

        }

        if (obj_tmp)
        {
            obj_tmp.GetComponentInChildren<Light>().intensity = 0.25f;
            obj_tmp.GetComponentInChildren<MeshRenderer>().materials = MaterialBu;
        }

        if (current_time < 1.0f)
        {
            current_time += Time.deltaTime * speed_multiply * 0.25f; 
        }
        else
        { 
            current_time += Time.deltaTime * speed_multiply;
        }
        int seq_pos = (int) current_time;
        if (seq_pos >= OrderRand.RandomizeLen)
        {

            //OrderRand.Randomize = true;
            current_time = 0.0f;
            CrystalChk.enabled = true;
            this.enabled = false;
        }
        else
        {
            GameObject obj = CrystalDB.crystal_list[OrderRand.RandomizeList[seq_pos]];
            obj.GetComponentInChildren<Light>().intensity = 1.0f;
            obj.GetComponentInChildren<MeshRenderer>().materials = ActiveMaterials;
            obj_tmp = obj;
        }


	}

    public void ResetCrystals()
    {
        int len = CrystalDB.crystal_list.Length;
        for (int i = 0; i < len; i++)
        {
            CrystalDB.crystal_list[i].GetComponentInChildren<MeshRenderer>().materials = MaterialBu;
            CrystalDB.crystal_list[i].GetComponentInChildren<Light>().color = CrystalBuColor;
            CrystalDB.crystal_list[i].GetComponentInChildren<Light>().intensity = 0.25f;
        }
        current_time = 0.0f;
    }

    public void HilightCrystals()
    {
        int len = CrystalDB.crystal_list.Length;
        for (int i = 0; i < len; i++)
        {
            CrystalDB.crystal_list[i].GetComponentInChildren<MeshRenderer>().materials = ActiveMaterialsRed;
            CrystalDB.crystal_list[i].GetComponentInChildren<Light>().color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
            CrystalDB.crystal_list[i].GetComponentInChildren<Light>().intensity = 0.75f;
        }
        current_time = 0.0f;
    }

    public void DeHilightCrystals()
    {
        int len = CrystalDB.crystal_list.Length;
        for (int i = 0; i < len; i++)
        {
            CrystalDB.crystal_list[i].GetComponentInChildren<MeshRenderer>().materials = ActiveMaterialsOff;
            //CrystalDB.crystal_list[i].GetComponentInChildren<Light>().color = new Color(0.0f, 0.0f, 0.0f, 1.0f);
            CrystalDB.crystal_list[i].GetComponentInChildren<Light>().intensity = 0.0f;
        }
        current_time = 0.0f;
    }




}
