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

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 androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import android.view.LayoutInflater;
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 static org.vostan.banvor.App.theApp;

import org.vostan.banvor.databinding.FragmentPuzzleListBinding;
import org.vostan.banvor.game.State;

/**
 *
 */
class PuzzlesAdapter extends BaseAdapter
{
	private State gameState;
	private Context context;
	private int icon_size;
	private int text_size;
	private int text_x;
	private int text_y;
	private Bitmap lock_icon;
	private Bitmap unlock_icon;
	private Paint paint;

	public PuzzlesAdapter(Context c)
	{
		super();

		gameState = theApp().state();
		context = c;
		icon_size = (int)c.getResources().getDimension(R.dimen.puzzle_list_icon_size);
		text_size = (int)c.getResources().getDimension(R.dimen.puzzle_list_number_size);
		text_x = (int)c.getResources().getDimension(R.dimen.puzzle_list_number_x_coord);
		text_y = (int)c.getResources().getDimension(R.dimen.puzzle_list_number_y_coord);

		//
		//	Load locked icon
		//
		Bitmap lock_icon_tmp = BitmapFactory.decodeResource( c.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);

		//
		//	Load unlock icon
		//
		Bitmap unlock_icon_tmp = BitmapFactory.decodeResource( c.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();
	}

	@Override
	public int getCount()
	{
		return gameState.getPuzzleCount();
	}

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

	public int getPositionLevel(int position)
	{
		return position+1;
	}

	@Override
	public long getItemId(int position)
	{
		return getPositionLevel(position);
	}

	// create a new ImageView for each item referenced by the Adapter
	@Override
	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;

		imageView.setImageBitmap(getIcon(position));
		return imageView;
	}

	private Bitmap getIcon(int position)
	{
		paint.setColor( Color.WHITE );
		paint.setTextSize( text_size );
		paint.setTextAlign(Paint.Align.RIGHT);

		int level = getPositionLevel(position);
		Bitmap icon = ( gameState.isLocked(level) )
				? lock_icon.copy(Bitmap.Config.ARGB_8888,true)
				: unlock_icon.copy(Bitmap.Config.ARGB_8888,true);

		Canvas canvas = new Canvas(icon);
		canvas.drawText( Integer.toString(level), text_x, text_y, paint);
		return icon;
	}
}


/**
 *
 */
public class PuzzleListFragment extends Fragment
{
	private FragmentPuzzleListBinding binding;
	private PuzzlesAdapter puzzleGridAdapter;
	private State gameState;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		gameState = theApp().state();
		binding = FragmentPuzzleListBinding.inflate(inflater, container, false);
		return binding.getRoot();
	}

	@Override
	public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		puzzleGridAdapter = new PuzzlesAdapter(getActivity());
		binding.puzzleGrid.setAdapter(puzzleGridAdapter);

		binding.puzzleGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				int level = puzzleGridAdapter.getPositionLevel(position);
				if (!gameState.isLocked(level)){
					PuzzleListFragmentDirections.ActionPuzzleListFragmentToPuzzleBoardFragment action =
					PuzzleListFragmentDirections.actionPuzzleListFragmentToPuzzleBoardFragment();
					action.setLevel(level);
					NavHostFragment.findNavController(PuzzleListFragment.this)
							.navigate(action);
				}
			}
		});
	}

	@Override
	public void onResume()
	{
		super.onResume();
		if ( binding != null && binding.puzzleGrid != null ) {
			puzzleGridAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void onDestroyView () {
		super.onDestroyView();
		binding = null;
	}
}
