package net.greencouchgames.ld27;

import org.andengine.entity.sprite.Sprite;
import org.andengine.util.color.Color;

import android.util.Log;

import tv.ouya.console.api.OuyaController;

public class e_Player {
	public float x = 608;
	public float y = 328;
	public float speed = 420;
	
	public float dodgetimer = 0;
	public float dodgecool = 0;
	public float dodgestartx = -1;
	public float dodgestarty = -1;
	public float dodgeendx = -1;
	public float dodgeendy = -1;
	public float dodgeangle = -10;
	public float dmgtimer = 0;
	
	public float preva = -10;
	public float prevca = -10;
	public float aim = -10;
	
	public int num;
	public Color hand;
	public Color trail;
	
	public e_Projectile grabbed = null;
	public float turnspeed = 0;
	public boolean grabblock = false;
	
	public boolean godspeed = false;
	public float godspeedbar = 0;
	
	public e_Player(int n) {
		num = n;
		if (num==0) {
			trail = new Color(0.91f, 0.77f, 0.69f);
			hand = new Color(1f, 0.7f, 0.5f);
		} else if (num==1) {
			trail = new Color(0.66f, 0.8f, 1);
			hand = new Color(0.5f, 0.7f, 1);
		} else if (num==2) {
			trail = new Color(1, 0.66f, 0.66f);
			hand = new Color(1, 0.5f, 0.5f);
		}
	}
	
	public void update(float t,OuyaController c,Sprite s,Sprite h) {
		if (!(Main.root.scene instanceof s_Multi)) {
			if (godspeed) {
				godspeedbar-=t/12;
				if (godspeedbar<=0) {
					Main.gsmusic.pause();
					Main.gsmusic.seekTo(0);
					godspeed=false;
					godspeedbar=0;
					Main.multi=1;
				}
			}
			if (c.buttonPressedThisFrame(OuyaController.BUTTON_Y) && godspeedbar>=1) {
				Main.gsmusic.resume();
				godspeed=true;
				godspeedbar=1;
				Main.multi=2;
			}
			
			if (c.buttonPressedThisFrame(OuyaController.BUTTON_O)) u_Projectiles.addBakedGood(x+32, y+32);
			//if (c.buttonPressedThisFrame(OuyaController.BUTTON_R3)) Main.multi=Math.abs(Main.multi-1);
		}
		
		float xa = c.getAxisValue(OuyaController.AXIS_LS_X);
		float ya = c.getAxisValue(OuyaController.AXIS_LS_Y);
		if (xa * xa + ya * ya < Main.deadzone * Main.deadzone) {
			  xa = ya = 0.0f;
		}
		
		if (dodgetimer>0) dodgetimer--;
		if (dodgecool>0) dodgecool--;
		if (dmgtimer>0) dmgtimer--;
		
		if (dodgetimer>0) {
			float p = (float)Math.pow(dodgetimer/30,5);
			float nx = limit(false,dodgeendx+(dodgestartx-dodgeendx)*p,dodgeendy+(dodgestarty-dodgeendy)*p);
			float ny = limit(true,dodgeendy+(dodgestarty-dodgeendy)*p,dodgeendx+(dodgestartx-dodgeendx)*p);
			u_Trail.doTrail(x+24,y+24,nx+24,ny+24,hand,0.25f,2,3.5f+((godspeed) ? 1.5f : 0));
			x=nx;
			y=ny;
			s.setPosition(x,y);
			s.setRotation(dodgeangle);
		}
		
		if (xa != 0.0f || ya != 0.0f) {
			if (dodgecool<=0 && c.buttonPressedThisFrame(OuyaController.BUTTON_L1)) {
				Main.dodge.play();
				dodgetimer=29;
				dodgecool=59;
				dodgestartx=x;
				dodgestarty=y;
				dodgeendx=x+xa*200;
				dodgeendy=y+ya*200;
				dodgeangle=((float)Math.atan2(-ya, xa) * ((float)-180/(float)Math.PI));
			}
			if (dodgetimer<=0 && dmgtimer<=0) {
				float a = ((float)Math.atan2(-ya, xa) * ((float)-180/(float)Math.PI));
				if (a < 0) a += 360;
				if (s!=null) s.setRotation(a);
				
				u_Trail.doTrail(x+24,y+24,limit(false,x+xa*speed*t+24,y+ya*speed*t+24),limit(true,y+ya*speed*t+24,x+xa*speed*t+24),trail,0.25f,1,3.5f+((godspeed) ? 1.5f : 0));
			}
		} else if (godspeed) u_Trail.doTrail(x+24,y+24,limit(false,x+xa*speed*t+24,y+ya*speed*t+24),limit(true,y+ya*speed*t+24,x+xa*speed*t+24),trail,0.25f,1,3.5f+((godspeed) ? 1.5f : 0));
		
		if (!c.getButton(OuyaController.BUTTON_R1)) grabblock = false;
		float xa2 = c.getAxisValue(OuyaController.AXIS_RS_X);
		float ya2 = c.getAxisValue(OuyaController.AXIS_RS_Y);
		if (xa2 * xa2 + ya2 * ya2 < Main.deadzone * Main.deadzone) {
			  xa2 = ya2 = 0.0f;
		}
		if (xa2 == 0.0f && ya2 == 0.0f && !c.getButton(OuyaController.BUTTON_R1)) {
			h.setPosition(x+24,y+24);
			if (grabbed != null) {
				grabbed.spr.setPosition(x+16-(float)Math.sin(preva-(float)Math.PI/2)*54,y+16-(float)Math.cos(preva-(float)Math.PI/2)*54);
				grabbed.grabbed = false;
				grabbed.xs = (float)Math.sin(aim+(float)Math.PI/2)*Math.abs(turnspeed)*1800;
				grabbed.ys = (float)Math.cos(aim+(float)Math.PI/2)*Math.abs(turnspeed)*1800;
				if (Math.sqrt(grabbed.xs*grabbed.xs+grabbed.ys*grabbed.ys)>360) {
					grabbed.thrown = true;
					Main.thrown.play();
				}
				grabbed.thrownbyplayer = num;
				grabbed = null;
			}
			preva = prevca = -10;
			turnspeed = 0;
		} else {
			if (grabbed != null) {
				if (!c.getButton(OuyaController.BUTTON_R1)) {
					grabbed.spr.setPosition(x+16-(float)Math.sin(preva-(float)Math.PI/2)*54,y+16-(float)Math.cos(preva-(float)Math.PI/2)*54);
					grabbed.grabbed = false;
					grabbed.xs = (float)Math.sin(aim+(float)Math.PI/2)*Math.abs(turnspeed)*1800;
					grabbed.ys = (float)Math.cos(aim+(float)Math.PI/2)*Math.abs(turnspeed)*1800;
					if (Math.sqrt(grabbed.xs*grabbed.xs+grabbed.ys*grabbed.ys)>360) {
						grabbed.thrown = true;
						Main.thrown.play();
					}
					grabbed.thrownbyplayer = num;
					grabbed = null;
				} else {
					if (xa2 != 0.0f || ya2 != 0.0f) {
						float a2 = (float)Math.atan2(-ya2, xa2);
						float dist = (a2-prevca);
						if (dist>Math.PI) dist -= Math.PI*2;
						else if (dist<-Math.PI) dist += Math.PI*2;
						if (dist<=0.15 && dist>=-0.15) {
							if (turnspeed>0) turnspeed -= 0.001;
							else if (turnspeed<0) turnspeed += 0.001;
							dist=0;
						} else {
							turnspeed += (dist-turnspeed)/30;
						}
						prevca = aim = a2;
					} else {
						if (turnspeed>0) turnspeed -= 0.0025;
						else if (turnspeed<0) turnspeed += 0.0025;
						prevca = -10;
					}
					float a2 = preva+(turnspeed*60*t);
					if (a2>Math.PI*2) a2 -= Math.PI*2;
					else if (a2<-Math.PI*2) a2 += Math.PI*2;
					float nx = x+24-(float)Math.sin(a2-(float)Math.PI/2)*54;
					float ny = y+24-(float)Math.cos(a2-(float)Math.PI/2)*54;
					if (preva!=-10) u_Trail.doRoundTrail(x+24,y+24,x+xa*speed*t+24,y+ya*speed*t+24,(float)54,preva-(float)Math.PI/2,a2-(float)Math.PI/2,trail,0.1f,(int)(Math.abs(preva-a2)*20)+((xa != 0.0f || ya != 0.0f) ? 1 : 0),0.8f);
					h.setPosition(nx,ny);
					grabbed.spr.setPosition(nx-8,ny-8);
					grabbed.spr.setRotation(aim * ((float)-180/(float)Math.PI));
					preva = a2;
				}
			} else {
				boolean doother = true;
				if (c.getButton(OuyaController.BUTTON_R1) && (xa2 != 0.0f || ya2 != 0.0f)) {
					e_Projectile p = u_Projectiles.testRange(h.getX()-8, h.getY()-8, 20, false, -2, false);
					if (p!=null && !grabblock) {
						if (p.grabbed && Main.root.scene instanceof s_Multi) {
							if (num==1) {
								((s_Multi) Main.root.scene).player2.grabbed=null;
								((s_Multi) Main.root.scene).player2.grabblock=true;
							} else if (num==2) {
								((s_Multi) Main.root.scene).player.grabbed=null;
								((s_Multi) Main.root.scene).player.grabblock=true;
							}
						}
						p.grabbed=true;
						grabbed=p;
						float xs = -p.xs*(float)Math.sin(preva);
						float ys = -p.ys*(float)Math.cos(preva);
						turnspeed = (Math.abs(xs)*xs+Math.abs(ys)*ys)/(float)1000/(float)1000;
						doother = false;
					}
				}
				if (doother) {
					turnspeed = 0;
					if (xa2 != 0.0f || ya2 != 0.0f) {
						float a2 = (float)Math.atan2(-ya2, xa2);
						float nx = x+24-(float)Math.sin(a2-(float)Math.PI/2)*54;
						float ny = y+24-(float)Math.cos(a2-(float)Math.PI/2)*54;
						if (preva!=-10) u_Trail.doRoundTrail(x+24,y+24,x+xa*speed*t+24,y+ya*speed*t+24,(float)54,preva-(float)Math.PI/2,a2-(float)Math.PI/2,trail,0.1f,(int)(Math.abs(preva-a2)*20)+((xa != 0.0f || ya != 0.0f) ? 1 : 0),0.8f);
						h.setPosition(nx,ny);
						preva = prevca = a2;
					} else {
						h.setPosition(x+24,y+24);
						preva = -10;
					}
				}
			}
		}
			
		if ((xa != 0.0f || ya != 0.0f) && dodgetimer<=0 && dmgtimer<=0) {
			x = limit(false,x+xa*speed*t,y+ya*speed*t);
			y = limit(true,y+ya*speed*t,x+xa*speed*t);
			if (s!=null) s.setPosition(x,y);
		}
		
		e_Projectile p = u_Projectiles.testRange(x+16, y+16, 32, true, num, false);
		if (p!=null && dmgtimer<=0 && !godspeed) {
			Main.hurt.play();
			dodgetimer=29;
			dodgecool=59;
			dmgtimer=59;
			dodgestartx=x;
			dodgestarty=y;
			float a = (float)Math.atan2(y+16-p.spr.getY(), x+16-p.spr.getX())+(float)Math.PI/2;
			dodgeendx=x+(float)Math.sin(a)*100;
			dodgeendy=y-(float)Math.cos(a)*100;
			dodgeangle=((a+(float)Math.PI/3*2) * ((float)-180/(float)Math.PI));
		}

		e_Enemy e = u_Enemy.testRange(x, y, 64, null, false, false);
		if (e!=null) {
			if (godspeed) e.forcedestroy=true;
			else {
				Main.hurt.play();
				dodgetimer=29;
				dodgecool=29;
				dmgtimer=29;
				dodgestartx=x;
				dodgestarty=y;
				float a;
				if (e.spr2==null) a = (float)Math.atan2(y-e.spr.getY(), x-e.spr.getX())+(float)Math.PI/2;
				else a = (float)Math.atan2(y+32-(e.spr.getY()+64), x+32-(e.spr.getX()+64))+(float)Math.PI/2;
				dodgeendx=x+(float)Math.sin(a)*100;
				dodgeendy=y-(float)Math.cos(a)*100;
				dodgeangle=((a+(float)Math.PI/3*2) * ((float)-180/(float)Math.PI));
			}
		}
	}
	
	public float limit(boolean isY,float n,float m) {
		if (Main.root.scene instanceof s_Multi) {
			if (n<48) n = 48;
			if (!isY) {
				if (n>1168) n = 1168;
			} else {
				if (n>608) n = 608;
			}
			
			if (!isY) {
				while ((n-48)+(m-48)<196) {
					n++; m++;
				}
				while ((n-48)+(-m+672-56)<196) {
					n++; m--;
				}
				while ((-n+1232-56)+(m-48)<196) {
					n--; m++;
				}
				while ((-n+1232-64)+(-m+672-64)<196) {
					n--; m--;
				}
				return n;
			} else {
				while ((m-48)+(n-48)<196) {
					m++; n++;
				}
				while ((m-48)+(-n+672-56)<196) {
					m++; n--;
				}
				while ((-n+1232-56)+(m-48)<196) {
					m--; n++;
				}
				while ((-n+1232-64)+(-m+672-64)<196) {
					m--; n--;
				}
				return n;
			}
		} else {
			if (n<70) return 70;
			if (!isY) {
				if (n>1146) return 1146;
			} else {
				if (n>586) return 586;
			}
		}
		return n;
	}
}
