package com.xhw.toucan.news.activity;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.xhw.toucan.news.R;
import com.xhw.toucan.news.fragment.HomeContentFragment;
import com.xhw.toucan.news.fragment.HomeMenuLeftFragment;
import com.xhw.toucan.news.utils.DimenUtils;

public class HomeActivity extends SlidingFragmentActivity
{
	private final static String TAG_HOME_CONTENT="tag_home_content";
	
	private final static String TAGH_HOME_MENU_LEFT="tagh_home_menu_left";

	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_home_content);
		//ÉèÖÃ×ó±ß²à±ßÀ¸
		this.setBehindContentView(R.layout.home_menu_left);
		//»ñÈ¡²à±ßÀ¸¶ÔÏó
		SlidingMenu slidingMenu = this.getSlidingMenu();
		//ÉèÖÃÎªÈ«ÆÁ»¬¶¯
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		//ÉèÖÃÖ÷½çÃæÔ¤Áô¿í¶È
		slidingMenu.setBehindOffset(DimenUtils.dp2px(this, 200));
		//³õÊ¼»¯Fragment
		initFragment();
	}

	/**
	 * ÇÐ»»²à±ßÀ¸×´Ì¬
	 */
	public void toggleSlidingMenu()
	{
		//»ñÈ¡²à±ßÀ¸¶ÔÏó
		SlidingMenu slidingMenu = this.getSlidingMenu();
		slidingMenu.toggle();
	}
	
	/**
	 * ³õÊ¼»¯Fragment
	 */
	private void initFragment()
	{
		FragmentManager fragmentManager = this.getSupportFragmentManager();
		FragmentTransaction trans = fragmentManager.beginTransaction();
		//ÉèÖÃTAGÎªÁËÒÔºóÄÜÕÒµ½Õâ¸öFragment
		trans.replace(R.id.fl_home_menu_left, new HomeMenuLeftFragment(), TAGH_HOME_MENU_LEFT);
		trans.replace(R.id.fl_home_content, new HomeContentFragment(),TAG_HOME_CONTENT);
		trans.commit();
		
	   //fragmentManager.findFragmentByTag(tag)
	}
	
	/**
	 * »ñÈ¡Ö÷ÄÚÈÝFragment
	 */
	public HomeContentFragment getHomeContentFragment()
	{
		FragmentManager fm=this.getSupportFragmentManager();
		return (HomeContentFragment) fm.findFragmentByTag(TAG_HOME_CONTENT);
	}
	/**
	 * »ñÈ¡²à±ßÀ¸Fragment
	 */
	public HomeMenuLeftFragment getHomeMenuLeftFragment()
	{
		FragmentManager fm=this.getSupportFragmentManager();
		return (HomeMenuLeftFragment) fm.findFragmentByTag(TAGH_HOME_MENU_LEFT);
	}
	
	//Êý×é³¤¶È±íÊ¾Òªµã»÷µÄ´ÎÊý
	long[] hits=new long[2];
	
	@Override
	public void onBackPressed()
	{
		System.arraycopy(hits, 1, hits, 0, hits.length-1);
		hits[hits.length-1]=SystemClock.uptimeMillis();//¿ª»úºó¿ªÊ¼¼ÆËãµÄÊ±¼ä
		if(hits[0]>=(SystemClock.uptimeMillis()-2000)){//2ÃëÁ¬»÷Ê±¼ä
			  android.os.Process.killProcess(android.os.Process.myPid());    //»ñÈ¡PID 
			  System.exit(0);   //³£¹æjava¡¢c#µÄ±ê×¼ÍË³ö·¨£¬·µ»ØÖµÎª0´ú±íÕý³£ÍË³ö
		}else{
			Toast.makeText(this, "Á¬ÐøË«»÷Á½´ÎÍË³öÓ¦ÓÃ", 0).show();
		}
	}
}
