package com.rehivetech.beeeon.gui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.gui.activity.AddGateActivity;
import com.rehivetech.beeeon.gui.activity.AddSensorActivity;
import com.rehivetech.beeeon.gui.activity.IntroActivity;

public final class IntroImageFragment extends Fragment {
	private static final String KEY_CONTENT = "TestFragment:Content";
	private static final String KEY_TEXT = "TestFragment:Text";

	public static IntroImageFragment newInstance(int resourceImg, String text) {
		IntroImageFragment fragment = new IntroImageFragment();

		fragment.mContent = resourceImg;
		fragment.mText = text;

		return fragment;
	}

	private int mContent = 0;
	private String mText = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getInt(KEY_CONTENT);
			mText = savedInstanceState.getString(KEY_TEXT);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		TextView text = new TextView(getActivity());
		text.setPadding(20, 20, 20, 20);
		text.setText(mText);
		text.setGravity(Gravity.CENTER_HORIZONTAL);
		text.setTextSize(20);
		text.setTextColor(getResources().getColor(R.color.white));

		ImageView image = new ImageView(getActivity());
		image.setImageResource(mContent);
		image.setPadding(20, 20, 20, 20);


		LinearLayout layout = new LinearLayout(getActivity());
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		layout.setGravity(Gravity.CENTER);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.addView(text);
		layout.addView(image);

		return layout;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			Context mActivity = getActivity();
			if (mActivity instanceof AddSensorActivity) {
				((AddSensorActivity) mActivity).resetBtn();
			} else if (mActivity instanceof IntroActivity) {
				if (((IntroActivity) mActivity).isLastFragment()) {
					((IntroActivity) mActivity).setLastFragmentBtn();
				} else {
					((IntroActivity) mActivity).resetBtn();
				}
			}
		}

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(KEY_CONTENT, mContent);
		outState.putString(KEY_TEXT, mText);
	}
}

@@ -445,67 +445,8 @@
 .R;%0A
-import com.rehivetech.beeeon.gui.activity.AddGateActivity;%0A
 impo
@@ -889,64 +889,140 @@
 %0A%0A%09%09
-fragment.mContent = resourceImg;%0A%09%09fragment.mText = text
+Bundle args = new Bundle();%0A%09%09args.putInt(KEY_CONTENT, resourceImg);%0A%09%09args.putString(KEY_TEXT, text);%0A%09%09fragment.setArguments(args)
 ;%0A%0A%09
@@ -1061,19 +1061,16 @@
 nt m
-Content = 0
+ImageRes
 ;%0A%09p
@@ -1092,13 +1092,8 @@
 Text
- = %22%22
 ;%0A%0A%09
@@ -1198,123 +1198,34 @@
 %0A%0A%09%09
-if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) %7B%0A%09%09%09mContent = savedInstanceState
+mImageRes = getArguments()
 .get
@@ -1248,17 +1248,16 @@
 ;%0A%09%09
-%09
 mText = 
 save
@@ -1252,34 +1252,30 @@
 mText = 
-savedInstanceState
+getArguments()
 .getStri
@@ -1288,20 +1288,16 @@
 _TEXT);%0A
-%09%09%7D%0A
 %09%7D%0A%0A%09@Ov
@@ -1717,23 +1717,24 @@
 source(m
-Content
+ImageRes
 );%0A%09%09ima
@@ -2597,192 +2597,6 @@
 %09%7D%0A%0A
-%09@Override%0A%09public void onSaveInstanceState(Bundle outState) %7B%0A%09%09super.onSaveInstanceState(outState);%0A%09%09outState.putInt(KEY_CONTENT, mContent);%0A%09%09outState.putString(KEY_TEXT, mText);%0A%09%7D%0A
 %7D%0A

Fix creating of intro image fragment