package java.game;

import java.io.*;
import java.util.*;
import java.util.resource.*;
import java.game.*;
import java.render.*;	//Text
import java.render.osd.*;	//Text
import java.sound.*;
import java.render.osd.dialog.*; //Dialog Window

import java.game.parts.*;
import java.game.parts.enginepart.*;

public class AVTODROM_PDD extends Track
{	
	final static String MapName = "AVTODROM_PDD";
	final static String MapDescription = " \n + BONUS BAGI =) \n  \n Track Wizard 2.0";
	final static String MapAuthor = "author : serega2000";
	final static ResourceRef MapImage = new ResourceRef(Maps.Addon_Maps.AVTODROM_PDD:0x0005r); // Image for track selector
	final static float	TS_Version = 1.0; // Do not change it!

	
	//Drift points system by Roltzy
	Text pointsText, totalpointsText, messageText;
	float	ang, ang2, slip, points, totalpoints, timewarp;
	Vector3 curpos, oldpos;
	float pi = 3.14159265;
	int drifting, enddrifting, paused = 0;

	public AVTODROM_PDD()
	{
		posStart = new Vector3( 0.000, 0.000, 0.000 ); // Start car position
		oriStart = new Ypr( 0.000, 0.000, 0.000 ); // Start car rotation

		map = new GroundRef( Maps.Addon_Maps.AVTODROM_PDD:0x00000001r );
		//nav = new Navigator( -5.317-2.64, -15.996, 2.64, maps.test_track.smallmap:0x00000001r, maps.test_track.smallmap:0x00000002r, maps.test_track.smallmap:0x00000005r, 6, 8, 10 ); // GPS map
	}

	public void StartMap()
	{ // Track selector run this void when you click "TAKE IT"
		GameLogic.changeActiveSection( new AVTODROM_PDD() );
	}

	public void enter( GameState prev_state )
	{
		Frontend.loadingScreen.show();
		GfxEngine.flush();

		super.enter( prev_state );
			
		curpos = player.car.getPos();
		oldpos = curpos;

		totalpointsText = osd.createText( "Points: 0", Frontend.largeFont, Text.ALIGN_RIGHT, 0.9, -0.9 );
		pointsText = osd.createText( " ", Frontend.largeFont, Text.ALIGN_CENTER, 0.0, -0.7 );
		messageText = osd.createText( " ", Frontend.largeFont, Text.ALIGN_CENTER, 0.0, -0.6 );

		addTimer( 0.06, 0 );
		
		setEventMask( EVENT_COLLISION );
        addNotification( GameLogic.player.car, EVENT_COLLISION, EVENT_SAME, null );
	}
	
	public void animate()
	{
        if(player.car)
        {
			if(drifting)
			{
				points += (timewarp*slip*player.car.getSpeedSquare())/100;
				pointsText.changeText( Float.toString(points, "%1.0f") );
				ColorMessage();
			}
			else
			{
				totalpoints += points;
				pointsText.changeText( " " );
				points = 0;
				messageText.changeText( " ");
			}
				
			totalpointsText.changeText( Float.toString(totalpoints, "Points: %1.0f") );
			timewarp = System.timeWarp(-1.0);
        }
		super.animate();
	}
	
	public void handleEvent( GameRef obj_ref, int event, int param )
	{
		super.handleEvent( obj_ref, event, param );

		if( event == EVENT_TIME )
		{
			if(player.car)
			{
				oldpos = curpos;
				curpos = player.car.getPos();
				Ypr carYpr = player.car.getOri();
				ang = (Math.abs((Math.abs(carYpr.y)-(pi/2))));
				
				float xlength = (Math.abs(Math.abs(oldpos.x)-Math.abs(curpos.x)));
				float ylength = (Math.abs(Math.abs(oldpos.z)-Math.abs(curpos.z)));
				
				if( oldpos.x != curpos.x && oldpos.z != curpos.z )
				{
					ang2 = (Math.atan(ylength/xlength));
				}
				else
					ang2 = ang;
					
				slip = (Math.abs(ang-ang2));
				
				if(slip >= 0.2 && player.car.getSpeedSquare() >= 90 && carYpr.p <= 0.55 && carYpr.p >= -0.55 && carYpr.r <= 0.55 && carYpr.r >= -0.55)
				{
					drifting = 1;
					enddrifting = 0;
				}
				else if(drifting)
				{
					if(enddrifting)
					{
						drifting = 0;
					}
					enddrifting = 1;
				}
			}			
		}		
	}
	
	public void handleEvent( GameRef obj_ref, int event, String param )
	{
		if( event == EVENT_COLLISION )
		{
			drifting = 0;
		}
	}
	
	public void ColorMessage()
	{
		if (points < 500)
		{
			messageText.changeText( " " );
		}
		else if (points >= 500 && points <= 1000)
		{
			messageText.changeText( "Good" );
			messageText.changeColor( 0xEFFFFF00 );
		}
		else if (points > 1000 && points <= 1750)
		{
			messageText.changeText( "Great" );
			messageText.changeColor( 0xEF00FF00 );
		}
		else if (points > 1750 && points <= 3000)
		{
			messageText.changeText( "Awesome" );
			messageText.changeColor( 0xEF0000FF );
		}
		else if (points > 3000 && points <= 4500)
		{
			messageText.changeText( "Outrageous!" );
			messageText.changeColor( 0xEFFF0000 );
		}
		else if (points > 4500)
		{
			messageText.changeText( "MONSTER DRIFT!" );
			messageText.changeColor( 0xEFFF00FF );
		}
	}
	

	public void exit( GameState next_state )
	{
		super.exit( next_state );
	}
}
