using UnityEngine;
using System.Collections;

public class SequenceChecker : MonoBehaviour {

    public OrderRandomized OrderRand = null;
    public Create_Crystals CrystalDB = null;
    public SequenceFlasher CrystalFlasher = null;

    public Material SwapMaterial;

    public int next_crystal_index = 0;
    private Transform next_crystal_position;

    public float target_trigger_distance = 0.5f;

    public float distance_to_target = 0.0f;

    public AudioSource HitOK;
    public AudioSource HitFail;

    public float EndTimeoutTime = 2.0f;
    public float TimeoutTime = 0.0f;
    public bool TimeoutOngoing = false;

    //public bool Sta
    private GameObject MousePointer = null;
    private GameObject TargetCrystal = null;

    private ScoreCounter ScoreDisp;
    private LevelTimer LevelTime;

    public Material[] MaterialList;

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

        ScoreDisp = GameObject.FindGameObjectWithTag("LevelTimer").GetComponent<ScoreCounter>();
        LevelTime = GameObject.FindGameObjectWithTag("LevelTimer").GetComponent<LevelTimer>();

        MousePointer = GameObject.FindGameObjectWithTag("MousePointer");
        if (!MousePointer)
            Debug.LogError("No mouse pointer");

        

	}
	
	// Update is called once per frame
	void Update () {
        if(!TimeoutOngoing)
        {
            LevelTime.enabled = true;

            if (!TargetCrystal)
            { 
                TargetCrystal = CrystalDB.crystal_list[OrderRand.RandomizeList[next_crystal_index]];
            }

            if (next_crystal_index < OrderRand.RandomizeLen)
            {
                if (Input.GetMouseButtonDown(0)) // Left Mouse button
                {
                    distance_to_target = (MousePointer.transform.position - TargetCrystal.transform.position).magnitude;
                    if (distance_to_target < target_trigger_distance)
                    {
                        // change clystal light
                        TargetCrystal.GetComponentInChildren<Light>().color = new Color(1.0f, 0.0f, 0.0f, 1.0f);
                        TargetCrystal.GetComponentInChildren<Light>().intensity = 1.0f;

                        //tmp[0] = SwapMaterial;
                        TargetCrystal.GetComponentInChildren<MeshRenderer>().materials = MaterialList;

                        next_crystal_index++;
                        TargetCrystal = CrystalDB.crystal_list[OrderRand.RandomizeList[next_crystal_index]];

                        ScoreDisp.Score += 100;

                        HitOK.Play();
                        
                    }
                    else
                    { 
                    // Fail to locate target
                        HitFail.Play();
                        LevelTime.RemoveTime(1.0f);
                    }
                }
            }
            else
            {
                TimeoutOngoing = true;
                TimeoutTime = EndTimeoutTime;
                LevelTime.enabled = false;
                GetComponent<SequenceFlasher>().HilightCrystals();
            }
        }
        else
        {
            if (TimeoutTime > 0.0f)
            {
                TimeoutTime -= Time.deltaTime;
            }
            else
            {
                this.enabled = false;
                CrystalFlasher.enabled = true;
                CrystalFlasher.CrystalReset = true;
                next_crystal_index = 0;
                TimeoutOngoing = false;
                OrderRand.Randomize = true;
                TargetCrystal = null;

                LevelTime.enabled = false;
                LevelTime.Reset();


            }
        }

    }


    public void Reset()
    {
        next_crystal_index = 0;
        TargetCrystal = null;
    
    }


}
