/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vostan.sokoban.menu;

import android.content.Context;
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.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import org.vostan.sokoban.R;

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

/**
 *
 */
public class PuzzleListFragment extends Fragment
{
	protected GridView puzzleGrid;
	protected PuzzlesAdapter puzzleGridAdapter; 
	protected GestureDetector gridViewXScrollDetector;
	protected OnPuzzleListAction actionListener;

	/*
	 * These are events which the fragments delegates to activity.
	 */
	public interface OnPuzzleListAction
	{
		public boolean onPuzzleGridDown();
		public boolean onPuzzleGridXScroll( float x );
		public void onPuzzleLevelClicked( int level );
	}
	public void setOnPuzzleListAction( OnPuzzleListAction l )
	{
		actionListener = l;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle)
	{
		super.onCreate(icicle);
		View v = inflater.inflate(R.layout.puzzle_list_fragment, container, false);
		//
		//	Configure GridView.
		//
		puzzleGrid = (GridView)v.findViewById(R.id.puzzle_grid);
		puzzleGridAdapter = new PuzzlesAdapter(getActivity());
		puzzleGrid.setAdapter( puzzleGridAdapter );
		puzzleGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id)
			{
				onPuzzleClicked(v,(int)id);
			}
		});
		puzzleGrid.setOnTouchListener( new View.OnTouchListener() {
			public boolean onTouch(View view, MotionEvent me) {
				gridViewXScrollDetector.onTouchEvent( me );
				return false;
			}
		});
		gridViewXScrollDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
			public boolean onDown(MotionEvent e) {
				return (actionListener!=null) ? actionListener.onPuzzleGridDown() : false;
			}
			public boolean onScroll(MotionEvent e1, MotionEvent e2, float dx, float dy) {
				return (actionListener!=null) ? actionListener.onPuzzleGridXScroll(dx) : false;
			}
		});
		
		return v;
	}
		
	@Override
	public void onResume()
	{
		super.onResume();
		if ( puzzleGrid != null )
			puzzleGridAdapter.notifyDataSetChanged();
	}

	public void onPuzzleClicked(View v, int level )
    {
		if (actionListener!=null)
			actionListener.onPuzzleLevelClicked(level);
    }
	
	public class PuzzlesAdapter extends BaseAdapter
	{
		private Context context;
		private int icon_size;
		private int text_x;
		private int text_y;
		private Bitmap lock_icon;
		private Bitmap unlock_icon;
		private Paint paint;

		public PuzzlesAdapter(Context c)
		{
			context = c;
			icon_size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 10, getResources().getDisplayMetrics() );
			int text_size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 4, getResources().getDisplayMetrics() );
			text_x = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 9, getResources().getDisplayMetrics() );
			text_y = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 9, getResources().getDisplayMetrics() );
			
			Bitmap lock_icon_tmp = BitmapFactory.decodeResource( getResources(), R.drawable.lock );
			lock_icon = Bitmap.createBitmap(icon_size,icon_size,Bitmap.Config.ARGB_8888);
			Canvas lock_canvas = new Canvas(lock_icon);
			lock_canvas.drawBitmap(lock_icon_tmp, 
				new Rect(0,0,lock_icon_tmp.getWidth()-1,lock_icon_tmp.getHeight()-1), 
				new Rect(0,0,icon_size, icon_size),
				null);
		
			Bitmap unlock_icon_tmp = BitmapFactory.decodeResource( getResources(), R.drawable.unlock );
			unlock_icon = Bitmap.createBitmap(icon_size,icon_size,Bitmap.Config.ARGB_8888);
			Canvas unlock_canvas = new Canvas(unlock_icon);
			unlock_canvas.drawBitmap(unlock_icon_tmp, 
				new Rect(0,0,unlock_icon_tmp.getWidth()-1,unlock_icon_tmp.getHeight()-1), 
				new Rect(0,0,icon_size, icon_size),
				null);
			
			paint = new Paint();
			paint.setColor( Color.WHITE );
			paint.setTextSize( text_size );
			paint.setTextAlign(Paint.Align.RIGHT);
		}

		public int getCount()
		{
			return theApp().getPuzzleCount();
		}

		public Object getItem(int position)
		{
			return null;
		}

		public long getItemId(int position)
		{
			return position+1;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ImageView imageView;
			if (convertView == null)
			{  // if it's not recycled, initialize some attributes
				imageView = new ImageView(context);
				imageView.setLayoutParams(new GridView.LayoutParams(icon_size, icon_size));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			}
			else
				imageView = (ImageView) convertView;

			int id = (int)getItemId(position);
			Bitmap icon = ( id <= theApp().getAchivedLevel() ) 
					? unlock_icon.copy(Bitmap.Config.ARGB_8888,true)
					: lock_icon.copy(Bitmap.Config.ARGB_8888,true);
			Canvas canvas = new Canvas(icon);
			canvas.drawText( Integer.toString(id), text_x, text_y, paint);
			imageView.setImageBitmap(icon);
			return imageView;
		}
	}

}
