package com.dubiousoft.sakana;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Typeface;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SceneResultsActivity extends Activity {
	
	public final static String SCORE_MESSAGE = "com.dubiousoft.sakana.SCORE_MESSAGE";
	public final static String SCENE_INDEX_MESSAGE = "com.dubiousoft.sakana.SCENE_INDEX_MESSAGE";
	public final static String SCENE_NAME_MESSAGE = "com.dubiousoft.sakana.SCENE_NAME_MESSAGE";
	public final static String STAR_LOW_MESSAGE = "com.dubiousoft.sakana.STAR_LOW_MESSAGE";
	public final static String STAR_MED_MESSAGE = "com.dubiousoft.sakana.STAR_MED_MESSAGE";
	public final static String STAR_HIGH_MESSAGE = "com.dubiousoft.sakana.STAR_HIGH_MESSAGE";
	
	private ScoreStore m_scoreStore;

	private int m_sceneIndex = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
							 WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_scene_results);
		
		Intent intent = getIntent();
		int score = intent.getIntExtra( SCORE_MESSAGE, 0 );
		m_sceneIndex = intent.getIntExtra( SCENE_INDEX_MESSAGE, 0 );
		String sceneName = intent.getStringExtra( SCENE_NAME_MESSAGE );
		int starLow = intent.getIntExtra( STAR_LOW_MESSAGE, 0 );
		int starMed = intent.getIntExtra( STAR_MED_MESSAGE, 0 );
		int starHigh = intent.getIntExtra( STAR_HIGH_MESSAGE, 0 );
		
		TextView highScoreText = (TextView)findViewById( R.id.high_score_text );
		m_scoreStore = new ScoreStore( this );
		int highScore = m_scoreStore.getScore( sceneName );
		if (score > highScore) {
			m_scoreStore.storeScore( sceneName, score );
			highScoreText.setText( "New High Score" );
		}
		else {
			highScoreText.setText( "High Score: " + Integer.toString( highScore ) );
		}

		// The display of this Activity can not be expressed in normal layout,
		// so we build it programmatically based on the display size
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize( size );
		
		// In landscape view the size is all determined by the height
		Point position = new Point( size.x/2 - 8*size.y/30, size.y/2 + size.y/10 );
		Point dimensions = new Point( 5*size.y/8, 4*size.y/10 );
		
		FrameLayout controlView = (FrameLayout)findViewById( R.id.control_view );
		controlView.setLayoutParams( new FrameLayout.LayoutParams( dimensions.x, dimensions.y ) );
		controlView.setX( position.x );
		controlView.setY( position.y );
		
		Typeface typeface = Typeface.createFromAsset( getAssets(), "EraserRegular.ttf" ); 
		TextView scoreText = (TextView)findViewById( R.id.score_text );
		scoreText.setTypeface( typeface );
		scoreText.setText( "Score: " + Integer.toString( score ));
		scoreText.setTextSize( Utils.px2dip(this, size.y/20) );
		scoreText.setX( 0 );
		scoreText.setY( dimensions.y/20 );	

		highScoreText.setTypeface( typeface );
		highScoreText.setTextSize( Utils.px2dip(this, size.y/20) );
		highScoreText.setX( 0 );
		highScoreText.setY( 2*dimensions.y/10 );	
		
		ImageButton backBtn = (ImageButton)findViewById( R.id.back_btn );
		backBtn.setLayoutParams( new FrameLayout.LayoutParams( size.y/8, size.y/8 ) );
		backBtn.setX( 5*dimensions.x/10 );
		backBtn.setY( 7*dimensions.y/12 );
		ImageButton forwardBtn = (ImageButton)findViewById( R.id.forward_btn );
		forwardBtn.setLayoutParams( new FrameLayout.LayoutParams( size.y/8, size.y/8 ) );
		forwardBtn.setX( 7*dimensions.x/10 );
		forwardBtn.setY( 7*dimensions.y/12 );
		
		ImageView star = (ImageView)findViewById( R.id.star_low );
		star.setLayoutParams( new FrameLayout.LayoutParams( size.y/10, size.y/10 ) );
		star.setX( 0 );
		star.setY( 4*dimensions.y/10 );
		if (score < starLow) {
			star.setVisibility( View.INVISIBLE );
			if (highScore < starLow) {
				forwardBtn.setVisibility( View.INVISIBLE );
			}
		}

		star = (ImageView)findViewById( R.id.star_med );
		star.setLayoutParams( new FrameLayout.LayoutParams( size.y/10, size.y/10 ) );
		star.setX( dimensions.x/6 );
		star.setY( 4*dimensions.y/10 );
		if (score < starMed) {
			star.setVisibility( View.INVISIBLE );
		}
		
		star = (ImageView)findViewById( R.id.star_high );
		star.setLayoutParams( new FrameLayout.LayoutParams( size.y/10, size.y/10 ) );
		star.setX( 2*dimensions.x/6 );
		star.setY( 4*dimensions.y/10 );
		if (score < starHigh) {
			star.setVisibility( View.INVISIBLE );
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scene_results, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
    	Intent intent = new Intent( this, SceneSelectionActivity.class );
    	intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
    	startActivity( intent );
	}

	public void onForwardClicked( View view ) {
    	playNextScene();
	}

	public void onBackClicked( View view ) {
    	replayScene();
	}

	private void replayScene() {
    	Intent intent = new Intent( this, GameScreenActivity.class );
    	intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
    	intent.putExtra( GameScreenActivity.SCENE_MESSAGE, m_sceneIndex );
    	startActivity( intent );
	}
	
	private void playNextScene() {
    	Intent intent = new Intent( this, GameScreenActivity.class );
    	intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
    	intent.putExtra( GameScreenActivity.SCENE_MESSAGE, m_sceneIndex+1 );
    	startActivity( intent );
	}
	
}
