package com.wasu.cs.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.cs.adapter.ContAdapter;
import com.wasu.cs.adapter.ContPagerAdapter;
import com.wasu.cs.model.HomeColumnModel;
import com.wasu.cs.module.ScreenSaverModule;
import com.wasu.cs.protocol.HomeColumnProtocol;
import com.wasu.cs.protocol.HomeColumnProtocol.HomeColumnFetchCallback;
import com.wasu.cs.protocol.HomeProtocol;
import com.wasu.cs.protocol.HomeProtocol.HomeFetchCallback;
import com.wasu.cs.widget.DialogAssetExitApp;
import com.wasu.cs.widget.FixedSpeedScroller;
import com.wasu.cs.widget.MainHPageItem21;
import com.wasu.cs.widget.MainHPageItemBase;
import com.wasu.cs.widget.MainHPageView;
import com.wasu.cs.widget.MainTabBar;
import com.wasu.cs.widget.MainTabBar.OnItemClickListener;
import com.wasu.cs.widget.MainTabBar.OnItemSelectedListener;
import com.wasu.cs.widget.MainTabItem;
import com.wasu.module.log.WLog;
import com.wasu.statistics.WasuStatistics;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cn.com.wasu.main.AppUtil;
import cn.com.wasu.main.Common;
import cn.com.wasu.main.ErrorMap;
import cn.com.wasu.main.IntentConstant;
import cn.com.wasu.main.LayoutCodeMap;
import cn.com.wasu.main.R;

/**
 * 
 * @ClassName: ActivityMain
 * @Description: Wasu TV4.0首页
 * @author 海迪
 * @date 2015年6月16日 下午12:25:33
 * 
 */
public class ActivityMain extends ActivityBase {

	private String TAG = "ActivityMain";

	private Context context;
	private ActivityMain activityMain;
	private static final String PageViewType = "front";

	private MainTabBar mainTabBar;
	private ViewPager viewPager;
	/**vip屏**/
	private MainVipPageView mainVipPageView;
	/**推荐屏**/
	private MainRecomPageView mainRecomPageView;
	/**点播屏**/
	private MainDemandPageView mainDemandPageView;
	/**生活屏**/
	private MainLifePageView mainLifePageView;
	private ContPagerAdapter pagerAdapter;
	private int tabItemCount = 0;
	private boolean isInitFocusItem = false;
	private List<MainHPageItemBase> statisticalList = new ArrayList<MainHPageItemBase>();
	private int loadDataCount = 0;

	@Override
	protected void onResume() {
		super.onResume();

		WLog.i(TAG,"onCreate()");
		//pageViewStart
		WasuStatistics.getInstance().pageViewStart(PageViewType);

		if(mainVipPageView!=null){
			mainVipPageView.bindUserData();
		}
		if(mainRecomPageView!=null){
			mainRecomPageView.bindUserData();
			/**播放器没有被遮住就恢复播放**/
			if(!mainRecomPageView.liveVideoIsCover()){
				mainRecomPageView.liveVideoResume();
			}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		//pageViewEnd
		String page;
		switch (CURRENT_PAGE){
			case 0:
				page = "VIP";
				break;
			case 1:
				page = "推荐";
				break;
			case 2:
				page = "点播";
				break;
			case 3:
				page = "生活";
				break;
			default:
				page = AppUtil.MAIN_PAGERVIEW_POSITION+"";
				break;
		}
		WasuStatistics.getInstance().pageViewEnd(PageViewType, "首页", page);

		if(mainRecomPageView != null){
			mainRecomPageView.liveVideoStop();//为了降低内存占用，翻页时把暂停视频播放改为停止视频播放
		}
	}

	/**
	 * pageview切換速度
	 */
	private int pageSwitchDuration = 300;

	/**
	 * 顶部标签区数据集合
	 */
	private List<HomeColumnModel> topDataList = new ArrayList<HomeColumnModel>();

	/**
	 * 底部功能入口区数据集合
	 */
	private List<HomeColumnModel> bottomDataList = new ArrayList<HomeColumnModel>();

	ArrayList<HomeColumnModel> recList = new ArrayList<HomeColumnModel>();

	ArrayList<HomeColumnModel> vodList = new ArrayList<HomeColumnModel>();

	ArrayList<HomeColumnModel> smartRecList = new ArrayList<HomeColumnModel>();

	public ArrayList<HomeColumnModel> getSmartRecList() {
		return smartRecList;
	}

	public void setSmartRecList(ArrayList<HomeColumnModel> smartRecList) {
		this.smartRecList = smartRecList;
	}

	public ArrayList<HomeColumnModel> getRecList() {
		return recList;
	}

	public void setRecList(ArrayList<HomeColumnModel> recList) {
		this.recList = recList;
	}

	public ArrayList<HomeColumnModel> getVodList() {
		return vodList;
	}

	public void setVodList(ArrayList<HomeColumnModel> vodList) {
		this.vodList = vodList;
	}

	public long startTime;
	/**判断是否为大数据推荐**/
	private boolean isHasSmartRec = false;

	private int CURRENT_PAGE = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
//			finish();
//			return;
//		}
		startTime = System.currentTimeMillis();
	}

	@Override
	protected void doCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_main);
		context = this;
		activityMain = this;
		reqLabelAreaData();
		// Intent intent = new Intent(this, ActivityFilmSpecial.class);
		/*
		 * Intent intent = new Intent(this, ActivityShortVideoSpecial.class);
		 * startActivity(intent);
		 */

		/**
		 * 登陆统计
		 * */
		WasuStatistics.getInstance().login();
	}

	/**
	 *
	 * @author 海迪
	 * @Title: reqLabelAreaData
	 * @Description: 向服务器获取首页数据，得到的数据分类保存到topDataList和bottomDataList，
	 *               并初始化标签区和功能入口区
	 * @param
	 * @return void
	 * @throws
	 *
	 */
	private void reqLabelAreaData() {
		loadDataCount++;

		// 预加载数据
		if (getIntent() != null && getIntent().hasExtra("preload")) {
			WLog.d("danxx", "have preload data");
			HomeProtocol homeProtocol = new HomeProtocol();
			try {
				homeProtocol.from(new JSONObject(getIntent().getStringExtra("preload")));

				loadDataCount = 0;
				List<HomeColumnModel> cList = homeProtocol.getColumnList();

				for (HomeColumnModel c : cList) {

					if ("top".equals(c.getPosition())) {
						topDataList.add(c);

					} else if ("bottom".equals(c.getPosition())) {
						bottomDataList.add(c);
					}
				}
				String bgPic = homeProtocol.getBgImage();

				setBg(bgPic);
//				initBottomBar();
				initTabBar();

				return;

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		showLoading();
		/**
		 * 首页数据请求
		 */
		HomeProtocol.fetch(Common.HOME_URL, new HomeFetchCallback() {

			@Override
			public void onResult(boolean successed, HomeProtocol homeProtocol) {
				hideLoading();
				if (successed) {
					loadDataCount = 0;
					List<HomeColumnModel> cList = homeProtocol.getColumnList();
					for (HomeColumnModel c : cList) {

						if ("top".equals(c.getPosition())) {
							topDataList.add(c);
						} else if ("bottom".equals(c.getPosition())) {
							bottomDataList.add(c);
						}
					}

					String bgPic = homeProtocol.getBgImage();

					setBg(bgPic);
//					initBottomBar();
					initTabBar();
				} else {
					if (loadDataCount < 3) {
						reqLabelAreaData();
						return;
					}

					if (homeProtocol != null) {
						showErrorExitDlg(ErrorMap.mapError(context, homeProtocol.getCode(), homeProtocol.getMessage()));
					} else {
						showErrorExitDlg(getResources().getString(R.string.error_unknown));
					}

				}
			}
		});
	}

	/**
	 *
	 * @author 海迪
	 * @Title: initBottomBar
	 * @Description: 初始化首页底部功能入口区
	 * @param
	 * @return void
	 * @throws
	 *
	 */
//	private void initBottomBar() {
//		bottomBar = (BottomBar) findViewById(R.id.function_area_bottombar);
//		bottomBar.initRightBar(bottomDataList);
//		if (bottomBar.userCenterView != null) {
//			userCenterView = bottomBar.userCenterView;
//		}
//
//	}

	List<HomeColumnModel> topDataListTemp;
	boolean ismIsChanged = false;

	/**
	 *
	 * @author 海迪
	 * @Title: initTabBar
	 * @Description: 初始化首页顶部标签区
	 * @param
	 * @return void
	 * @throws
	 *
	 */
	private void initTabBar() {

		mainTabBar = (MainTabBar) findViewById(R.id.label_area_tabbar);
		ContAdapter tabbarAdapter = new ContAdapter(topDataList);
		tabbarAdapter.setViewInterface(new ContAdapter.ViewInterface() {

			@Override
			public View getView(int position, Object dataObject, View convertView, ViewGroup parent) {
				HomeColumnModel scm = (HomeColumnModel) dataObject;

				MainTabItem item = null;
				if (convertView == null) {
					item = new MainTabItem(context);
				} else {
					item = (MainTabItem) convertView;
				}

				item.initData(scm);

				return item;
			}
		});

		mainTabBar.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(View view, int position) {
				viewPager.setCurrentItem(position);
			}
		});

		mainTabBar.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(int position, View view) {
				viewPager.setCurrentItem(position);
				if (position >= 0) {
					/**当点击titlebar的时候viewpager的内容去请求焦点**/
					Object child = pagerAdapter.getObject(position);
					if(child != null && child instanceof MainVipPageView){
						MainVipPageView vipPageView = (MainVipPageView) child;
						vipPageView.firstRequestFocus(View.FOCUS_DOWN);
					}
					else if(child != null && child instanceof MainRecomPageView){
						MainRecomPageView recomPageView = (MainRecomPageView) child;
						recomPageView.firstRequestFocus(View.FOCUS_DOWN);
					}
					else if(child != null && child instanceof MainDemandPageView){
						MainDemandPageView demandPageView = (MainDemandPageView) child;
						demandPageView.firstRequestFocus(View.FOCUS_DOWN);
					}
					else if(child != null && child instanceof MainLifePageView){
						MainLifePageView lifePageView = (MainLifePageView) child;
						lifePageView.firstRequestFocus(View.FOCUS_DOWN);
					}
					else{
						WLog.d("danxx" ,"not instanceof MainHPageView and MainVipPageView");
					}
				}

			}
		});

		mainTabBar.setAdapter(tabbarAdapter);

		viewPager = (ViewPager) findViewById(R.id.content_area_pager);
		viewPager.setOffscreenPageLimit(4);
		// viewPager.setPageTransformer(true, new DepthPageTransformer());
		topDataListTemp = new ArrayList<HomeColumnModel>();

		tabItemCount = topDataList.size();
		if (tabItemCount > 1) {
			topDataListTemp.addAll(topDataList);
		} else {
			topDataListTemp.addAll(topDataList);
		}
		initViewPageAdapter();
		/**
		 * viewpager翻页监听器
		 */
		viewPager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				HomeColumnModel pagemodel = null;
				if (pagerAdapter==null) {
					return;
				}
				AppUtil.MAIN_PAGERVIEW_POSITION = position;
				/**页面切换的时候对播放器做播放和恢复操作**/
				if(mainRecomPageView != null ){
//					if(1 == position){
//						mainRecomPageView.liveVideoResume();
//						if(mainRecomPageView != null)
//							mainRecomPageView.showImageView();
//						if(mainDemandPageView != null)
//							mainDemandPageView.recycleImageView();
//						if(mainVipPageView!=null)
//							mainVipPageView.recycleImageView();
//					}
//					if(2 == position){
//						if(mainDemandPageView != null)
//							mainDemandPageView.showImageView();
//						if(mainRecomPageView != null)
//							mainRecomPageView.recycleImageView();
//					}
//					if(0 == position){
//						if(mainVipPageView != null)
//							mainVipPageView.showImageView();
//						if(mainRecomPageView != null)
//							mainRecomPageView.recycleImageView();
//					}
					if(1 == position){
						// 恢复视频播放
						if(mainRecomPageView!=null)
							mainRecomPageView.liveVideoResume();
					}
					if(0 == position || 2  == position){
//						mainRecomPageView.liveVideoPause();
						mainRecomPageView.liveVideoStop();
					}
				}
				if (pagerAdapter.getCount() > 1) {
					if (position == 3) {
						if (!isHasSmartRec) {
							//如果切换到第三屏就更新第二屏的大数据推荐
//							notifyData(modelRec);
						}
					}
					mainTabBar.setItemSelected(position);
				} else if (pagerAdapter.getCount() > 0) {
					mainTabBar.setItemSelected(position);
				}
				pagemodel = (HomeColumnModel) pagerAdapter.getItem(position);
				if(pagemodel!=null){
					sendShowStatistical(pagemodel);
				}
				CURRENT_PAGE = position;
			}

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

			@Override
			public void onPageScrollStateChanged(int arg0) {

				if (arg0 == 0) {// 如果翻页滚动完毕
					// 重新设置一下播放器状态
				}
			}

		});

		String layoutCode = getIntent().getStringExtra(IntentConstant.LAYOUT_CODE.value());
		boolean isHomeVip = !TextUtils.isEmpty(layoutCode) && layoutCode.equals(LayoutCodeMap.Home_Vip);
		viewPager.setAdapter(pagerAdapter);
		AppUtil.MAIN_PAGERVIEW_POSITION = isHomeVip ? 0 : 1;
		viewPager.setCurrentItem(AppUtil.MAIN_PAGERVIEW_POSITION);
		mainTabBar.setItemSelected(AppUtil.MAIN_PAGERVIEW_POSITION);
	}

	/**
	 * 更新大数据推荐
	 * @param modelRec
     */
	private void notifyData(HomeColumnModel modelRec) {
		final HomeColumnModel scModel = modelRec;
		String jsonUrl = modelRec.getJsonUrl();
		jsonUrl = jsonUrl + "&appId=" + Common.appKey + "&userId=" + AuthSDK.getInstance().getValue(IAuthInterface.KEY_TVID) + "&bizCode=apkdata&scenarioCode=FrontPG";
		HomeColumnFetchCallback homeColumFetchCallback = new HomeColumnFetchCallback() {

			@Override
			public void onResult(boolean successed, HomeColumnProtocol homeColumnProtocol) {

				if (successed) {
					isHasSmartRec = true;
					homeColumnProtocol.initChildrenTimeAndCorner(scModel.getCarouselTime(), scModel.getCornerIconMap());
					smartRecList.clear();
					smartRecList = (ArrayList<HomeColumnModel>) homeColumnProtocol.getColumnList();
					getForMatList();
					handler.postDelayed(runnable, 200);

				}

			}
		};

		HomeColumnProtocol.fetch(jsonUrl, homeColumFetchCallback);
	}

	public void getForMatList() {

		ArrayList<HomeColumnModel> testRecList = new ArrayList<HomeColumnModel>();
		for (int i = 0; i < smartRecList.size(); i++) {
			if (smartRecList.get(i).getAliRecommend() == 1) {// 是大数据推荐的数据
				if (smartRecList.get(i).getColspan() == 1 && smartRecList.get(i).getRowspan() == 2) {
					testRecList.add(smartRecList.get(i));
				}

			}
		}
		smartRecList.clear();
		smartRecList.addAll(testRecList);
	}

	Handler handler = new Handler();

	Runnable runnable = new Runnable() {
		@Override
		public void run() {// 刷新ui

			if (pagerAdapter==null) {
				return;
			}
			if (pagerAdapter.getCount()<=1) {
				return;
			}

			Object child = pagerAdapter.getObject(1);
			if (child != null && child instanceof MainHPageView) {
				MainHPageView pageView = (MainHPageView) child;
				ArrayList<MainHPageItem21> testViewList = new ArrayList<MainHPageItem21>();
				// 获取第一屏的大数据推荐坑位的item
				if (pageView.mAdapter==null) {
					return;
				}
				for (int i = 0; i < pageView.mAdapter.getCount(); i++) {

					MainHPageItemBase Childchild = (MainHPageItemBase) pageView.mAdapter.getObject(i);
					if (Childchild == null) {
						continue;
					}

					if (Childchild.isAliRecItem()) {

						MainHPageItem21 childItem = (MainHPageItem21) Childchild;

						testViewList.add(childItem);
					}
				}

				for (int j = 0; j < smartRecList.size(); j++) {
					if (j < testViewList.size()) {
						// 刷新推荐数据
						testViewList.get(j).notifyData(smartRecList.get(j));

					}
				}
			}
		}
	};
	HomeColumnModel modelRec, model;

	private void initViewPageAdapter() {
		pagerAdapter = new ContPagerAdapter(topDataListTemp);
		pagerAdapter.setViewInterface(new ContPagerAdapter.ViewInterface() {

			@Override
			public View getView(int position, Object dataObject, View convertView, ViewGroup parent) {

				model = (HomeColumnModel) dataObject;
				if(convertView != null){
					return convertView;
				}
				if(model != null && model.getLayout()!=null && model.getLayout().equalsIgnoreCase(AppUtil.Home_Vip_Layout)){
					convertView = new MainVipPageView(ActivityMain.this, model.getCsJsonUrl(), viewPager);
					mainVipPageView = (MainVipPageView) convertView;
					return convertView;
				}
				if (!TextUtils.isEmpty(model.getLayout())) {
					if (model.getLayout().equals(AppUtil.Home_Rec_Layout)) {
//						modelRec = model;
						mainRecomPageView = new MainRecomPageView(ActivityMain.this, model.getJsonUrl(), viewPager);
						/**页面第一次启动时请求焦点**/
						mainRecomPageView.firstRequestFocus(View.FOCUS_DOWN);
						convertView = mainRecomPageView;
						return convertView;
					}
				}
				if (!TextUtils.isEmpty(model.getLayout())) {
					if (model.getLayout().equals(AppUtil.Home_Demand_Layout)) {
//						modelRec = model;
						mainDemandPageView = new MainDemandPageView(ActivityMain.this, model.getJsonUrl(), viewPager);
						convertView =  mainDemandPageView;
						return convertView;
					}
				}
				if (!TextUtils.isEmpty(model.getLayout())){
					if (model.getLayout().equals(AppUtil.Home_Life_Layout)) {
//						modelRec = model;
						mainLifePageView = new MainLifePageView(ActivityMain.this, model.getJsonUrl(), viewPager);
						convertView =  mainLifePageView;
						return convertView;
					}
				}
				View tempView = new View(ActivityMain.this);
				return tempView;

			}
		});
	}

	/**
	 * 推荐屏幕统计
	 * @param pageItem
     */
	private void sendShowStatistical(HomeColumnModel pageItem) {

		// 推荐屏的曝光统计
		List<HomeColumnModel> list;
		if (pageItem.getLayout()!=null && pageItem.getLayout().equals("Home_Rec")) {
			// 统计展示
			list = recList;
		} else {
			list = null;
		}

		if (list != null && list.size() > 0) {
			int len = list.size();
			String[] items = new String[len];
			String[] items_traceid = new String[len];

			for (int index = 0; index < len; index++) {
				HomeColumnModel hcm = list.get(index);

				if (hcm == null)
					continue;

				items[index] = hcm.getEid() + "";
				items_traceid[index] = hcm.getTraceid();
				if (TextUtils.isEmpty(items_traceid[index])) {
					items_traceid[index] = "";
//					WasuStatistics.getInstance().view(items[index]);
					WasuStatistics.getInstance().BigDataRecommendListView(items[index], "");
				}
			}
			// 需要BigDataRecommendListView
//			WasuStatistics.getInstance().recommendListView("", "", items, items_traceid);
		}

	}
	public void firstRequestFocus(){
		MainTabItem mainTabItem = mainTabBar.getFocusItem();
		if (mainTabItem!=null){
			mainTabItem.performClick();
		}
	}
	private FixedSpeedScroller scroller = null;

	public void setScrollerTime(int scrollerTime) {
		try {
			if (scroller != null) {
				scroller.setTime(scrollerTime);
			} else {
				Field mScroller;
				mScroller = ViewPager.class.getDeclaredField("mScroller");
				mScroller.setAccessible(true);
				scroller = new FixedSpeedScroller(viewPager.getContext(), new LinearInterpolator());
				scroller.setTime(scrollerTime);
				mScroller.set(viewPager, scroller);
			}
		} catch (Exception e) {
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN && (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT)) {
			setScrollerTime(pageSwitchDuration);
			if (pagerAdapter == null) {
				return super.dispatchKeyEvent(event);
			}
		} else {
			setScrollerTime(pageSwitchDuration);
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() != KeyEvent.ACTION_UP){
			if(mainRecomPageView != null && mainRecomPageView.liveVideoIsFullScreen()){
				/**当播放器是全屏的时候就不拦截返回键，不然播放器无法缩小成小屏**/
				return super.onKeyDown(keyCode, event);
			}
			if(mainTabBar!=null && mainTabBar.hasFocus()){
				exitDialogShow();
				return true;
			}
			if(mainTabBar == null){
				return true;
			}
			/**当在首页按返回键时，先把当前页面滚到顶部，然后首页的title再请求焦点，当
			 * 首页已经获得焦点按返回键的时候就显示退出窗口**/
			if(1 == CURRENT_PAGE){
				if(mainRecomPageView == null)
					return true;
				if(0 == mainRecomPageView.getScrollY()){
					mainTabBar.requestFocus();
					return true;
				}else if(mainRecomPageView.getScrollY() > 0) {
					mainRecomPageView.scrollToTop();
					mainTabBar.requestFocus();
					return true;
				}else{
					return true;
				}
			}else if(2 == CURRENT_PAGE){
				if(mainDemandPageView == null)
					return true;
				if(0 == mainDemandPageView.getScrollY()){
					mainTabBar.requestFocus();
					return true;
				}else if(mainDemandPageView.getScrollY() > 0) {
					mainDemandPageView.scrollToTop();
					mainTabBar.requestFocus();
					return true;
				}else{
					return true;
				}
			}else if(0 == CURRENT_PAGE){
				if(mainVipPageView == null)
					return true;
				if(mainTabBar != null){
					mainVipPageView.scrollToTop();
					mainTabBar.requestFocus();
					return true;
				}
			}else if(3 == CURRENT_PAGE){
				if(mainTabBar != null) {
					mainTabBar.requestFocus();
					return true;
				}else{
					return true;
				}
			}
			return true;
		}

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (CURRENT_PAGE == 2) {
			}
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void playSound(int keyCode, View view) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			view.playSoundEffect(SoundEffectConstants.getContantForFocusDirection(View.FOCUS_LEFT));
			break;
		case KeyEvent.KEYCODE_DPAD_UP:
			view.playSoundEffect(SoundEffectConstants.getContantForFocusDirection(View.FOCUS_UP));
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			view.playSoundEffect(SoundEffectConstants.getContantForFocusDirection(View.FOCUS_RIGHT));
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			view.playSoundEffect(SoundEffectConstants.getContantForFocusDirection(View.FOCUS_DOWN));
			break;
		}
	}
	DialogAssetExitApp exitDialog;
	private void exitDialogShow() {
		if (context==null) {
			return;
		}
		if(exitDialog != null && exitDialog.isShowing()){
			return;
		}
		exitDialog = new DialogAssetExitApp(context);
		exitDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				exitDialog.DestoryDialog();
				exitDialog.dismiss();
				exitDialog = null;
			}
		});
		exitDialog.setStartTime(startTime);
		exitDialog.show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ScreenSaverModule.getInstance().clearTask();
		System.exit(0);
	}

	/**
	 * 
	 * @author 海迪
	 * @Title: releaseResources
	 * @Description: 退出应用时，释放资源
	 * @param
	 * @return void
	 * @throws
	 * 
	 */
	public void releaseResources() {

		if (mainTabBar != null) {
			mainTabBar.releaseResources();
			mainTabBar.removeAllViews();
			mainTabBar = null;
		}

		if (viewPager != null) {
			viewPager.removeAllViews();
			viewPager = null;
		}

		if (pagerAdapter != null) {
			pagerAdapter.releaseResources();
			pagerAdapter = null;
		}

		if (topDataList != null) {
			topDataList.clear();
			topDataList = null;
		}

		if (bottomDataList != null) {
			bottomDataList.clear();
			bottomDataList = null;
		}

		context = null;
	}

	@Override
	public void finish() {
		releaseResources();
		super.finish();
	}

	public List<MainHPageItemBase> getStatisticalList() {
		return statisticalList;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
	}

}
