package org.dyndns.vahagn.sokoban.play;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
import java.util.zip.Inflater;
import org.dyndns.vahagn.sokoban.App;
import static org.dyndns.vahagn.sokoban.App.TAG;
import static org.dyndns.vahagn.sokoban.App.theApp;
import org.dyndns.vahagn.sokoban.R;
import org.dyndns.vahagn.sokoban.Puzzle;
import org.dyndns.vahagn.sokoban.menu.MainActivity;

public class PlayActivity extends Activity
							implements PuzzleControl.PuzzleControlLister
{
	protected final static int RESET_ITEM = 1;
	protected final static int UNDO_ITEM = 2;
	
	public Puzzle puzzle = null;
	public PuzzleControl puzzle_view;
	View title_view;
	public TextView title_text;

	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.d(TAG, "onCreate: " + savedInstanceState );
        super.onCreate(savedInstanceState);
		//
		//	No title.
		//
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//
		//	Create and set up the puzzle_view.
		//
		puzzle_view = new PuzzleControl( this );
		setContentView( puzzle_view );
		puzzle_view.setPuzzleControlLister( this );
		registerForContextMenu(puzzle_view);
		//
		//	Create action bar.
		//
		FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
		View.inflate(this, R.layout.puzzle_view_title, rootLayout);
		title_text = (TextView)findViewById(R.id.title_text);
		title_view = findViewById(R.id.puzzle_view_title_layout);
		//
		//	Load the puzzle.
		//
		loadCurrentPuzzle();

		if (savedInstanceState == null)
		{
            // We were just launched -- set up a new game
//            mSnakeView.setMode(SnakeView.READY);
        }
		else
		{
            // We are being restored
//            Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
//            if (map != null) {
//                mSnakeView.restoreState(map);
//            } else {
//                mSnakeView.setMode(SnakeView.PAUSE);
//            }
        }
//        
//        
//    	
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.menu);
//        
//        try {
//			theApp().getPuzzleContainer().getPuzzle( 0 );
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    }

	public void onSolved()
	{
		//
		//	Advance current level and achieved level.
		//
		theApp().advanceCurrentLevel();
		//
		//	Bring a dialog for user to choose to continue or stop.
		//
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("");
		builder.setMessage( "Congratulations! You Won!" );
		builder.setNegativeButton("Menu",
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface d, int w)
					{
						finish();						
					}
				});
		builder.setPositiveButton("Next",
				new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface d, int w)
					{
						loadCurrentPuzzle();
						puzzle_view.invalidate();
					}
				});
		AlertDialog dlg = builder.create();
		dlg.show();
	}
	
	public void onLongPress()
	{
		if ( title_view.getVisibility() != View.VISIBLE )
			showTitle();
	}

	public void onTouch()
	{
		if ( title_view.getVisibility() == View.VISIBLE )
			hideTitle();
	}

	public void showTitle()
	{
		Animation anim = AnimationUtils.loadAnimation(this, R.animator.puzzle_action_bar_enter);
		title_view.startAnimation(anim);
		title_view.setVisibility(View.VISIBLE);
	}

	public void hideTitle()
	{
		Animation anim = AnimationUtils.loadAnimation(this, R.animator.puzzle_action_bar_exit);
		title_view.startAnimation(anim);
		title_view.setVisibility(View.INVISIBLE);
	}
			
	public boolean onReset( View v )
	{
		while ( puzzle.isUndoable() )
			puzzle.restore();
		puzzle_view.invalidate();
		return true;
	}

	public boolean onPrev( View v )
	{
		//
		//	If current level is less than achived level the move to next
		//	puzzle.
		//
		if ( theApp().getCurrentLevel() > App.MIN_LEVEL )
		{
			theApp().setCurrentLevel( theApp().getCurrentLevel()-1 );
			loadCurrentPuzzle();
			puzzle_view.invalidate();
		}
		return true;
	}

	public boolean onNext( View v )
	{
		//
		//	If current level is less than achived level the move to next
		//	puzzle.
		//
		if ( theApp().getCurrentLevel() < theApp().getAchivedLevel() )
		{
			theApp().setCurrentLevel( theApp().getCurrentLevel()+1 );
			loadCurrentPuzzle();
			puzzle_view.invalidate();
		}
		return true;
	}

	public boolean onUndo( View v )
	{
		if ( puzzle.isUndoable() )
		{
			puzzle.restore();
			puzzle_view.invalidate();
			return true;
		}
		else
			return false;
	}

	private void loadCurrentPuzzle()
	{
		puzzle = theApp().getCurrentPuzzle();
		puzzle_view.setPuzzle(puzzle);

		String title = "Level " + new Integer(theApp().getCurrentLevel()).toString();
		setTitle(title);
	}

	public void setTitle(CharSequence title)
	{
		super.setTitle(title);
		if (title_text != null) {
			title_text.setText(title);
		}
	}
}
