package org.vostan.sokoban.play;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import org.vostan.sokoban.App;

import static org.vostan.sokoban.App.theApp;
import org.vostan.sokoban.R;
import org.vostan.sokoban.Puzzle;

public class PlayActivity extends FragmentActivity
							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)
    {
        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 );
		//
		//	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();
    }

	public void onSolved()
	{
		//
		//	Advance current level and achieved level.
		//
		final int nextl = theApp().getCurrentLevel()+1;
		theApp().advanceAchivedLevel(nextl);
		theApp().setCurrentLevel(nextl);
		//
		//	Bring a dialog for user to choose to continue or stop.
		//
		DialogFragment df = new DialogFragment()
		{
			public Dialog onCreateDialog(Bundle savedInstanceState)
			{
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setTitle("Congratulations! You Won!");
				builder.setMessage( "Would you like to try the next puzzle?" );
				builder.setNegativeButton("Enough",
						new DialogInterface.OnClickListener()
						{
							public void onClick(DialogInterface d, int w)
							{
								finish();						
							}
						});
				builder.setPositiveButton("Please",
						new DialogInterface.OnClickListener()
						{
							public void onClick(DialogInterface d, int w)
							{
								loadCurrentPuzzle();
								puzzle_view.invalidate();
							}
						});
				// Create the AlertDialog object and return it
				Dialog dlg = builder.create();
				dlg.setCancelable(false);
				dlg.setCanceledOnTouchOutside(false);
				return dlg;
			}
		};
		df.setCancelable(false);
		df.show(getSupportFragmentManager(), null);
		//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;
	}
	//
	//	Go to previouse puzzle.
	//
	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;
	}
	//
	//	Go to next puzzle.
	//
	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;
	}
	//
	//	Undo last action.
	//
	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);
		updateTitle();
	}
	public void updateTitle()
	{
		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);
		}
	}
}
