package com.wasu.cs.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.wasu.comp.ad.AdView;
import com.wasu.comp.ad.AdViewListener;
import com.wasu.comp.ad.WasuAdEngine;
import com.wasu.cs.module.ScreenSaverModule;
import com.wasu.module.log.WLog;
import com.wasu.vast.model.AdExtension;

import cn.com.wasu.main.R;
/**
 * 屏保Activity
 * @author Danxx
 *
 */
public class ActivityScreenSaver extends Activity{
	private final String TAG = "ActivityScreenSaver";

	private FrameLayout adLayout;
	private TextView text_hint;
	private AdView adView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		WLog.i(TAG,"onCreate()");

		setContentView(R.layout.activity_screen_saver);
		ScreenSaverModule.getInstance().inScreenSaver();
		adLayout = (FrameLayout) findViewById(R.id.adLayout);
		text_hint = (TextView) findViewById(R.id.text_hint);
		if(adView == null){
			WasuAdEngine.getInstance().removeDirectP();
			adView =WasuAdEngine.getInstance().getImageAd(82, 0,true,null, new AdViewListener() {
				
				@Override
				public void AdUserClose() {
					// TODO Auto-generated method stub

				}

				@Override
				public void AdPause() {

				}

				@Override
				public void AdResume() {

				}

				@Override
				public void AdStopped() {
					// TODO Auto-generated method stub
					/*有下发广告的时候直接退出*/
					finish();
				}
				
				@Override
				public void AdSkipped() {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void AdLoaded(AdView.Property property) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void AdStarted(AdExtension adExtension) {
					text_hint.setVisibility(View.VISIBLE);
				}

				@SuppressLint("NewApi")
				@Override
				public void AdError() {
					// TODO Auto-generated method stub
//					adLayout.setBackgroundResource(R.drawable.default_boot);
					/**当获取屏保出错后直接退出**/
//					outScreenSaver();
					finish();
				}

				@Override
				public void AdClickThru(AdExtension adExtension) {

				}
			}); 
		}
    	adLayout.addView(adView);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		outScreenSaver();
		finish();
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
//		outScreenSaver();
		return super.dispatchKeyEvent(event);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
//		outScreenSaver();
	}
	
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		if (adView != null) {
			adView.destroyAd();
		}
		if(adLayout != null && adLayout.getChildCount()>0){
			adLayout.removeAllViews();
		}
		ScreenSaverModule.getInstance().outScreenSaver();
		ScreenSaverModule.getInstance().updateUserActionTime();
		adView = null;
		adLayout = null;
		
	}
	
	
	
}
