package com.nukeguys.fork.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import com.nukeguys.fork.R;
import com.nukeguys.fork.ui.adapter.CustomIndicatorPagerAdapter;
import com.nukeguys.fork.ui.adapter.CustomIndicatorPagerAdapter.OnTabReselectedListener;
import com.nukeguys.fork.ui.base.ForkBaseActivity;
import com.nukeguys.fork.ui.base.ForkBaseFragment;
import com.nukeguys.fork.ui.fragment.HomeForkListFragment;
import com.nukeguys.fork.ui.fragment.HomeOfficialListFragment;
import com.nukeguys.fork.ui.fragment.ProfileFragment;

public class HomeActivity extends ForkBaseActivity implements OnPageChangeListener, OnTabReselectedListener
{
	private Button						mBtnFork;
	private Button						mBtnOffical;
	private Button						mBtnProfile;

	private ViewPager					mViewPager;
	private CustomIndicatorPagerAdapter	mPagerAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fork_home);

		initActionBar();
		initView();
	}

	private void initActionBar()
	{
		final ActionBar actionBar = getSupportActionBar();

		actionBar.setDisplayShowTitleEnabled(false);

		// manifest의 activity속성에 android:logo를 지정하면 적용됨
		// 2.3.3에서는 직접 logo를 지정해 줘야 반영 되었음.
		actionBar.setLogo(R.drawable.ic_action_logo);
		actionBar.setDisplayUseLogoEnabled(true);
	}

	private void initView()
	{
		mBtnFork = (Button) findViewById(R.id.btn_hometab_fork);
		mBtnFork.setTag(mBtnFork.getText());
		mBtnOffical = (Button) findViewById(R.id.btn_hometab_official);
		mBtnOffical.setTag(mBtnOffical.getText());
		mBtnProfile = (Button) findViewById(R.id.btn_hometab_profile);
		mBtnProfile.setTag(mBtnProfile.getText());

		mViewPager = (ViewPager) findViewById(R.id.homtab_pager);

		mViewPager.setOffscreenPageLimit(2); // 모두 유지
		mPagerAdapter = new CustomIndicatorPagerAdapter(this, getSupportFragmentManager(),
				mViewPager);
		mPagerAdapter.addTab(mBtnFork, HomeForkListFragment.class, null);
		mPagerAdapter.addTab(mBtnOffical, HomeOfficialListFragment.class, null);
		mPagerAdapter.addTab(mBtnProfile, ProfileFragment.class, null);

		// 처음 0을 선택 하면 onPageSelected가 호출되지 않음.
		mViewPager.setCurrentItem(0);
		mPagerAdapter.onPageSelected(0);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.menu_create, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.action_create:
			Intent createIntent = new Intent(this, CreateActivity.class);
			// 연속으로 눌려도 하나만 나오도록.
			createIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(createIntent);
			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		Toast.makeText(this, "Selected " + item.getTitle(), Toast.LENGTH_SHORT).show();

		return true;
	}

	@Override
	public void onPageScrollStateChanged(int arg0)
	{}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2)
	{}

	@Override
	public void onPageSelected(int arg0)
	{}

	@Override
	public void onTabReselected(int index)
	{
		Fragment fragment = mPagerAdapter.getPageOfIndex(index);
		if (fragment instanceof ForkBaseFragment)
			((ForkBaseFragment) fragment).scrollToTop();
	}
}
