package com.rehivetech.beeeon.gui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.gui.adapter.IntroFragmentPagerAdapter;
import com.viewpagerindicator.CirclePageIndicator;

/**
 * Class that provides the common features for AddSensorAcitivity and AddGateActivity, both Activities inherit from this one
 */
public abstract class BaseGuideActivity extends BaseApplicationActivity{
	protected Button mSkip;
	protected Button Cancel;
	protected Button mNext;

	protected IntroFragmentPagerAdapter mPagerAdapter;
	protected ViewPager mPager;
	protected CirclePageIndicator mIndicator;
	protected Toolbar mToolbar;

	@Override
	protected void onCreate(Bundle savedInstanceData) {
		super.onCreate(savedInstanceData);

		mPagerAdapter = initPagerAdapter();

		setContentView(R.layout.activity_intro);

		mPager = (ViewPager) findViewById(R.id.intro_pager);
		mPager.setAdapter(mPagerAdapter);
		mPager.setOffscreenPageLimit(mPagerAdapter.getCount());

		mToolbar = (Toolbar) findViewById(R.id.toolbar);
		if (mToolbar != null) {
			setSupportActionBar(mToolbar);
			getSupportActionBar().setHomeButtonEnabled(true);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}

		mIndicator = (CirclePageIndicator) findViewById(R.id.intro_indicator);
		mIndicator.setViewPager(mPager);

		mIndicator.setPageColor(0x88FFFFFF);
		mIndicator.setFillColor(0xFFFFFFFF);
		mIndicator.setStrokeColor(0x88FFFFFF);
		mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				if (position == mPagerAdapter.getCount() - 1) {
					mSkip.setVisibility(View.INVISIBLE);
					mNext.setText(getLastPageNextTextResource());
					//InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					//imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
				} else {
					mSkip.setVisibility(View.VISIBLE);
					mNext.setText(R.string.tutorial_next);
				}
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});

		initButtons();
	}

	protected void initButtons() {
		mSkip = (Button) findViewById(R.id.add_gate_skip);
		Cancel = (Button) findViewById(R.id.add_gate_cancel);
		mNext = (Button) findViewById(R.id.add_gate_next);

		mSkip.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mPager.setCurrentItem(mPagerAdapter.getCount());
			}
		});


		Cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				closeActivity();
			}
		});

		mNext.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mPager.getCurrentItem() == mPagerAdapter.getCount() - 1) {
					onLastFragmentActionNext();
				} else {
					mPager.setCurrentItem(mPager.getCurrentItem() + 1);
				}
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
			case android.R.id.home:
				closeActivity();
		}
		return super.onOptionsItemSelected(item);
	}

	protected void closeActivity() {
		setResult(Activity.RESULT_CANCELED);
		finish();
	}

	public void setNextButtonEnabled(boolean enabled){
		mNext.setEnabled(enabled);
	}

	protected abstract void onLastFragmentActionNext();

	protected abstract IntroFragmentPagerAdapter initPagerAdapter();

	protected abstract int getLastPageNextTextResource();
}

@@ -826,37 +826,8 @@
 tor;
-%0A%09protected Toolbar mToolbar;
 %0A%0A%09@
@@ -1163,235 +1163,8 @@
 );%0A%0A
-%09%09mToolbar = (Toolbar) findViewById(R.id.toolbar);%0A%09%09if (mToolbar != null) %7B%0A%09%09%09setSupportActionBar(mToolbar);%0A%09%09%09getSupportActionBar().setHomeButtonEnabled(true);%0A%09%09%09getSupportActionBar().setDisplayHomeAsUpEnabled(true);%0A%09%09%7D%0A%0A
 %09%09mI
@@ -2951,205 +2951,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public boolean onOptionsItemSelected(MenuItem item) %7B%0A%0A%09%09switch (item.getItemId()) %7B%0A%09%09%09case android.R.id.home:%0A%09%09%09%09closeActivity();%0A%09%09%7D%0A%09%09return super.onOptionsItemSelected(item);%0A%09%7D%0A%0A
 %09pro

Remove Toolbar from GuideActivities