package com.backfire.doudizhu;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.doudizhu.common.LoadingProgressDialog;
import com.backfire.doudizhu.common.ShareData;
import com.backfire.php.PHPRequestPool;
import com.backfire.room.data.ConfigCenter;
import com.backfire.utils.Log;
import com.umeng.analytics.MobclickAgent;

public abstract class ActivityBase extends Activity {

	protected int buyMethod = 0;

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

		// we just use music / media volume, not ring, not system
		this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		// Dirty code.
		int width = Math.max(dm.widthPixels, dm.heightPixels);
		int height = Math.min(dm.widthPixels, dm.heightPixels);
		DdzApplication.getApplication().setupScreenSize(width, height);
		ActivityContainer.getInstance().setCurrentActivity(this);
		Log.v(this, "onCreate and set to Current Activity", this.hashCode());
//		MobclickAgent.onError(this);
		ConfigCenter.volume_off = ShareData.getInstance().getBooleanValue(
				"VOLUME_OFF", false);
		PHPRequestPool.getInstance().resetPool();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		Log.v(this, "onStart", this.hashCode());
		super.onStart();
		System.gc();
	}

	@Override
	protected void onRestart() {
		Log.v(this, "onRestart", this.hashCode());
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.v(this, "onResume", this.hashCode());
		super.onResume();
		MobclickAgent.onResume(this);
	}

	// @Override
	// protected void onFreeze(Bundle outIcicle){
	// super.onFreeze(outIcicle);
	// }

	@Override
	protected void onPause() {
		Log.v(this, "onPause", this.hashCode());
		super.onPause();
		MobclickAgent.onPause(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.v(this, "onStop", this.hashCode());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.v(this, "onDestroy", this.hashCode());
		System.gc();
	}

	public void playBtnPressMusic() {
		DdzApplication.getApplication().playLobby("button_press");
	}

	public void showInfo(String msg) {
		DdzApplication.getApplication().showMoneyToast(msg);
	}

	protected void startLoading(String msg) {
		LoadingProgressDialog.show(this, msg);
	}

	protected void stopLoading() {
		LoadingProgressDialog.dissmissDlg();
	}

	protected int getTime(int times) {
		switch (times) {
		case 0:
			return 5000;
		case 1:
			return 3000;
		case 2:
			return 5000;
		case 3:
			return 7000;
		case 4:
			return 15000;
		case 5:
			return 30000;
		case 6:
			return 60000;
		case 7:
			return 120000;
		default:
			return (int) (Math.pow(2, times - 4) * 15000);
		}
	}

}
