using UnityEngine;
using System.Collections;

public class BlockScript : MonoBehaviour {

	// 0 = Wall
	// 1 = Switch Block
	// 2 = Switch Button
	// 3 = Spikes
	// 4 = Exit
	public int blocktype = 0;
	public bool dreamstate = false;
	public bool switchstate = false;
	public int switchid = 0;

	public PlayerScript ps;
	public SpriteRenderer sr;

	// Use to trigger a switch block
	void SwitchBlock () {
		if (switchstate) {
			switchstate = false;
			collider2D.enabled = false;
			sr.sprite = ps.bricksprite[11];
		} else {
			switchstate = true;
			collider2D.enabled = true;
			if (dreamstate) {
				sr.sprite = ps.bricksprite[3];
			} else {
				sr.sprite = ps.bricksprite[1];
			}
		}
	}

	// Use to trigger a switch button
	void FlipSwitch () {
		BlockScript bs;
		if (switchstate) {
			switchstate = false;
			sr.sprite = ps.bricksprite[6];
			GameObject[] turnoff = GameObject.FindGameObjectsWithTag ("SwitchBlocks");
			foreach (GameObject offyougo in turnoff) {
				bs = offyougo.GetComponent<BlockScript>();
				if (bs.switchid == switchid) {
					bs.SwitchBlock ();
				}
			}
		} else {
			switchstate = true;
			sr.sprite = ps.bricksprite[7];
			GameObject[] turnon = GameObject.FindGameObjectsWithTag ("SwitchBlocks");
			foreach (GameObject onyougo in turnon) {
				bs = onyougo.GetComponent<BlockScript>();
				if (bs.switchid == switchid) {
					bs.SwitchBlock ();
				}
			}
		}
		AudioSource.PlayClipAtPoint (ps.sfx[0],transform.position);
	}

	// Use this for initialization
	void Start () {
		ps = GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerScript>();
		sr = GetComponentInChildren<SpriteRenderer>();
		switch (blocktype) {
		case 1:
			if (!switchstate) {
				collider2D.enabled = false;
				sr.sprite = ps.bricksprite[11];
			}
			break;
		case 2:
			collider2D.enabled = false;
			if (switchstate) {
				sr.sprite = ps.bricksprite[7];
			}
			break;
		case 4:
			collider2D.enabled = false;
			break;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (ps.isdreaming) {
			if ((!ps.transition) && (!dreamstate)) {
				dreamstate = true;
				if (blocktype == 0) {
					sr.sprite = ps.bricksprite[2];
					if (((Random.value * 3F) < ps.stress) && (Vector3.Distance (ps.transform.position,transform.position) > 2.5F)) {
						Instantiate (ps.miasma,transform.position,Quaternion.identity);
					}
				} else if ((blocktype == 1) && (switchstate)) {
					sr.sprite = ps.bricksprite[3];
				} else if (blocktype == 4) {
					sr.sprite = ps.bricksprite[11];
				}
			} else if ((ps.transition) && (dreamstate)) {
				dreamstate = false;
				if (blocktype == 0) {
					sr.sprite = ps.bricksprite[0];
				} else if ((blocktype == 1) && (switchstate)) {
					sr.sprite = ps.bricksprite[1];
				} else if (blocktype == 4) {
					sr.sprite = ps.bricksprite[5];
				}
			}
		}
		if (!ps.gameover) {
			if (Vector3.Distance (transform.position,ps.transform.position) < 0.65F) {
				if ((blocktype == 2) && (Input.GetButtonDown ("Use")) && (!ps.transition)) {
					FlipSwitch ();
				} else if ((blocktype == 4) && (Input.GetButtonDown ("Use")) && (!ps.transition) && (!ps.isdreaming) && (!ps.ishurt)) {
					ps.gameover = true;
					ps.haswon = true;
					ps.fs.whiteout = true;
					ps.fs.playerwon = true;
					ps.pm1 = false;
					ps.pm2 = false;
					ps.pm3 = false;
					ps.pm4 = false;
					ps.pm5 = false;
				}
			}
		}
	}
}
