package com.gh.gamecenter.game;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.base.HomeFragment;
import com.gh.common.util.DisplayUtils;
import com.gh.gamecenter.R;
import com.gh.gamecenter.eventbus.EBSkip;
import com.gh.gamecenter.eventbus.EBUISwitch;

import de.greenrobot.event.EventBus;

/**
 * 
 * @author 温冠超
 * @email 294299195@qq.com
 * @date 2015-8-8 主页fragment3页面
 */
public class GameFragment extends HomeFragment implements OnPageChangeListener {

	private TextView tv_chajian;
	private TextView tv_tuijian;
	private TextView tv_danji;

	private int width;
	private int currentItem;

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("currentItem", currentItem);
	}

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

		initTextView();

		home_vp_content.setAdapter(new FragmentAdapter(getChildFragmentManager()));
		home_vp_content.addOnPageChangeListener(this);

		currentItem = 0;
		if (savedInstanceState != null) {
			currentItem = savedInstanceState.getInt("currentItem");
		}
		home_vp_content.setCurrentItem(currentItem);

		DisplayMetrics outMetrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		width = outMetrics.widthPixels / 3;
		lparams = new LinearLayout.LayoutParams(width / 2,
				DisplayUtils.dip2px(getActivity(), 2));
		lparams.leftMargin = width * currentItem + width / 4;
		home_slide_line.setLayoutParams(lparams);

		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				EventBus.getDefault().post(new EBUISwitch("GameFragment", home_vp_content.getCurrentItem()));
			}
		}, 100);
	}

	private void initTextView() {
		tv_chajian = new TextView(getActivity());
		tv_chajian.setText("插件");
		tv_chajian.setTextColor(getResources().getColor(R.color.theme));
		tv_chajian.setGravity(Gravity.CENTER);
		tv_chajian.setOnClickListener(this);
		LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(
				0, DisplayUtils.dip2px(getActivity(), 35));
		lparams1.weight = 1;
		home_ll_top.addView(tv_chajian, lparams1);

		tv_tuijian = new TextView(getActivity());
		tv_tuijian.setText("推荐");
		tv_tuijian.setTextColor(getResources().getColor(R.color.title));
		tv_tuijian.setGravity(Gravity.CENTER);
		tv_tuijian.setOnClickListener(this);
		LinearLayout.LayoutParams lparams2 = new LinearLayout.LayoutParams(
				0, DisplayUtils.dip2px(getActivity(), 35));
		lparams2.weight = 1;
		home_ll_top.addView(tv_tuijian, lparams2);

		tv_danji = new TextView(getActivity());
		tv_danji.setText("单机");
		tv_danji.setTextColor(getResources().getColor(R.color.title));
		tv_danji.setGravity(Gravity.CENTER);
		tv_danji.setOnClickListener(this);
		LinearLayout.LayoutParams lparams3 = new LinearLayout.LayoutParams(
				0, DisplayUtils.dip2px(getActivity(), 35));
		lparams3.weight = 1;
		home_ll_top.addView(tv_danji, lparams3);
	}

	private class FragmentAdapter extends FragmentPagerAdapter {

		public FragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			if (position == 0) {
				return new Game1Fragment();
			} else if (position == 1) {
				return new Game2Fragment();
			} else {
				return new Game3Fragment();
			}
		}

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

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == tv_chajian) {
			home_vp_content.setCurrentItem(0);
		} else if (v == tv_tuijian) {
			home_vp_content.setCurrentItem(1);
		} else if (v == tv_danji) {
			home_vp_content.setCurrentItem(2);
		}
	}

	@Override
	public void onPageSelected(int position) {
		if (position == 0) {
			tv_chajian.setTextColor(getResources().getColor(R.color.theme));
			tv_tuijian.setTextColor(getResources().getColor(R.color.title));
			tv_danji.setTextColor(getResources().getColor(R.color.title));
		} else if (position == 1) {
			tv_chajian.setTextColor(getResources().getColor(R.color.title));
			tv_tuijian.setTextColor(getResources().getColor(R.color.theme));
			tv_danji.setTextColor(getResources().getColor(R.color.title));
		} else if (position == 2) {
			tv_chajian.setTextColor(getResources().getColor(R.color.title));
			tv_tuijian.setTextColor(getResources().getColor(R.color.title));
			tv_danji.setTextColor(getResources().getColor(R.color.theme));
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		if (positionOffset != 0) {
			lparams.leftMargin = (int) (width * (position + positionOffset) + width / 4);
			home_slide_line.setLayoutParams(lparams);
		} else {
			if (currentItem != home_vp_content.getCurrentItem()) {
				currentItem = home_vp_content.getCurrentItem();
				EventBus.getDefault().post(new EBUISwitch("GameFragment", currentItem));
			}
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {
		
	}

	public void onEventMainThread(EBUISwitch busNine) {
		if ("MainActivity".equals(busNine.getFrom())) {
			if (busNine.getPosition() == 0) {
				EventBus.getDefault().post(
						new EBUISwitch("GameFragment", home_vp_content.getCurrentItem()));
			}
		}
	}

	public void onEventMainThread(EBSkip skip) {
		if ("MainActivity".equals(skip.getType())) {
			home_vp_content.setCurrentItem(skip.getCurrentItem());
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		tv_chajian = null;
		tv_tuijian = null;
		tv_danji = null;
	}

}
