using UnityEngine;
using System.Collections;

public class PlayerControll : MonoBehaviour {
	public float maxSpeed;
	public float minSpeed;
	public float jumpAccel;
	public Texture2D crossfire;
	
	public Vector3 additionForce;
	
	
	public int selectedWeapon;
	private float damage;
	private float force;
	private float reloading;
	private float reloadTime;
	private float recoil;
	
	private float lastJump;
	
	private CharacterMotor controller;
	
	private GunController guns;
	public GameObject preview;
	// Use this for initialization
	void Start () {
		guns = GameObject.FindGameObjectWithTag("Game").GetComponent<GunController>();
		
		transform.GetComponent<UnitStatus>().Ammo[0] = 150;
		selectedWeapon=0;
		SetWeapon();
		
		
		controller=transform.GetComponent<CharacterMotor>();
	}
	
	// Update is called once per frame
	void Update () {
		//MOUSE SETUP
		
		transform.GetComponent<MouseLook>().sensitivityX = GameOptions.mouseSensivity;
		transform.GetComponent<MouseLook>().sensitivityY = GameOptions.mouseSensivity;
		
		transform.FindChild("camera").GetComponent<MouseLook>().sensitivityX = GameOptions.mouseSensivity;
		transform.FindChild("camera").GetComponent<MouseLook>().sensitivityY = GameOptions.mouseSensivity;
		////////////
		
		transform.GetComponent<CharacterController>().Move(additionForce*Time.deltaTime);
		additionForce*=0.97f;
		if(Vector3.Distance(additionForce,new Vector3(0,0,0))<5)
			additionForce=Vector3.zero;
		
		
		
		
		Shooting();
		//Movement();
		ChangeWeapon();
		JumpingAccel();
	}
	
	
	void JumpingAccel()
	{
		lastJump+=Time.deltaTime;
		if(lastJump>1)
			controller.movement.maxForwardSpeed=minSpeed;
		
		
		if(controller.movement.maxForwardSpeed<minSpeed)
		{
			controller.movement.maxForwardSpeed=minSpeed;
		}
		
		if(Input.GetKeyDown(KeyCode.Space))
		{
			lastJump=0;
			controller.movement.maxForwardSpeed+=jumpAccel;
			if(controller.movement.maxForwardSpeed>maxSpeed)
			{
				controller.movement.maxForwardSpeed=maxSpeed;
			}
		}
	}
	
	void Shooting()
	{
		reloading+=Time.deltaTime;
		
		if(reloading>guns.guns[selectedWeapon].Reload)
		{
			reloading=guns.guns[selectedWeapon].Reload;
			audio.Stop();
		}
		if(Input.GetAxis("Fire1")>0 & reloading==guns.guns[selectedWeapon].Reload)
		{
			if(transform.GetComponent<UnitStatus>().Ammo[selectedWeapon]>0)
			{
				transform.GetComponent<UnitStatus>().Ammo[selectedWeapon]-=1;
				reloading=0;
				
				if(guns.guns[selectedWeapon].sound)
				{
					audio.clip = guns.guns[selectedWeapon].sound;
					audio.volume=GameOptions.soundVolume;
					audio.Play();
				}
				
				
				
				// Recoil
				
				gameObject.layer = 3;
				for(int h=0;h<guns.guns[selectedWeapon].shoots;h++)
				{
					float p = Random.Range(0.1f,1);
					float p2 = Random.Range(-Mathf.PI,Mathf.PI);
					
					Ray ray =  transform.FindChild("camera").camera.ScreenPointToRay (new Vector2(Screen.width/2 + guns.guns[selectedWeapon].Recoil*Mathf.Sin(p2)*p,Screen.height/2+guns.guns[selectedWeapon].Recoil*Mathf.Cos(p2)*p));
					RaycastHit hit;
					if(Physics.Raycast(ray, out hit,500,3))
					{
						if(guns.guns[selectedWeapon].missile)
						{
							GameObject g = Instantiate(guns.guns[selectedWeapon].missile,transform.FindChild("camera").transform.Find("launch").position,transform.FindChild("camera").transform.rotation)as GameObject;
							g.transform.LookAt(hit.point);
							g.transform.GetComponent<MissileScript>().id = transform.GetComponent<UnitStatus>().id;
							Physics.IgnoreCollision(g.collider,collider);
						}
						
						if(hit.transform.GetComponent<DecorScript>())
						{
							hit.transform.GetComponent<DecorScript>().Shoot(transform.position,hit.point,guns.guns[selectedWeapon].Force);
							
						}
						
						if(hit.transform.GetComponent<UnitStatus>())
						{
							hit.transform.GetComponent<UnitStatus>().ApplyDamage(guns.guns[selectedWeapon].Damage,transform.GetComponent<UnitStatus>().id);
						}
						
						//Animation
						if(guns.guns[selectedWeapon].touchEffect)
						{
							GameObject g = Instantiate(guns.guns[selectedWeapon].touchEffect,hit.point,new Quaternion(hit.normal.x,hit.normal.y,hit.normal.z,0)) as GameObject;
							g.transform.Translate(hit.normal*0.1f);
						}
					}
					gameObject.layer = 0;
				}
			}
		}
	}
	
	void OnGUI()
	{
		GUI.DrawTexture(new Rect(Screen.width/2-crossfire.width/2,Screen.height/2-crossfire.height/2,crossfire.width,crossfire.height),crossfire);	
	}
	
	public int GetAmmo()
	{
		return transform.GetComponent<UnitStatus>().Ammo[selectedWeapon];
	}
	
	void ChangeWeapon()
	{
		
		if(Input.GetKeyDown("1"))
		{
			selectedWeapon=0;
			SetWeapon();
		}
		if(Input.GetKeyDown("2"))
		{
			selectedWeapon=1;
			SetWeapon();
		}
		if(Input.GetKeyDown("3"))
		{
			selectedWeapon=2;
			SetWeapon();
			
		}
		if(Input.GetKeyDown("4"))
		{
			selectedWeapon=3;
			SetWeapon();
		}
		preview.transform.rotation = transform.FindChild("camera").transform.rotation;
		
		
	}
	


	void OnControllerColliderHit(ControllerColliderHit hit)
	{
		//Debug.Log("Normal:"+hit.normal);
		//Debug.Log("Before: "+additionForce);
		additionForce+= new Vector3(additionForce.x * hit.normal.x * Mathf.Sign(additionForce.x),additionForce.y * hit.normal.y * Mathf.Sign(additionForce.y),additionForce.z * hit.normal.z * Mathf.Sign(additionForce.z));
		//Debug.Log("After: "+additionForce);
	}
	
	
	
	public void SetWeapon()
	{
		//Defaults
		if(preview!=null)
			Destroy(preview);
		preview = Instantiate(guns.guns[selectedWeapon].preview,transform.FindChild("camera").transform.Find("launch").position,transform.FindChild("camera").rotation) as GameObject;
		preview.transform.parent = transform.FindChild("camera");
	}
	
}
