package org.dyndns.vahagn.sokoban.menu;

import org.dyndns.vahagn.sokoban.play.PlayActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import org.dyndns.vahagn.sokoban.R;
import static org.dyndns.vahagn.sokoban.App.theApp;

public class MainActivity extends FragmentActivity 
{
	protected final String TAG = "SokobanMenu";
	protected final String LEVEL= "org.dyndns.vahagn.sokoban.LEVEL";
	protected MainFragment mainFragment;
	protected PuzzleListFragment puzzleListFragment;
	protected float puzzleListXScroll;
	protected float puzzleListXScrollMax;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

		mainFragment = new MainFragment();
		puzzleListFragment = new PuzzleListFragment();
		puzzleListFragment.setOnPuzzleListAction(new PuzzleListFragment.OnPuzzleListAction() {
			public boolean onPuzzleGridDown() {
				return onPuzzleGridDown1();
			}
			public boolean onPuzzleGridXScroll(float x) {
				return onPuzzleGridXScroll1(x);
			}
			public void onPuzzleLevelClicked(int level) {
				onPuzzleClicked1(level);
			}
		});
		puzzleListXScrollMax = getWindowManager().getDefaultDisplay().getWidth();

		//
		//	Set initila fragment.
		//
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.add(R.id.fragment_container, mainFragment);
//		fragmentTransaction.add(R.id.fragment_container, puzzleListFragment);
		fragmentTransaction.commit();
    }
   	    
    //@Override
    public void onConfigurationChanged()
    {
       // super.onConfigurationChanged();
       // setContentView(R.layout-l.menu);
    }
    
    public void onStartCurrentPuzzle(View v)
    {
    	Intent intent = new Intent(this, PlayActivity.class);		
    	startActivity( intent );
    }
	
    public void onPuzzleListShow(View v)
    {
		getSupportFragmentManager()
			.beginTransaction()
			.setCustomAnimations(R.animator.fragment_slide_left_enter,
								R.animator.fragment_slide_left_exit)
			.replace(R.id.fragment_container, puzzleListFragment)
			.commit();
    }
	
	public boolean onPuzzleGridDown1()
	{
		puzzleListXScroll = 0;
		return true;
	}
	
	public boolean onPuzzleGridXScroll1( float x )
	{
		puzzleListXScroll += x;
		Log.d(TAG, Float.toString(puzzleListXScroll));
		/*
		getSupportFragmentManager()
			.beginTransaction()
			.setCustomAnimations(R.animator.fragment_slide_right_enter,
								R.animator.fragment_slide_right_exit)
			.commit();
			*/
		if ( -puzzleListXScroll/puzzleListXScrollMax > 0.5 )
		{
			Log.d(TAG, "annim");
			getSupportFragmentManager()
				.beginTransaction()
				.setCustomAnimations(R.animator.fragment_slide_right_enter,
									R.animator.fragment_slide_right_exit)
				.replace(R.id.fragment_container, mainFragment)
				.commit();
		}
		return true;
	}
		
	public void onPuzzleClicked1( int level )
    {
		if ( level <= theApp().getAchivedLevel() )
		{
			theApp().setCurrentLevel(level);
			Intent intent = new Intent(this, PlayActivity.class);
			startActivity( intent );
		}
    }
}
