using FalcoEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class player_s : MonoBehaviour
{
	private float grave;
	private box_s[] all_box;
	private box_s try_coll;
	private float jump_power;
	public GameObject inside_box_green;
	public GameObject inside_box_red;
	private float box_rot;
	private int dir;
	private GameObject my_camera;
	public string color="green";
	private bool destroy;
	public Prefab small_cube1;
	public Prefab small_cube2;
	public Prefab game_over;
	public Prefab completed;
	private bool finish;
	private GameObject gate;
	private bool over;
	public Prefab sound_change;
	public Prefab sound_jump;
	public Prefab sound_jumper;
	public Prefab sound_win;
	public Prefab sound_dead;
	private int rel;
	private int sound_rel;
	
	void Start()
	{
		gate=GameObject.Find("gate");
		my_camera=GameObject.Find("Camera");
	}
	
	void FixedUpdate()
	{	
		if(sound_rel>0)
		{
			sound_rel--;
		}
		
		if(color=="red")
		{
			if(inside_box_red.enabled==false)
			{
				sound_change.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
			}
			inside_box_red.enabled=true;
			inside_box_green.enabled=false;
		}
		
		if(color=="green")
		{
			if(inside_box_green.enabled==false)
			{
				sound_change.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
			}
			inside_box_red.enabled=false;
			inside_box_green.enabled=true;
		}			
		
		my_camera.transform.position=Vector3.Lerp(my_camera.transform.position,new Vector3(transform.position.x,transform.position.y+2,my_camera.transform.position.z),0.1f);
		all_box=FindObjectsOfType<box_s>();
		
		dir=0;
		
		if(Input.GetKey(ScanCode.A) || Input.GetKey(ScanCode.LeftArrow))
		{
			dir=1;
			box_rot-=5;
			transform.position-=new Vector3(0.1f,0,0);
			
			try_coll=collide_verefy();
		
			if(try_coll!=null)
			{
				transform.position+=new Vector3(0.1f,0,0);
			}			
		}

		if(Input.GetKey(ScanCode.D) || Input.GetKey(ScanCode.RightArrow))
		{
			dir=2;
			box_rot+=5;
			transform.position+=new Vector3(0.1f,0,0);
			
			try_coll=collide_verefy();
		
			if(try_coll!=null)
			{
				transform.position-=new Vector3(0.1f,0,0);
			}			
		}

		transform.position+=new Vector3(0,jump_power,0);
		
		try_coll=collide_verefy();
		
		if(try_coll!=null)
		{
			transform.position-=new Vector3(0,jump_power,0);
			jump_power=0;
			grave=0;
		}
		
		grave+=0.003f;
		
		transform.position-=new Vector3(0,grave,0);
	
		try_coll=collide_verefy();
		
		if(try_coll!=null)
		{			
			transform.position=new Vector3(transform.position.x,try_coll.transform.position.y+2.001f,transform.position.z);
			jump_power=0;
			grave=0;
		}
		
		if(dir==0 && grave==0)
		{
			//box_rot=Mathf.Lerp(box_rot,0,0.5f);
			box_rot=0;
		}
		
		inside_box_red.transform.localRotation=Quaternion.Euler(90,box_rot,0);
		inside_box_green.transform.localRotation=Quaternion.Euler(90,box_rot,0);
		
		jumper_s[] all_jumper= FindObjectsOfType<jumper_s>();
		
		for(int i=0;i<all_jumper.Length;i++)
		{
			if(Vector3.Distance(transform.position,all_jumper[i].transform.position)<2)
			{
				grave=0;
				jump_power=0.23f;
			}
		}
		
		if(sound_rel==0)
		{
			sound_rel=20;
			
			if(grave<0.1f && jump_power==0.23f)
			{
				sound_jumper.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
			}
		}
		
		color_changer_red_s[] all_changer= FindObjectsOfType<color_changer_red_s>();
		
		for(int i=0;i<all_changer.Length;i++)
		{
			if(Vector3.Distance(transform.position,all_changer[i].transform.position)<2)
			{
				color="red";
			}
		}
		
		color_changer_blue_s[] all_changer2= FindObjectsOfType<color_changer_blue_s>();
		
		for(int i=0;i<all_changer2.Length;i++)
		{
			if(Vector3.Distance(transform.position,all_changer2[i].transform.position)<2)
			{
				color="green";
			}
		}	
		
		spike_s[] all_spikes= FindObjectsOfType<spike_s>();
		
		for(int i=0;i<all_spikes.Length;i++)
		{
			if(color==all_spikes[i].color)
			{
				if(Mathf.Abs(transform.position.x-all_spikes[i].transform.position.x)<all_spikes[i].size_x && Mathf.Abs(transform.position.y-all_spikes[i].transform.position.y)<all_spikes[i].size_y)
				{
					destroy=true;
					
					Destroy(gameObject);
					Vector3 pos= new Vector3(0,0,0);
					
					for(int i2=0;i2<10;i2++)
					{
						pos=new Vector3(FalcoEngine.Random.Range(-1f,1f),FalcoEngine.Random.Range(-1f,1f),FalcoEngine.Random.Range(-1f,1f));
						small_cube1.Instantiate(transform.position+pos,Quaternion.Euler(0,0,0));
						small_cube2.Instantiate(transform.position+pos,Quaternion.Euler(0,0,0));
					}
					sound_dead.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
					game_over.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
				}
			}
		}
		
		if(Vector3.Distance(transform.position,gate.transform.position)<3)
		{
			if(finish==false)
			{
				finish=true;
				sound_win.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
				completed.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
			}
		}
		
		if(transform.position.y<-10)
		{
			if(over==false)
			{
				over=true;
				sound_dead.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
				game_over.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
			}
		}
	}
	
	box_s collide_verefy()
	{
		box_s result=null;
		
		for(int i=0;i<all_box.Length;i++)
		{
			if(Mathf.Abs(transform.position.x-all_box[i].transform.position.x)<all_box[i].size_x && Mathf.Abs(transform.position.y-all_box[i].transform.position.y)<all_box[i].size_y)
			{
				result=all_box[i];
				break;
			}
		}
		
		return result;
	}
	
	void Update()
	{
		if(Input.GetKeyDown(ScanCode.Escape))
		{
			FalcoEngine.Application.Quit();
		}
		
		if(Input.GetKey(ScanCode.W) || Input.GetKey(ScanCode.UpArrow))
		{
			if(grave==0)
			{
				if(jump_power<0.15f)
				{
					sound_jump.Instantiate(new Vector3(0,0,0),Quaternion.Euler(0,0,0));
					jump_power=0.15f;
				}
			}
		}
	}
}
