package com.dubiousoft.sakana;

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Typeface;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SceneSelectionActivity extends Activity 
	implements OnClickListener {

	private SceneManager m_sceneManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.LOG( "Starting up" );
		Utils.dumpScreenInfo( this );
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
							 WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_scene_selection);
		
		Typeface typeface = Typeface.createFromAsset( getAssets(), "EraserRegular.ttf" ); 
		TextView titleText = (TextView)findViewById( R.id.title_text ); 
		titleText.setTypeface( typeface );
		// The layout xml specifies that the titleText is 10% of the screen, but there's no way
		// to say that the textSize is also 10% of the screen.  That's what this does:
		titleText.setTextSize( Utils.px2dip( this, getResources().getDisplayMetrics().heightPixels/10 ) );

		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize( size );

		AssetManager mgr = getAssets();		
		m_sceneManager = new SceneManager( mgr );
		ScoreStore scoreStore = new ScoreStore( this );
		// clear out the scores to debug
		//scoreStore.DEBUG_clearScores( m_sceneManager );
		//
		
		TableLayout sceneTable = (TableLayout)findViewById( R.id.scene_grid );
		int pad = size.y/30;
		sceneTable.setPadding( pad, pad, pad, pad );
		TableRow row = null;
		for (int i=0; i<m_sceneManager.getSize(); ++i) {
			SceneManager.SceneNode sceneNode = m_sceneManager.getNode(i);
			SceneManager.SceneNode previousSceneNode = ((i==0) ? null : m_sceneManager.getNode(i-1));

			row = (TableRow)sceneTable.getChildAt( i/4 );
			try {
				ImageButton sceneBtn = (ImageButton)row.getChildAt( i % 4 );
				sceneBtn.setId( i );
				
				Bitmap bitmap = null;
				int previousHighScore = ((previousSceneNode==null) ? 0 : scoreStore.getScore( previousSceneNode.name ));
				int previousMinScore = ((previousSceneNode==null) ? 0 : previousSceneNode.scores.Min);
				if (previousHighScore < previousMinScore) {
					bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lock);
				}
				else {				
					InputStream stream = getAssets().open( sceneNode.image );
					bitmap = BitmapFactory.decodeStream( stream );
					sceneBtn.setOnClickListener( this );
				}				
				// TableRows resize themselves based on the height of their children.
				// My lock images and scene images are different sizes, which means 
				// rows without locks are of a different height then rows with locks. 
				// At this point in the View creation there is no size associated with
				// the view, so I can't resize based on parent view.  So I scale all
				// images up to the same large size, then the rows will be balanced,
				// and they will rescale them all back down to the same size.
				bitmap = Bitmap.createScaledBitmap( bitmap, 500, 500, false );
				sceneBtn.setImageBitmap( bitmap );
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Utils.LOG( "Finished creating SceneSelectionActivity" );
	}

	@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_selection, menu);
		return true;
	}

	@Override
	public void onClick(View view) {
    	Intent intent = new Intent( this, GameScreenActivity.class );
    	intent.putExtra( GameScreenActivity.SCENE_MESSAGE, view.getId() );
    	startActivity( intent );
	}

	public void showAboutDialog(View view) {
		AboutDialogFragment newFragment = new AboutDialogFragment();
	    newFragment.show(getFragmentManager(), "About Sakana");		
	}
		
}
