package com.example.kingway.ptt;

import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class pttNative {
    private static String TAG = "pttNative";
    static {
        try {
            System.loadLibrary("ptt");
        } catch (Throwable e) {
            Log.e(TAG, "Failed to load ptt library: " + e.getMessage());
        }
    }

    public static char Char2ASCII(char c)
    {
        return (char) (c <= 9 ? (c+0x30) : (c+0x37));
    }

    public static char Ascii2Char(char nib_h, char nib_l)
    {
        nib_h = (nib_h <= '9')?((char)(nib_h-0x30)):(((nib_h<='F')?((char)(nib_h-0x37)):((char)(nib_h-0x57))));
        nib_l = (nib_l <= '9')?((char)(nib_l-0x30)):(((nib_l<='F')?((char)(nib_l-0x37)):((char)(nib_l-0x57))));
        return (char)((nib_h<<4)+nib_l);
    }


    //Callback Function for Recv the message from POC Lib
    public void OEM_AT_cb(String sData)
    {
        Log.e(TAG,"Poc2 AT Callback: " + sData);
        int len, i, j=0;
        char[] bytes = sData.toCharArray();

        List<String> lisHex = new ArrayList<>();
        if (bytes.length > 5 && bytes[0] == '+' && bytes[1] == 'P' && bytes[2] == 'O' && bytes[3] == 'C' && bytes[4] == ':') {
            len = bytes.length - 5;
            if (len >= 2) {
                for (i = 0; i < len - 1; i = i + 2) {
                    String temp = Integer.toHexString(Ascii2Char(bytes[i + 5], bytes[i + 6]));

                    lisHex.add(temp.length() == 1 ? "0" + temp : temp);
                }
            }
        }

        CallBackResolution.at_OEM_AT_cb(lisHex);
    }

    public void OEM_Play_cb(byte[] bytes){
        if (mAudioTrack == null) {
            initAudioTrack();
        }

        if (mAudioTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING){
            mAudioTrack.play();
        }
        mAudioTrack.write(bytes, 0, bytes.length);
    }

    public  void OEM_Play_Start_cb()
    {
        Log.e("Play Start!!!!\r\n","");
    }

    public void OEM_Play_Stop_cb()
    {
        Log.e("Play Stop!!!!\r\n","");
    }

    public  void OEM_Record_Start_cb()
    {
        Log.e("Record Start!!!!\r\n","");
    }

    public void OEM_Record_Stop_cb()
    {
        Log.e("Record Stop!!!!\r\n","");
    }

    public void  OEM_Play_TTS_cb(String sData)
    {
        Log.e(TAG , "Play TTS " + sData);
        CallBackResolution.at_Play_TTS_cb(sData);
    }

    //Define the Function of the AT Command send
    private native void OEM_AT_Send(String strAtCmd);
    private native int OEM_Set_AT_CallBack(String cb);

    private native void OEM_Push_Record_Data(byte[]  data);
    private native int OEM_Set_Play_CallBack(String cb);

    private native int OEM_Set_Play_Start_Callback(String cb);
    private native int OEM_Set_Play_Stop_Callback(String cb);

    private native int OEM_Set_Record_Start_Callback(String cb);
    private native int OEM_Set_Record_Stop_Callback(String cb);

    private native int OEM_Set_TTS_Callback(String cb);

    private native void OEM_Poc_Task_Start();

    private native int OEM_Set_Package_Name(String name);

    public void pttNativeSendRecordCmd(byte[] data) {
        OEM_Push_Record_Data(data);
    }

    public void pttNativeSendAtCmd(String strAtCmd)
    {
        try {
            Log.i(TAG,"strAtCmd " + strAtCmd);
            OEM_AT_Send(strAtCmd);
        } catch (Throwable e) {
            Log.e(TAG, "Native AT Send failed: " + e.getMessage());
        }
    }

    public void pttNativePocTaskStart()
    {
        try {
            OEM_Poc_Task_Start();
        } catch (Throwable e) {
            Log.e(TAG, "Native PocTaskStart failed");
        }
    }

    public int pttNativeSetAtCallback()
    {
        try {
            return OEM_Set_AT_CallBack("OEM_AT_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetPlayCallback()
    {
        try {
            return OEM_Set_Play_CallBack("OEM_Play_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetPalyStartCallback()
    {
        try {
            return OEM_Set_Play_Start_Callback("OEM_Play_Start_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetPlayStopCallback()
    {
        try {
            return OEM_Set_Play_Stop_Callback("OEM_Play_Stop_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetRecordStartCallback()
    {
        try {
            return OEM_Set_Record_Start_Callback("OEM_Record_Start_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetRecordStopCallback()
    {
        try {
            return OEM_Set_Record_Stop_Callback("OEM_Record_Stop_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetTTSCallback()
    {
        try {
            return OEM_Set_TTS_Callback("OEM_Play_TTS_cb");
        } catch (Throwable e) {
            return -1;
        }
    }

    public int pttNativeSetPackageName(String name)
    {
        try {
            return OEM_Set_Package_Name(name);
        } catch (Throwable e) {
            return -1;
        }
    }

    public pttNative() {
        initAudioTrack();
    }

    private AudioTrack mAudioTrack;
    public void initAudioTrack() {
        int mAudioMinBufSize = AudioTrack.getMinBufferSize(8000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) // 通信场景
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) // 内容类型
                .setLegacyStreamType(AudioManager.STREAM_MUSIC) // 设置通话流类型
                .build();

        AudioFormat audioFormat = new AudioFormat.Builder()
                .setSampleRate(8000)
                .setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                .build();

        mAudioTrack = new AudioTrack(
                audioAttributes,
                audioFormat,
                mAudioMinBufSize * 4,
                AudioTrack.MODE_STREAM,
                AudioManager.AUDIO_SESSION_ID_GENERATE
        );
        mAudioTrack.setVolume(1.0f);// 设置音量

    }

    public void destory() {
        if (mAudioTrack != null) {
            try {
                if (mAudioTrack.getState() == AudioRecord.STATE_INITIALIZED) {
                    mAudioTrack.stop();
                }
                mAudioTrack.release();
                mAudioTrack = null;
            } catch (Exception e) {}
        }
    }

}
