package com.ygtx.emcs.comm;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;
import android.util.SparseIntArray;

import com.ygtx.emcs.R;

import java.lang.ref.WeakReference;


/**
 * Audio player, pls set your application's targetSdkVersion to 21 or higher
 *
 * @author 79442
 * @date 2018/2/2
 */
@SuppressLint("NewApi")
public class SoundPoolMgr {

    /**
     * A weak reference with a Context, to avoid leak memory
     */
    private static WeakReference<Context> sWeakReference;
    private SoundPool mSoundPool;
    /**
     * Create a SparseIntArray object and manage various audios using SparseIntArray
     */
    private SparseIntArray mSoundArray = new SparseIntArray();

    /**
     * Inner class, Singleton mode
     */
    public static SoundPoolMgr getInstance(Context context) {
        try {
            if (sWeakReference == null) {
                AudioManager audioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
                if (audioManager == null) {
                    Log.e("Huang,SoundPoolMgr", "initialize SoundPoolMgr fail with get AudioManager returns null");
                } else {
                    /*
                     * Set media volume to maximum volume
                     */
                    int maxMusic = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
                    int curMusic = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
                    Log.e("Huang,SoundPoolMgr", "music_max:" + maxMusic + ", cur:" + curMusic);
                    audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, maxMusic, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
                }
                sWeakReference = new WeakReference<Context>(context);
            }
        }catch (Exception ex){
            sWeakReference = new WeakReference<Context>(context);
            ex.printStackTrace();
        }
        return SoundPoolMgrHolder.sSoundPoolMgr;
    }

    private static class SoundPoolMgrHolder {
        private static SoundPoolMgr sSoundPoolMgr = new SoundPoolMgr(sWeakReference.get());
    }

    private SoundPoolMgr(Context context) {
        if (context == null) {
            Log.e("Huang,SoundPoolMgr", "initialize SoundPoolMgr fail with context == null");
        }
        SoundPool.Builder builder = new SoundPool.Builder();
        // Builder of AudioAttributes
        AudioAttributes.Builder attrBuild = new AudioAttributes.Builder();
        // Set streamType to system
        attrBuild.setLegacyStreamType(AudioManager.STREAM_SYSTEM);
        // Set AudioAttributes to SoundPool
        builder.setAudioAttributes(attrBuild.build());
        // Set the number of audio that can be played simultaneously
        builder.setMaxStreams(4);
        // Get SoundPool object
        mSoundPool = builder.build();
        // Load sound resource
        load(context);
    }

    /**
     * Load audio files stored in array
     */
    private void load(Context context) {
        // Put your sound resource in res/raw, the following parameter 1 is the soundID
        mSoundArray.put(1, mSoundPool.load(context, R.raw.msg, 1));
        mSoundArray.put(2, mSoundPool.load(context, R.raw.beep, 1));
        mSoundArray.put(3, mSoundPool.load(context, R.raw.wlfc_done, 1));
        mSoundArray.put(4, mSoundPool.load(context, R.raw.wlbd_done, 1));
        mSoundArray.put(5, mSoundPool.load(context, R.raw.rqfz, 1));
        mSoundArray.put(6, mSoundPool.load(context, R.raw.wlz, 1));
        mSoundArray.put(7, mSoundPool.load(context, R.raw.ceng, 1));
        mSoundArray.put(8, mSoundPool.load(context, R.raw.ge, 1));
        mSoundArray.put(9, mSoundPool.load(context, R.raw.no_power, 1));
        mSoundArray.put(10, mSoundPool.load(context, R.raw.beep51, 1));
        mSoundArray.put(11, mSoundPool.load(context, R.raw.beep2, 1));
        mSoundArray.put(12, mSoundPool.load(context, R.raw.wlfz, 1));

        mSoundArray.put(10000, mSoundPool.load(context, R.raw.b0, 1));
        mSoundArray.put(10001, mSoundPool.load(context, R.raw.b1, 1));
        mSoundArray.put(10002, mSoundPool.load(context, R.raw.b2, 1));
        mSoundArray.put(10003, mSoundPool.load(context, R.raw.b3, 1));
        mSoundArray.put(10004, mSoundPool.load(context, R.raw.b4, 1));
        mSoundArray.put(10005, mSoundPool.load(context, R.raw.b5, 1));
        mSoundArray.put(10006, mSoundPool.load(context, R.raw.b6, 1));
        mSoundArray.put(10007, mSoundPool.load(context, R.raw.b7, 1));
        mSoundArray.put(10008, mSoundPool.load(context, R.raw.b8, 1));
        mSoundArray.put(10009, mSoundPool.load(context, R.raw.b9, 1));
        mSoundArray.put(10010, mSoundPool.load(context, R.raw.b10, 1));
        mSoundArray.put(10020, mSoundPool.load(context, R.raw.b20, 1));
        mSoundArray.put(10030, mSoundPool.load(context, R.raw.b30, 1));
        mSoundArray.put(10040, mSoundPool.load(context, R.raw.b40, 1));
        mSoundArray.put(10050, mSoundPool.load(context, R.raw.b50, 1));
        mSoundArray.put(10060, mSoundPool.load(context, R.raw.b60, 1));
        mSoundArray.put(10070, mSoundPool.load(context, R.raw.b70, 1));
        mSoundArray.put(10080, mSoundPool.load(context, R.raw.b80, 1));
        mSoundArray.put(10090, mSoundPool.load(context, R.raw.b90, 1));
        mSoundArray.put(10100, mSoundPool.load(context, R.raw.b100, 1));
        mSoundArray.put(10200, mSoundPool.load(context, R.raw.b200, 1));
        mSoundArray.put(10300, mSoundPool.load(context, R.raw.b300, 1));
        mSoundArray.put(10400, mSoundPool.load(context, R.raw.b400, 1));
        mSoundArray.put(10500, mSoundPool.load(context, R.raw.b500, 1));
        mSoundArray.put(10600, mSoundPool.load(context, R.raw.b600, 1));
        mSoundArray.put(10700, mSoundPool.load(context, R.raw.b700, 1));
        mSoundArray.put(10800, mSoundPool.load(context, R.raw.b800, 1));
        mSoundArray.put(10900, mSoundPool.load(context, R.raw.b900, 1));

        mSoundArray.put(20001, mSoundPool.load(context, R.raw.car_1, 1));//通用车
        mSoundArray.put(20002, mSoundPool.load(context, R.raw.car_2, 1));//领料车
        mSoundArray.put(20003, mSoundPool.load(context, R.raw.car_3, 1));//产出车
        mSoundArray.put(20004, mSoundPool.load(context, R.raw.car_4, 1));//往返车
        mSoundArray.put(20005, mSoundPool.load(context, R.raw.car_5, 1));//采收车
        mSoundArray.put(20006, mSoundPool.load(context, R.raw.car_6, 1));//质检车
        mSoundArray.put(20007, mSoundPool.load(context, R.raw.car_7, 1));//出货车

        mSoundArray.put(21001, mSoundPool.load(context, R.raw.fun_1, 1));//运行
        mSoundArray.put(21002, mSoundPool.load(context, R.raw.fun_2, 1));//前进
        mSoundArray.put(21003, mSoundPool.load(context, R.raw.fun_3, 1));//后退
        mSoundArray.put(21004, mSoundPool.load(context, R.raw.fun_4, 1));//进入分区地址
        mSoundArray.put(21005, mSoundPool.load(context, R.raw.fun_5, 1));//转向
        mSoundArray.put(21006, mSoundPool.load(context, R.raw.fun_6, 1));//下架
        mSoundArray.put(21007, mSoundPool.load(context, R.raw.fun_7, 1));//作业
        mSoundArray.put(21008, mSoundPool.load(context, R.raw.fun_8, 1));//机器人暂停
        mSoundArray.put(21009, mSoundPool.load(context, R.raw.fun_9, 1));//参数
        mSoundArray.put(21010, mSoundPool.load(context, R.raw.fun_10, 1));//设置
        mSoundArray.put(21011, mSoundPool.load(context, R.raw.fun_11, 1));//加速
        mSoundArray.put(21012, mSoundPool.load(context, R.raw.fun_12, 1));//减速
        mSoundArray.put(21013, mSoundPool.load(context, R.raw.fun_13, 1));//遇障
        mSoundArray.put(21014, mSoundPool.load(context, R.raw.fun_14, 1));//掉头
        mSoundArray.put(21015, mSoundPool.load(context, R.raw.fun_15, 1));//进入主干道地址
        mSoundArray.put(21016, mSoundPool.load(context, R.raw.fun_16, 1));//右转90度
        mSoundArray.put(21017, mSoundPool.load(context, R.raw.fun_17, 1));//左转90度

        mSoundArray.put(22001, mSoundPool.load(context, R.raw.cmm_1, 1));//开始
        mSoundArray.put(22002, mSoundPool.load(context, R.raw.cmm_2, 1));//进入
        mSoundArray.put(22003, mSoundPool.load(context, R.raw.cmm_3, 1));//机器人已到位
        mSoundArray.put(22004, mSoundPool.load(context, R.raw.cmm_4, 1));//结束
        mSoundArray.put(22005, mSoundPool.load(context, R.raw.cmm_5, 1));//开始初始化
        mSoundArray.put(22006, mSoundPool.load(context, R.raw.cmm_6, 1));//初始化完成
        mSoundArray.put(22008, mSoundPool.load(context, R.raw.cmm_8, 1));//机器人正忙
        mSoundArray.put(22009, mSoundPool.load(context, R.raw.cmm_9, 1));//长按

        mSoundArray.put(23001, mSoundPool.load(context, R.raw.ps_back, 1));//返回
        mSoundArray.put(23002, mSoundPool.load(context, R.raw.ps_bl, 1));//补料
        mSoundArray.put(23003, mSoundPool.load(context, R.raw.ps_cancel, 1));//撤销
        mSoundArray.put(23004, mSoundPool.load(context, R.raw.ps_cclz, 1));//产出流转
        mSoundArray.put(23005, mSoundPool.load(context, R.raw.ps_ccrrq, 1));//产出入容器
        mSoundArray.put(23006, mSoundPool.load(context, R.raw.ps_dd, 1));//调度
        mSoundArray.put(23007, mSoundPool.load(context, R.raw.ps_ld, 1));//拉动
        mSoundArray.put(23008, mSoundPool.load(context, R.raw.ps_next_order, 1));//下一订单
        mSoundArray.put(23009, mSoundPool.load(context, R.raw.ps_next_wo, 1));//下一工单
        mSoundArray.put(23010, mSoundPool.load(context, R.raw.ps_ok, 1));//确定
        mSoundArray.put(23011, mSoundPool.load(context, R.raw.ps_sel_addr, 1));//选择目的地
        mSoundArray.put(23012, mSoundPool.load(context, R.raw.ps_sl, 1));//上料
        mSoundArray.put(23013, mSoundPool.load(context, R.raw.ps_td, 1));//推动
        mSoundArray.put(23014, mSoundPool.load(context, R.raw.ps_tl, 1));//投料

    }

    /**
     * Play a sound from a sound ID.
     *
     * @param soundId The position of the sound resource in the mSoundArray, specified in the method load().(Here, the soundID is 1)
     */
    public void play(int soundId) {
        int streamID = mSoundPool.play(mSoundArray.get(soundId), 1, 1, 0, 2, 1);
        Log.i("Huang,SoundPoolMgr", "play() return streamID: " + streamID);
    }

    /**
     * Play a sound from a sound ID.
     *
     * @param soundId The position of the sound resource in the mSoundArray, specified in the method load().(Here, the soundID is 1)
     */
    public void play(int soundId, int loop) {
        int streamID = mSoundPool.play(mSoundArray.get(soundId), 1, 1, 0, loop, 1);
        Log.i("Huang,SoundPoolMgr", "play() return streamID: " + streamID);
    }

    public void playRaw(int soundId) {
        Context context = sWeakReference.get();
        int sound = mSoundPool.load(context, soundId, 1);
        int streamID = mSoundPool.play(sound, 1, 1, 0, 1, 1);
        Log.i("Huang,SoundPoolMgr", "play() return streamID: " + streamID);
    }

    /**
     * Release audio resources
     */
    public void release() {
//        mSoundPool.unload(mSoundArray.get(1));
//        mSoundPool.release();
//        mSoundPool = null;
        sWeakReference.clear();
        sWeakReference = null;
    }
}
