package cz.skaut.mlha.odymaterialy;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog.Builder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A fragment which shows a loading wheel. Also displays an error dialog when loading failed for some reason.
 */
@SuppressWarnings("WeakerAccess")
public class LoadingFragment extends Fragment
{
	/**
	 * Creates the root view.
	 */
	@Override
	public final View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment_loading, container, false);
	}

	/**
	 * Call when loading a lesson failed. Shows an error dialog, than returns to the previous fragment.
	 */
	public final void failLesson()
	{
		final Builder builder = new Builder(getActivity());
		builder.setCancelable(false);
		builder.setTitle(getString(R.string.lesson_load_fail_title));
		builder.setMessage(getString(R.string.lesson_load_fail_message));
		builder.setPositiveButton(getString(R.string.lesson_load_fail_ok), new OnClickListener()
		{
			@Override
			public void onClick(final DialogInterface dialogInterface, final int i)
			{
				getFragmentManager().popBackStack();
			}
		});
		builder.show();
	}

	/**
	 * Call when loading the Lesson list failed. Shows an error dialog.
	 */
	public final void failApp()
	{
		final Builder builder = new Builder(getActivity());
		builder.setCancelable(false);
		builder.setTitle(getString(R.string.app_load_fail_title));
		builder.setMessage(getString(R.string.app_load_fail_message));
		builder.setPositiveButton(getString(R.string.app_load_fail_ok), new EmptyOnClickListener());
		builder.show();
	}

	private static class EmptyOnClickListener implements OnClickListener
	{
		@Override
        public void onClick(final DialogInterface dialogInterface, final int i) {}
	}
}
