using UnityEngine;
using System.Collections;

public class Dipsy : MonoBehaviour {

	public GameObject[] bullets;
	public Transform weaponTip;

	public int currentWeaponId;

	public LaserBeam laser;
	public GameObject catchLight;

	public AudioSource[] sfxCatch;
	public AudioSource sfxBullet;
	public AudioSource sfxLaser;
	public AudioSource[] sfxShoot;

	private float[] weaponFrequency;
	private float posX;
	private float posY;
	private float countdown;

	private float angle;

	private bool laserUnlocked;
	private bool laserFiring;

	private float laserCountdown;
	private float laserLimit;

	private int laserUsed;

	private float lightCountdown;

	void Start(){
		currentWeaponId = 0;
		weaponFrequency = new float[bullets.Length];
		weaponFrequency [0] = Constants.BASIC_WEAPON_FREQUENCY;
		laserUnlocked = false;
		laserFiring = false;
		laserCountdown = 0.0f;
		laserUsed = 0;
		laserLimit = Constants.LASER_TIME_LIMIT;
		catchLight.renderer.enabled = false;
		lightCountdown = 0.0f;
	}

	void FixedUpdate () {
		transform.position = arrangePosition(convert3to2(Camera.main.ScreenToWorldPoint (Input.mousePosition)).normalized, Constants.ORBIT_RADIUS);
		angle = Mathf.Atan (transform.position.y / transform.position.x) * Mathf.Rad2Deg;
		transform.eulerAngles = new Vector3(0.0f, 0.0f, angle);
		if (transform.position.x < 0) {
			transform.localScale = new Vector3 (-1.0f, transform.localScale.y, transform.localScale.z);
		} else {
			transform.localScale = new Vector3 (1.0f, transform.localScale.y, transform.localScale.z);
		}
		countdown += Time.deltaTime;
		if (Input.GetMouseButton (0)) {
			Fire ();
		}

		if(Input.GetKeyDown(KeyCode.LeftControl)){
			changeWeapon();
		}

		if(Input.GetMouseButtonUp(1) && (laserUsed < Constants.LASER_USAGE_LIMIT) && !laserFiring){
			activateLaser();
		}

		if(Input.GetKeyDown(KeyCode.R)){
			Application.LoadLevel(0);
		}

		if (catchLight.renderer.enabled) {
			if(lightCountdown < Constants.CATCH_TIME_LIMIT){
				lightCountdown += Time.deltaTime;
			} else {
				catchLight.renderer.enabled = false;
				lightCountdown = 0.0f;
			}
		}

		if(laserFiring){
			laser.Fire(transform);
			if(!sfxLaser.isPlaying)sfxLaser.Play ();

			if(laserCountdown < laserLimit){
				laserCountdown += Time.deltaTime;
			} else {
				laser.StopFire();
				laserCountdown = 0.0f;
				laserFiring = false;
				currentWeaponId = 0;
			}
		}
	}

	void OnTriggerEnter2D(Collider2D other){
		if(other.tag == "Thinkie"){
			if(other.GetComponent<Thinkie>().type == Thinkie.ThinkieType.GOOD){
				/* Catch */
				int i = Random.Range(0, sfxCatch.Length);
				sfxCatch[i].Play();
				catchLight.renderer.enabled = true;
				lightCountdown = 0.0f;
				other.GetComponent<GThinkie>().TeleportToExit();
			}
		}
	}

	public void Fire(){
		if (currentWeaponId == 0 && !(countdown < weaponFrequency[0])) {
			countdown = 0.0f;
			Bullet b = ((GameObject)Instantiate (bullets [currentWeaponId], weaponTip.position, Quaternion.identity)).GetComponent<Bullet> ();
			b.Activate (transform);
			sfxBullet.Play();
			if(transform.position.x < 0.0f)
				b.transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y * -1.0f, transform.localScale.z);
		}
	}

	public void activateLaser(){
		currentWeaponId = 1;
		if(!laserUnlocked) GameObject.FindGameObjectWithTag ("LaserUnlocker").GetComponent<LaserUnlocker>().Activate();
		GameObject.FindGameObjectWithTag ("LaserHUD").GetComponent<LaserHUD> ().Activate();
		laserUnlocked = true;
		laserFiring = true;
		laserUsed++;
	}


	public void changeWeapon(){
		currentWeaponId++;
		if (!(currentWeaponId < bullets.Length))
			currentWeaponId = 0;
	}

	private Vector2 convert3to2(Vector3 v){
		return new Vector2 (v.x, v.y);
	}

	private Vector3 arrangePosition(Vector2 v, float f){
		return new Vector3 (v.x * f, v.y * f, transform.position.z);
	}
}
