package de.nicidienase.chaosflix.fragments;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v17.leanback.app.ErrorFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import de.nicidienase.chaosflix.R;

/**
 * Created by felix on 22.03.17.
 */

public class BrowseErrorFragment extends ErrorFragment{

	private static final boolean TRANSLUCENT = true;
	public static final String FRAGMENT = "fragmentId";
	private SpinnerFragment mSpinnerFragment;

	public static BrowseErrorFragment showErrorFragment(FragmentManager manager,int fragmentId){
		BrowseErrorFragment errorFragment = new BrowseErrorFragment();
		Bundle args = new Bundle();
		args.putInt(BrowseErrorFragment.FRAGMENT,fragmentId);
		errorFragment.setArguments(args);
		manager.beginTransaction().replace(fragmentId, errorFragment)
				.addToBackStack(null).commit();
		return errorFragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int fragmentId = getArguments().getInt(FRAGMENT);
		mSpinnerFragment = new SpinnerFragment();
		getFragmentManager().beginTransaction().add(fragmentId, mSpinnerFragment).commit();
	}

	@Override
	public void onStop() {
		super.onStop();
		getFragmentManager().beginTransaction().remove(mSpinnerFragment).commit();
	}

	public void setErrorContent(int resourceId){
		setErrorContent(getResources().getString(resourceId));
	}

	public void setErrorContent(String message) {
		setImageDrawable(getResources().getDrawable(R.drawable.lb_ic_sad_cloud, null));
		setMessage(message);
		setDefaultBackground(TRANSLUCENT);

		setButtonText(getResources().getString(R.string.dismiss_error));
		setButtonClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dismiss();
			}
		});
	}

	public void dismiss() {
		getFragmentManager().beginTransaction().remove(BrowseErrorFragment.this).commit();
		getFragmentManager().popBackStack();
	}


	public static class SpinnerFragment extends Fragment {
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
								 Bundle savedInstanceState) {
			ProgressBar progressBar = new ProgressBar(container.getContext());
			if (container instanceof FrameLayout) {
				Resources res = getResources();
				int width = res.getDimensionPixelSize(R.dimen.spinner_width);
				int height = res.getDimensionPixelSize(R.dimen.spinner_height);
				FrameLayout.LayoutParams layoutParams =
						new FrameLayout.LayoutParams(width, height, Gravity.CENTER);
				progressBar.setLayoutParams(layoutParams);
			}
			return progressBar;
		}
	}
}

@@ -2104,36 +2104,121 @@
 s() %7B%0A%09%09
-getFragmentManager()
+FragmentManager fragmentManager = getFragmentManager();%0A%09%09if(fragmentManager != null)%7B%0A%09%09%09fragmentManager
 .beginTr
@@ -2270,28 +2270,26 @@
 commit();%0A%09%09
-getF
+%09f
 ragmentManag
@@ -2290,18 +2290,16 @@
 tManager
-()
 .popBack
@@ -2303,24 +2303,28 @@
 ackStack();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%0A%09public

fix crash when going back while activity is launching