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

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

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import org.vostan.banvor.board.PuzzleControl;
import org.vostan.banvor.databinding.FragmentPuzzleBoardBinding;
import org.vostan.banvor.game.State;

/**
 *
 */
public class PuzzleBoardFragment extends Fragment
{
	private FragmentPuzzleBoardBinding binding;
	private State gameState;


	@Override
	public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		gameState = theApp().state();
		binding = FragmentPuzzleBoardBinding.inflate(inflater, container, false);
		binding.btnTest.setVisibility(getResources().getBoolean(R.bool.debug_visible) ? View.VISIBLE :  View.GONE);
		return binding.getRoot();
	}

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

		int gameLevel = PuzzleBoardFragmentArgs.fromBundle(getArguments()).getLevel();
		gameState.setCurrentLevel(gameLevel);

		initAndShowCurrentPuzzle();

		binding.gameBoard.setPuzzleControlLister(new PuzzleControl.PuzzleControlLister() {
			@Override
			public void onSolved() {
				PuzzleBoardFragment.this.onSolved();
			}

			@Override
			public void onLongPress() {
			}

			@Override
			public void onTouch() {
			}
		});
		binding.btnTest.setOnClickListener((View.OnClickListener) v -> {
			createNextLevelDialog((d, w) -> {
				initAndShowCurrentPuzzle();
			}).show();
		});
		binding.btnPrev.setOnClickListener((View.OnClickListener) v -> {
			gameState.setCurrentLevel(gameState.getCurrentLevel()-1);
			PuzzleBoardFragment.this.initAndShowCurrentPuzzle();
		});
		binding.btnNext.setOnClickListener((View.OnClickListener) v -> {
			gameState.setCurrentLevel(gameState.getCurrentLevel()+1);
			PuzzleBoardFragment.this.initAndShowCurrentPuzzle();
		});
		binding.btnReset.setOnClickListener((View.OnClickListener) v -> {
			PuzzleBoardFragment.this.initAndShowCurrentPuzzle();
		});
		binding.btnUndo.setOnClickListener((View.OnClickListener) v -> {
			binding.gameBoard.undoLastMove();
		});
	}

	private void initAndShowCurrentPuzzle() {
		int gameLevel = gameState.getCurrentLevel();
		binding.levelNumber.setText(Integer.toString(gameLevel));
		binding.gameBoard.setPuzzle(gameState.getCurrentPuzzle());
	}

	private void onSolved(){
		//
		//	Since the puzzle is solved, we need to unlock next level if still locked.
		//
		gameState.levelSolved(gameState.getCurrentLevel());
		//
		//	Bring a dialog for user to choose to continue or stop.
		//
		createNextLevelDialog((d, w) -> {
			gameState.advanceCurrentLevel();
			initAndShowCurrentPuzzle();
		}).show();
	}

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

	private Dialog createNextLevelDialog(DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.BanvorDialog);
		builder.setTitle("Congratulations! You Won!");
		builder.setMessage( "Would you like to try the next puzzle?" );
		builder.setPositiveButton("Yes, please.", listener);
		// Create the AlertDialog object and return it
		Dialog dlg = builder.create();
		dlg.setCancelable(true);
		return dlg;
	}
}
