package org.vostan.sokoban.menu;

import org.vostan.sokoban.play.PlayActivity;

import android.content.Intent;
import android.view.View;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;

import org.vostan.sokoban.R;
import static org.vostan.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;
		/*
		getSupportFragmentManager()
			.beginTransaction()
			.setCustomAnimations(R.animator.fragment_slide_right_enter,
								R.animator.fragment_slide_right_exit)
			.commit();
			*/
		if ( -puzzleListXScroll/puzzleListXScrollMax > 0.5 )
		{
			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 );
		}
    }
}
