package com.backfire.doudizhu.common;

import java.lang.ref.SoftReference;
import java.util.HashMap;

import android.content.Context;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

import com.backfire.arithmetic.Card;
import com.backfire.arithmetic.CardJudge.CardType;
import com.backfire.room.data.ConfigCenter;
import com.backfire.room.data.State.Sex;
import com.backfire.utils.Log;

public class SoundManager implements AudioConfig {
	private String TAG = SoundManager.class.getName();

	private byte[] sync = new byte[0];
	private boolean binit = false;
	private SoundPool soundPool = null;
	private HashMap<String, SoftReference<Integer>> soundMap = new HashMap<String, SoftReference<Integer>>();

	public void init() {
		synchronized (sync) {
			if (binit)
				return;
			binit = true;
			soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 200);
		}
		loadPlayCardSound();
	}

	public void loadPlayCardSound() {
		loadPlayCardSound(kWin);
		loadPlayCardSound(kLose);
		loadPlayCardSound(kSelectCard);
		loadPlayCardSound(kSelectMenu);
		loadPlayCardSound(kAnimBomb);
		loadPlayCardSound(kAnimChain);
		loadPlayCardSound(kAnimChainPair);
		loadPlayCardSound(kAnimPlane);
		loadPlayCardSound(kAnimRocket);
		
		String prePath = kGirlVoicePath;
		
		loadPlayCardSound(prePath + k3Dai0);
		loadPlayCardSound(prePath + k3Dai1);
		loadPlayCardSound(prePath + k3Dai2);
		loadPlayCardSound(prePath + kBomb);
		loadPlayCardSound(prePath + k4Dai2);
		loadPlayCardSound(prePath + k4Dai2Dui);
		loadPlayCardSound(prePath + kShunzi);
		loadPlayCardSound(prePath + kRocket);
		loadPlayCardSound(prePath + kShunzi);
		loadPlayCardSound(prePath + kLianDui);
		loadPlayCardSound(prePath + kFeiji);
		loadPlayCardSound(prePath + k3Dai0);
		loadPlayCardSound(prePath + k3Dai0);

		loadPlayCardSound(prePath + kDingtianShun);
		loadPlayCardSound(prePath + kCall);
		loadPlayCardSound(prePath + kNotCall);
		loadPlayCardSound(prePath + kGrab);
		loadPlayCardSound(prePath + kNotGrab);
		loadPlayCardSound(prePath + kControl);
		loadPlayCardSound(prePath + kPass);
		loadPlayCardSound(prePath + kPass1);
		// 加载男生音效
		//loadSound(Sex.MALE);
		// 加载女生音效
		//loadSound(Sex.FEMALE);
	}

	private void loadSound(int sex) {
//		String prePath = (sex == Sex.FEMALE ? kBoyVoicePath : kGirlVoicePath);
		String prePath = kGirlVoicePath;
		if (null != kSingleVoices) {
			loadSoundFile(prePath, kSingleVoices);
		}
		if (null != kPairVoices) {
			loadSoundFile(prePath, kPairVoices);
		}

		
	}

	private void loadSoundFile(String prePath, String[] files) {
		if (null == files)
			return;

		try {
			for (int i = 0; i < files.length; i++) {
				String key = prePath + files[i];
				if (null != key) {
					loadPlayCardSound(key);
				}
			}
		} catch (Exception e) {
			Log.e(this, e);
		}
	}

	// 播放菜单选择音效
	public void PlayMenuSelect() {

	}

	// 播放选牌音效
	public void playCardSelect() {

	}

	// 播放出牌音效
	public void playCardEffect(CardType cardType, int headPoint, int sex) {
		String key = null;

		switch (cardType) {
		// 单张
		case CardSingle:
			if (headPoint >= 3) {
				key = kSingleVoices[headPoint - 3];
			}
			break;

		// 对子
		case CardPair:
			if (headPoint >= 3) {
				key = kPairVoices[headPoint - 3];
			}
			break;

		// 三不带
		case CardThreeWithNo:
			key = k3Dai0;
			break;

		// 三带一
		case CardThreeWithOne:
			key = k3Dai1;
			break;

		// 三带二
		case CardThreeWithTwo:
			key = k3Dai2;
			break;

		// 炸弹
		case CardBombWith_4:
			key = kBomb;
			break;

		// 四带二
		case CardFourWithTwo:
			key = k4Dai2;
			break;

		// 四带两对
		case CardFourWithTwoPair:
			key = k4Dai2Dui;
			break;

		// 火箭
		case CardBombBoss:
		case CardBigBombBoss:
			key = kRocket;
			break;

		// 顺子
		case CardChain:
			// 顶天顺
			if (headPoint == Card.PointA)
				key = kDingtianShun;
			else
				key = kShunzi;
			break;

		// 连对
		case CardChainPair:
			key = kLianDui;
			break;

		// 飞机
		case CardAirplaneNo:
		case CardAirplaneOne:
		case CardAirplaneTwo:
			key = kFeiji;
			break;
		}

		if (null != key) {
			String pre = "";
			/*if (sex == Sex.MALE) {
				pre = kBoyVoicePath;
			} else if(sex==Sex.FEMALE){
				pre = kGirlVoicePath;
			}*/
			pre=kGirlVoicePath;
			key = pre + key;
			play(key);
		}
	}

	// 动画音效
	public void playAnimEffect(CardType cardType) {
		String key = null;

		switch (cardType) {
		case CardBombWith_4:
			key = kAnimBomb;
			break;
		case CardChain:
			key = kAnimChain;
			break;
		case CardChainPair:
			key = kAnimChainPair;
			break;
		case CardAirplaneNo:
		case CardAirplaneOne:
		case CardAirplaneTwo:
			key = kAnimPlane;
			break;
		case CardBombBoss:
		case CardBigBombBoss:
			key = kAnimRocket;
			break;
		}

		if (null != key) {
			play(key);
		}
	}

	// 播放牌局开始音效
	public void playGameStart(boolean maleVoice) {

	}

	// 播放胜利音效
	public void playGameWin() {

	}

	// 播放失败音效
	public void playGameLose() {

	}

	// 播放叫地主音效
	public void playCall(boolean maleVoice) {

	}

	// 播放不叫音效
	public void playNotCall(boolean maleVoice) {

	}

	// 播放抢地主音效
	public void playGrab(boolean maleVoice) {

	}

	// 播放不抢音效
	public void playNotGrab(boolean maleVoice) {

	}

	// 播放不出音效
	void playPass(boolean maleVoice) {

	}

	// 播放管上音效
	void playControl(boolean maleVoice) {

	}

	public void play(String key) {
		try {
			int id = 0;
			synchronized (sync) {
				if (soundMap == null) {
					clear();
					init();
				}
				SoftReference<Integer> ref = soundMap.get(key);
				if (null != ref) {
					id = ref.get();
					if (id==0) {
						soundMap.remove(key);
					}
				}
			}
			if (id==0) {
				loadPlayCardSound(key);
				//play(key);
				//return;
			}else{
				if (DdzApplication.getApplication().isDebug()) {
					Log.i(this, "Play-" + key + "-" + id);
				}
				if (soundPool != null && !ConfigCenter.volume_off) {
					AudioManager mAudioManager = (AudioManager) DdzApplication
							.getApplication().getSystemService(
									Context.AUDIO_SERVICE);
					float max = mAudioManager
							.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
					float current = mAudioManager
							.getStreamVolume(AudioManager.STREAM_MUSIC);
					int voice_music = ShareData.getInstance().getIntValue(
							"MUSIC_VOICE", (int) ((current / max) * 100f));
					soundPool.setVolume(soundPool.play(id, 1, 1, 1, 0, 1f),
							voice_music / 100f, voice_music / 100f);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void clear() {
		synchronized (sync) {
			if (binit) {
				binit = false;
				soundMap.clear();
				soundPool.release();
				soundPool = null;
			}
		}
	}
	public void loadPlayCardSound(String key) {
		try {
			AssetManager assetManager = DdzApplication.getApplication().getAssets();
			// 加载共用音效
			synchronized (sync) {
				if(soundMap!=null && soundPool!=null){
					SoftReference<Integer> ref = soundMap.get(key);
					if (null != ref) {
						int id = ref.get();
						if (id==0) {
							soundMap.remove(key);
							soundMap.put(key, new SoftReference<Integer>(soundPool.load(assetManager.openFd(key), 0)));
							return;
						}
					}else{
						soundMap.put(key, new SoftReference<Integer>(soundPool.load(assetManager.openFd(key), 0)));
						return;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
