package com.backfire.doudizhu.common;

import java.util.ArrayList;

import net.yeeyo.doudizhu.feiyu.R;
import android.app.Activity;
import android.app.Application;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.backfire.arithmetic.CardJudge.CardType;
import com.backfire.doudizhu.bean.PayTip;
import com.backfire.room.data.State.Sex;
import com.backfire.utils.Debug;
import com.backfire.utils.Log;
import com.backfire.utils.UEHandler;

/**
 * @author wf
 *
 */
public class DdzApplication extends Application {
	public static final int PAY_TIP_CHARGE_FAIL = 1; // 充值不成功
	public static final int PAY_TIP_CHARGE_WAIT_DELIVERY = 2; // 订单已创建，等待回调
	public static final int PAY_TIP_DELIVERY = 3; // 发货提醒

	public static final String payFailTip1 = "网络不给力，请稍候再试";// 充值不成功，请重试！
	public static final String payFailTip2 = "网络不给力，扣费成功后金币将到帐。若有问题请拨打客服电话!";// 订单已创建，发货有时会有延时，请稍后重新登录更新您的信息!
	public static final String payFailTip3 = "您共充值了XX笔，总XX金币XX彩券已到帐。";//
	
	private static DdzApplication app = null;
	
	private BitmapManager bitmapManager = null;
	private boolean isDebug = true;
	private SoundManager soundManager = null;
	private LobbySoundManager lobbySoundManager = null;
	// Network
	public int NetworkStatus;
	
	// Screen Parameters
	public int screenWidth;
	public int screenHeight;
	
	private boolean isMMInited = false;
	private Toast toastTop = null;
	private PlayMedia playMedia = null;
	//private PlayMedia commonMedia=null;
	
	private ArrayList<PayTip> payTips = null;
	
	public static DdzApplication getApplication() {
		return app;
	}
	
	// Methods
	public void setupScreenSize(int width, int height) {
		screenWidth = width;
		screenHeight = height;
	}
	
	public void onCreate() {
		app = this;
		bitmapManager = null;
		UEHandler.getInstance().init(app);
		Debug.threadInit();
		Log.init(app.getFilesDir().getAbsolutePath());
		super.onCreate();
	}
	
	public void onTerminate() {
		bitmapManager.release();
		super.onTerminate();
	}
	public void clearBmpPool() {
		bitmapManager.clear();
	}
	public BitmapManager getBmpPool() {
		if (null == bitmapManager) {
			bitmapManager = new BitmapManager();
		}
		
		return bitmapManager;
	}
	public void initLobbySound() {
		deInitLobbySound();
		if (null == lobbySoundManager) {
			lobbySoundManager = new LobbySoundManager();
			lobbySoundManager.init();
		}
	}

	public void deInitLobbySound() {

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

	}
	public void playLobby(String key) {
		if (null == lobbySoundManager) {
			lobbySoundManager = new LobbySoundManager();
			lobbySoundManager.init();
		}
		lobbySoundManager.play(key);
	}
	public void initSound() {
		deInitSound();
		if (null == soundManager) {
			soundManager = new SoundManager();
			soundManager.init();//加载声音资源
		}
	}

	public void deInitSound() {

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

	}

	public void play(String key) {
		play(key, -1);
	}
	
	public void play(String key,int sex){
		if (null == key || 0 == key.length())
			return;
		if (null == soundManager) {
			soundManager = new SoundManager();
			soundManager.init();
		}
		
		String pre="";
		if(sex==Sex.MALE){
			pre=AudioConfig.kGirlVoicePath;
		}else if(sex==Sex.FEMALE){
			pre=AudioConfig.kGirlVoicePath;
		}
		key=pre+key;
		soundManager.play(key);
	}
	
	public void playCardEffect(CardType cardType,int headPoint, int sex){
		if (CardType.CardEnd==cardType)
			return;
		if (null == soundManager) {
			soundManager = new SoundManager();
			soundManager.init();
		}
		soundManager.playCardEffect(cardType, headPoint, sex);
	}
	
	public void playAnimEffect(CardType cardType){
		if (CardType.CardEnd==cardType)
			return;
		if (null == soundManager) {
			soundManager = new SoundManager();
			soundManager.init();
		}
		soundManager.playAnimEffect(cardType);
	}
	
	public boolean isDebug() {
		return isDebug;
	}
	
	public int getSoundVolume(){
		// TODO 语音时抑制音效
		AudioManager am = (AudioManager)this.getSystemService(AUDIO_SERVICE);
		return am.getStreamVolume(AudioManager.STREAM_MUSIC);
	}
	public String getStringData(int id){
		return app.getApplicationContext().getString(id);
	}
	public PlayMedia getPlayMedia(){
		if(playMedia==null){
			playMedia = new PlayMedia(this);
		}
		return playMedia;
	}
	public void clearPlayMedia(){
		if(playMedia!=null)playMedia=null;
	}
	
	public void showToast(String msg){
		showToast(msg, 1);
	}
	
	public void showToast(String msg,int duration) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.banner, null);
		TextView text = (TextView) view.findViewById(R.id.banner_title);
		text.setText(Html.fromHtml(msg));
		Toast toast = new Toast(this);
		toast.setView(view);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setDuration(duration);
		toast.show();

	}
	public void showTopToast(Activity activity,String string) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.banner, null);
		TextView text = (TextView) view.findViewById(R.id.banner_title);
		text.setText(Html.fromHtml(string));
		Toast toast = new Toast(this);
		toast.setView(view);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP, 0, DeviceUtil.AdaptScreenHeight(activity, 60));
		toast.show();

	}

	// 用toast显示 加入了动画效果
	public void showMoneyToast(String tips) {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		View view = layoutInflater.inflate(R.layout.burst_toast, null);
		ImageView burst = (ImageView) view.findViewById(R.id.busrt);
		TextView tx = (TextView) view.findViewById(R.id.money);
		tx.setText(Html.fromHtml(tips));
		tx.getPaint().setFakeBoldText(true);
		
		burst.setImageDrawable(
				new BitmapDrawable(DdzApplication.getApplication().getBmpPool().getBitmap(R.drawable.burst, 0, 0)));
		((ImageView)view.findViewById(R.id.gold)).setImageDrawable(
				new BitmapDrawable(DdzApplication.getApplication().getBmpPool().getBitmap(R.drawable.gold, 0, 0)));

//		ScaleAnimation scaleAnimation = new ScaleAnimation(0.5f, 1.0f, 0.5f,
//				1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
//				Animation.RELATIVE_TO_SELF, 0.5f);
//		scaleAnimation.setDuration(2000);
//		scaleAnimation.setInterpolator(new AccelerateInterpolator());
//		tx.startAnimation(scaleAnimation);

		Animation animation = AnimationUtils.loadAnimation(this, R.anim.burst);
		burst.startAnimation(animation);
		Toast toast = new Toast(this);
		toast.setView(view);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
		toast.show();
		playLobby("success_music");
	}
	
	public void setPayTips(ArrayList<PayTip> payTipList) {
		this.payTips = payTipList;
	}

	public String getPayTip(int id) {
		String result = null;
		if (null != payTips) {
			for (int i = 0; i < payTips.size(); i++) {
				PayTip payTip = payTips.get(i);
				if (null == payTip || payTip.getId() != id)
					continue;
				result = payTip.getContent();
			}
		}
		if (null == result) {
			switch (id) {
			case PAY_TIP_CHARGE_FAIL:
				result = payFailTip1;
				break;
			case PAY_TIP_CHARGE_WAIT_DELIVERY:
				result = payFailTip2;
			case PAY_TIP_DELIVERY:
				result = payFailTip3;
			default:
				break;
			}
		}
		return result;
	}
}
