package com.espritmobile.sim.restocheck;

import java.util.Arrays;
import java.util.List;

import com.espritmobile.sim.restocheck.fragments.FirstUsePageFragment;
import com.espritmobile.sim.restocheck.fragments.FirstUsePageFragmentMini;
import com.facebook.Request;
import com.facebook.Request.GraphUserListCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.gc.materialdesign.views.ButtonRectangle;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class StartActivity extends Activity {
	ButtonRectangle skip;
	private UiLifecycleHelper uiHelper;
	private static final String TAG = "StartActivity";
	LoginButton authButton;
	private ViewPager mPager;
	private ViewPager mPagerMini;

	private PagerAdapter mPagerAdapter;
	private PagerAdapter mPagerAdapterMini;

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

		setContentView(R.layout.activity_start);

		// Instantiate a ViewPager and a PagerAdapter.
		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerMini = (ViewPager) findViewById(R.id.pager2);

		mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
		mPagerAdapterMini = new ScreenSlidePagerMiniAdapter(
				getFragmentManager());

		mPager.setAdapter(mPagerAdapter);
		mPagerMini.setAdapter(mPagerAdapterMini);

		mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				// When changing pages, reset the action bar actions since they
				// are dependent
				// on which page is currently active. An alternative approach is
				// to have each
				// fragment expose actions itself (rather than the activity
				// exposing actions),
				// but for simplicity, the activity provides the actions in this
				// sample.

				invalidateOptionsMenu();

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				mScrollState = arg0;
				if (mScrollState == ViewPager.SCROLL_STATE_IDLE)
					mPagerMini.setCurrentItem(mPager.getCurrentItem(), false);
			}

			int mScrollState = ViewPager.SCROLL_STATE_IDLE;

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				if (mScrollState == ViewPager.SCROLL_STATE_IDLE)
					return;
				mPagerMini.scrollTo((int) (mPager.getScrollX() * 0.54),
						(int) (mPager.getScrollY()));
			}
		});

		mPagerMini
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						// When changing pages, reset the action bar actions
						// since they are dependent
						// on which page is currently active. An alternative
						// approach is to have each
						// fragment expose actions itself (rather than the
						// activity exposing actions),
						// but for simplicity, the activity provides the actions
						// in this sample.

						invalidateOptionsMenu();

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
						// TODO Auto-generated method stub
						mScrollState = arg0;
						if (mScrollState == ViewPager.SCROLL_STATE_IDLE)
							mPager.setCurrentItem(mPagerMini.getCurrentItem(),
									false);
					}

					int mScrollState = ViewPager.SCROLL_STATE_IDLE;

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
						// TODO Auto-generated method stub
						if (mScrollState == ViewPager.SCROLL_STATE_IDLE)
							return;
						mPager.scrollTo((int) (mPagerMini.getScrollX() * 1.82),
								(int) (mPagerMini.getScrollY()));
					}
				});

		// FACEBOOK
		uiHelper = new UiLifecycleHelper(this, callback);
		uiHelper.onCreate(savedInstanceState);
		skip = (ButtonRectangle) findViewById(R.id.skip);
		skip.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent i = new Intent(StartActivity.this, MainActivity.class);
				startActivity(i);
				finish();
			}
		});

		authButton = (LoginButton) findViewById(R.id.login_button);
		authButton.setReadPermissions(Arrays.asList("user_friends",
				"public_profile"));
		Session session = Session.getActiveSession();
		if (session != null && session.isOpened()) {
			// authButton.setVisibility(View.INVISIBLE);
			Request.newMyFriendsRequest(session, new GraphUserListCallback() {

				@Override
				public void onCompleted(List<GraphUser> users, Response response) {
					// TODO Auto-generated method stub
					Log.i("users", response.getRawResponse());
					Log.i("users", response.getError().getErrorMessage());
					Log.i("users", users.size() + "");
					for (int i = 0; i < users.size(); i++) {
						Log.i("users", "users " + users.get(i).getName());
					}

				}
			});
		}

	}

	private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
		public ScreenSlidePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return FirstUsePageFragment.create(position);
		}

		@Override
		public int getCount() {
			return 3;
		}
	}

	private class ScreenSlidePagerMiniAdapter extends FragmentStatePagerAdapter {
		public ScreenSlidePagerMiniAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return FirstUsePageFragmentMini.create(position);
		}

		@Override
		public int getCount() {
			return 3;
		}
	}

	// FACEBOOK

	private void onSessionStateChange(Session session, SessionState state,
			Exception exception) {
		if (state.isOpened()) {
			Log.i(TAG, "Logged in...");
		} else if (state.isClosed()) {
			Log.i(TAG, "Logged out...");
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		// For scenarios where the main activity is launched
		// and user
		// session is not null, the session state change notification
		// may not be triggered. Trigger it if it's open/closed.
		Session session = Session.getActiveSession();
		if (session != null && (session.isOpened() || session.isClosed())) {
			onSessionStateChange(session, session.getState(), null);
		}

		uiHelper.onResume();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onPause() {
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		uiHelper.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		uiHelper.onSaveInstanceState(outState);
	}

	private Session.StatusCallback callback = new Session.StatusCallback() {

		@Override
		public void call(Session session, SessionState state,
				Exception exception) {
			// TODO Auto-generated method stub
			if (exception != null) {
				new AlertDialog.Builder(StartActivity.this).setTitle("ERREUR")
						.setMessage(exception.getMessage())
						.setPositiveButton("OK", null).show();
				Log.i("TAG", exception.getMessage());
			}

			onSessionStateChange(session, state, exception);
		}
	};

	// STOP FACEBOOK

}
