package com.longtu.sample;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.acegameproject.burstshooter.R;

import com.bh.sdk.Interface.LTBaseSDK;
import com.longtu.sdk.base.LTBaseParam;
import com.longtu.sdk.base.multiMedia.LTMultiMediaCallback;

import java.util.ArrayList;


/**
 * 语音功能
 * 作者：liqingshuo on 2019-12-02 19:35
 *
 * @author jinmingqiang
 */
public class LTBase_Demo_MultiMedia extends Activity implements OnClickListener {
	private Button StartRecording, StopRecording, PlayRecordedFileVoice, DownloadRecordedFile, PlayRecordedFile,
			SpeechToText, StopPlayFile, PauseRecording, ResumeRecording, CancelRecording;
	private Button Room, EnableMic, EnableSpeaker, ExitRoom, IsRoomEntered, GetMicLevel,
			GetSpeakerLevel, GetRoomVoiceType, SetRoomVoiceType;
	static Activity activity;
	private Button SetGetMicVolume, SetGetSpeakerVolume, PTTGetMicLevel, PTTGetSpeakerLevel;
	private EditText SetGetMicVolumeAdd, SetGetSpeakerVolumeAdd;
	private EditText editText, GetMicLevelAdd, GetSpeakerLevelAdd, GetRoomId, VoiceType, RoomMember;
	private String playUrl;

	static ArrayList<String> identifierMember = null;

	public static String TAG = "LTBaseMultiMedia.demo";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		activity = this;
		setContentView(R.layout.longtu_demo_multimedia);
		StartRecording = (Button) findViewById(R.id.StartRecording);
		StopRecording = (Button) findViewById(R.id.StopRecording);
		PlayRecordedFileVoice = (Button) findViewById(R.id.PlayRecordedFileVoice);
		//DownloadRecordedFile = (Button) findViewById(RhelperUtil.getIdResIDByName(activity, "DownloadRecordedFile"));
		PlayRecordedFile = (Button) findViewById(R.id.PlayRecordedFile);
		//SpeechToText = (Button) findViewById(RhelperUtil.getIdResIDByName(activity, "SpeechToText"));
		editText = (EditText) findViewById(R.id.textView1);
		StopPlayFile = (Button) findViewById(R.id.StopPlayFile);
		PauseRecording = (Button) findViewById(R.id.PauseRecording);
		ResumeRecording = (Button) findViewById(R.id.ResumeRecording);
		CancelRecording = (Button) findViewById(R.id.CancelRecording);
		SetGetMicVolume = (Button) findViewById(R.id.SetGetMicVolume);
		SetGetSpeakerVolume = (Button) findViewById(R.id.SetGetSpeakerVolume);
		PTTGetMicLevel = (Button) findViewById(R.id.PTTGetMicLevel);
		PTTGetSpeakerLevel = (Button) findViewById(R.id.PTTGetSpeakerLevel);
		SetGetMicVolumeAdd = (EditText) findViewById(R.id.SetGetMicVolumeAdd);
		SetGetSpeakerVolumeAdd = (EditText) findViewById(R.id.SetGetSpeakerVolumeAdd);

		Room = (Button) findViewById(R.id.Room);
		EnableMic = (Button) findViewById(R.id.EnableMic);
		EnableSpeaker = (Button) findViewById(R.id.EnableSpeaker);
		ExitRoom = (Button) findViewById(R.id.ExitRoom);
		IsRoomEntered = (Button) findViewById(R.id.IsRoomEntered);
		GetMicLevel = (Button) findViewById(R.id.GetMicLevel);
		GetSpeakerLevel = (Button) findViewById(R.id.GetSpeakerLevel);
		GetRoomVoiceType = (Button) findViewById(R.id.GetRoomVoiceType);
		SetRoomVoiceType = (Button) findViewById(R.id.SetRoomVoiceType);

		GetMicLevelAdd = (EditText) findViewById(R.id.GetMicLevelAdd);
		GetSpeakerLevelAdd = (EditText) findViewById(R.id.GetSpeakerLevelAdd);
		GetRoomId = (EditText) findViewById(R.id.GetRoomId);
		VoiceType = (EditText) findViewById(R.id.VoiceType);
		RoomMember = (EditText) findViewById(R.id.RoomMember);

		StartRecording.setOnClickListener(this);
		StopRecording.setOnClickListener(this);
		PlayRecordedFileVoice.setOnClickListener(this);
		//DownloadRecordedFile.setOnClickListener(this);
		PlayRecordedFile.setOnClickListener(this);
		//SpeechToText.setOnClickListener(this);
		//editText.setOnClickListener(this);
		StopPlayFile.setOnClickListener(this);
		PauseRecording.setOnClickListener(this);
		ResumeRecording.setOnClickListener(this);
		CancelRecording.setOnClickListener(this);
		SetGetMicVolume.setOnClickListener(this);
		SetGetSpeakerVolume.setOnClickListener(this);
		PTTGetMicLevel.setOnClickListener(this);
		PTTGetSpeakerLevel.setOnClickListener(this);

		Room.setOnClickListener(this);
		EnableMic.setOnClickListener(this);
		EnableSpeaker.setOnClickListener(this);
		ExitRoom.setOnClickListener(this);
		IsRoomEntered.setOnClickListener(this);
		GetMicLevel.setOnClickListener(this);
		GetSpeakerLevel.setOnClickListener(this);
		GetRoomVoiceType.setOnClickListener(this);
		SetRoomVoiceType.setOnClickListener(this);




		Log.d(TAG, "语音SDK设置回调");
		LTBaseSDK.getInstance(activity).LTBaseMultiMediaListener(new LTMultiMediaCallback() {
			@Override
			public void LTBaseMultiMediaSuccess(int i, Intent intent) {
				final  Intent data = intent;
				String[] identifierList;
				Log.d(TAG,"code=" + i);
				switch (i) {
					case LTBaseParam.CODE_PTT_STREAMINGRECOGNITION_SUCCESS: {//录音成功
						//语音转文字识别的文本
						String textString = data.getStringExtra("text");
						//录音在后台的 url 地址，录音在服务器存放90天
						playUrl = data.getStringExtra("file_id");
						String filePath = data.getStringExtra("file_path");
						int voiceDuration = data.getExtras().getInt("voice_duration");
						editText.setText(textString);
						Log.d(TAG, "录音时长=====" + voiceDuration);
						Log.d(TAG, "filepath=====" + filePath);
						Toast.makeText(activity, "录音成功", Toast.LENGTH_LONG).show();
						break;
					}

					case LTBaseParam.CODE_PTT_STREAMINGRECOGNITION_FAIL: {//录音失败
						Log.d(TAG, "录音失败");
						Toast.makeText(activity, "录音失败", Toast.LENGTH_LONG).show();
						break;

					}

					case LTBaseParam.CODE_PTT_PLAY_SUCCESS: {
						Log.d(TAG, "播放语音成功");
						break;
					}

					case LTBaseParam.CODE_PTT_PLAY_FAIL: {
						Log.d(TAG, "播放语音失败");
						break;
					}

					case LTBaseParam.CODE_ROOM_ENTERROOM_SUCCESS:
						Log.d(TAG, "进入房间成功");
						Toast.makeText(activity, "成功进入房间", Toast.LENGTH_LONG).show();
						break;

					case LTBaseParam.CODE_ROOM_ENTERROOM_FAIL:
						Log.d(TAG, "进入房间失败");
						Toast.makeText(activity, "进入房间失败：code=" + data.getIntExtra("result", -1), Toast.LENGTH_LONG).show();
						break;

					case LTBaseParam.CODE_ROOM_EXITROOM_SUCCESS:
						Log.d(TAG, "退出了房间");
						Toast.makeText(activity, "退出了房间", Toast.LENGTH_LONG).show();
						identifierMember.clear();
						RoomMember.setText("");
						break;

					case LTBaseParam.CODE_ROOM_USERROOM_ENTER:
						Log.d(TAG, "有成员进入了房间");
						Toast.makeText(activity, "有成员进入了房间", Toast.LENGTH_LONG).show();
						identifierList = data.getStringArrayExtra("user_list");
						RoomMember.setText(getIdentifierList(identifierList, 1));
						break;

					case LTBaseParam.CODE_ROOM_USERROOM_EXIT:
						Log.d(TAG, "有成员退出了房间");
						Toast.makeText(activity, "有成员退出了房间", Toast.LENGTH_LONG).show();
						identifierList = data.getStringArrayExtra("user_list");
						RoomMember.setText(getIdentifierList(identifierList, 0));
						break;

					case LTBaseParam.CODE_ROOM_CHANGEROOMTYPE_COMPLETE:
						Log.d(TAG, "切换了语音");
						Toast.makeText(activity, "切换了语音：" + data.getIntExtra("new_room_type", 0), Toast.LENGTH_LONG).show();
						break;
				}
			}

			@Override
			public void LTBaseMultiMediaFail(int i, String s) {
				Log.d(TAG, "语音SDK异常");
			}
		});

	}

	public String getIdentifierList(String [] list, int type){
		String identifier = "";
		int i;
		if (identifierMember == null) {
			identifierMember = new ArrayList<String>();
		}

		if (type == 1) {
			for (i = 0; i < list.length; i++) {
				Log.d(TAG, "加入房间成员：" + list[i]);
				identifierMember.add(list[i]);
			}
		} else if(type == 0){
			for (i = 0; i < list.length; i++) {
				identifierMember.remove(list[i]);
				Log.d(TAG, "删除房间成员：" + list[i]);
			}

		}

		for (i = 0; i < identifierMember.size(); i++) {
			identifier += identifierMember.get(i) + "\n";

			Log.d(TAG, "房间成员：" + identifier);
		}

		return identifier;
	}


	@Override
	public void onClick(View view) {
		int voiceType;
		switch (view.getId()) {
			case R.id.StartRecording:
				Log.d(TAG, "StartRecording=====");
				//Toast.makeText(this, "开始录音了，停止请点击停止录音", Toast.LENGTH_LONG).show();
				LTBaseSDK.getInstance(activity).LTBaseStartRecordingWithStreamingRecognition();
				break;
			case R.id.StopRecording:
				Log.d(TAG, "StopRecording=====");
				LTBaseSDK.getInstance(activity).LTBaseStopRecording();
				//Toast.makeText(this, "停止录音了，播放请点击播放录音", Toast.LENGTH_LONG).show();
				break;

			case R.id.PlayRecordedFile:
				Log.d(TAG, "PlayRecordedFile=====");
				//Toast.makeText(this, "播放语音", Toast.LENGTH_LONG).show();
				LTBaseSDK.getInstance(activity).LTBasePlayRecordedFile(playUrl);
				break;

			case R.id.PlayRecordedFileVoice:
				Log.d(TAG, "PlayRecordedFileVoice=====");
				//Toast.makeText(this, "播放变声语音", Toast.LENGTH_LONG).show();
				LTBaseSDK.getInstance(activity).LTBasePlayRecordedFileVoice(playUrl, 1);
				break;

			case R.id.Room:
				Log.d(TAG, "GetRoom=====");
				voiceType = TextUtils.isEmpty(VoiceType.getText())== true ? 1: Integer.parseInt(VoiceType.getText().toString());
				Log.d(TAG, "GetRoom=====voiceType=" + voiceType);
				if (TextUtils.isEmpty(GetRoomId.getText())) {
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaEnterRoom("192837", voiceType);
				} else {
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaEnterRoom(GetRoomId.getText().toString(), voiceType);
				}
				break;

			case R.id.EnableMic:
				Log.d(TAG, "EnableMic=====");
				if (LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetMicState() == 0){
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaEnableMic(true);
					Toast.makeText(this, "打开了麦克风", Toast.LENGTH_LONG).show();
				} else {
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaEnableMic(false);
					Toast.makeText(this, "关闭了麦克风", Toast.LENGTH_LONG).show();
				}

				break;

			case R.id.EnableSpeaker:
				Log.d(TAG, "EnableSpeaker=====");
				if (LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetSpeakerState() == 0){
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaEnableSpeaker(true);
					Toast.makeText(this, "打开了扬声器", Toast.LENGTH_LONG).show();
				} else {
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaEnableSpeaker(false);
					Toast.makeText(this, "关闭了扬声器", Toast.LENGTH_LONG).show();
				}

				break;

			case R.id.ExitRoom:
				Log.d(TAG, "ExitRoom=====");
				LTBaseSDK.getInstance(activity).LTBaseMultiMediaExitRoom();
				break;

			case R.id.IsRoomEntered:
				Log.d(TAG, "IsRoomEntered=====");
				Toast.makeText(this, "是否进入了房间：" + LTBaseSDK.getInstance(activity).LTBaseMultiMediaIsRoomEntered(),
						Toast.LENGTH_LONG).show();

				break;

			case R.id.GetMicLevel:
				Log.d(TAG, "GetMicLevel=====");
				if (TextUtils.isEmpty(GetMicLevelAdd.getText())){
					Toast.makeText(this, "麦克风音量：" + LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetMicVolume(),
							Toast.LENGTH_LONG).show();
				} else {
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaSetMicVolume(Integer.parseInt(GetMicLevelAdd.getText().toString()));
					Toast.makeText(this, "麦克风音量：" + LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetMicVolume(),
							Toast.LENGTH_LONG).show();
				}
				break;

			case R.id.GetSpeakerLevel:
				Log.d(TAG, "GetSpeakerLevel=====");
				if (TextUtils.isEmpty(GetSpeakerLevelAdd.getText())){
					Toast.makeText(this, "扬声器音量：" + LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetSpeakerVolume(),
							Toast.LENGTH_LONG).show();
				} else {
					LTBaseSDK.getInstance(activity).LTBaseMultiMediaSetSpeakerVolume(Integer.parseInt(GetSpeakerLevelAdd.getText().toString()));
					Toast.makeText(this, "扬声器音量：" + LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetSpeakerVolume(),
							Toast.LENGTH_LONG).show();
				}
				break;

			case R.id.GetRoomVoiceType:
				Log.d(TAG, "GetRoomVoiceType=====");
				Toast.makeText(this, "音频类型：" + LTBaseSDK.getInstance(activity).LTBaseMultiMediaGetRoomType(),
						Toast.LENGTH_LONG).show();
				break;

			case R.id.SetRoomVoiceType:
				Log.d(TAG, "SetRoomVoiceType=====");
				voiceType = TextUtils.isEmpty(VoiceType.getText())== true ? 1: Integer.parseInt(VoiceType.getText().toString());
				LTBaseSDK.getInstance(activity).LTBaseMultiMediaChangeRoomType(voiceType);
				break;

			case R.id.StopPlayFile:
				Log.d(TAG, "StopPlayFile=====");
				LTBaseSDK.getInstance(activity).LTBaseStopPlayFile();
				break;
			case R.id.PauseRecording:
				Log.d(TAG, "PauseRecording=====");
				LTBaseSDK.getInstance(activity).LTBasePauseRecording();
				break;
			case R.id.ResumeRecording:
				Log.d(TAG, "ResumeRecording=====");
				LTBaseSDK.getInstance(activity).LTBaseResumeRecording();
				break;
			case R.id.CancelRecording:
				Log.d(TAG, "CancelRecording=====");
				LTBaseSDK.getInstance(activity).LTBaseCancelRecording();
				break;
			case R.id.SetGetMicVolume:
				Log.d(TAG, "SetGetMicVolume=====");
				if (TextUtils.isEmpty(SetGetMicVolumeAdd.getText())){
					Toast.makeText(this, "录制音量：" + LTBaseSDK.getInstance(activity).LTBasePttGetMicLevel(),
							Toast.LENGTH_LONG).show();
				} else {
					LTBaseSDK.getInstance(activity).LTBasePttSetMicVolume(Integer.parseInt(SetGetMicVolumeAdd.getText().toString()));
					Toast.makeText(this, "录制音量：" + LTBaseSDK.getInstance(activity).LTBasePttGetMicLevel(),
							Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.SetGetSpeakerVolume:
				Log.d(TAG, "SetGetSpeakerVolume=====");
				if (TextUtils.isEmpty(SetGetSpeakerVolumeAdd.getText())){
					Toast.makeText(this, "播放音量：" + LTBaseSDK.getInstance(activity).LTBasePttGetSpeakerVolume(),
							Toast.LENGTH_LONG).show();
				} else {
					LTBaseSDK.getInstance(activity).LTBasePttSetSpeakerVolume(Integer.parseInt(SetGetSpeakerVolumeAdd.getText().toString()));
					Toast.makeText(this, "播放音量：" + LTBaseSDK.getInstance(activity).LTBasePttGetSpeakerVolume(),
							Toast.LENGTH_LONG).show();
				}
				break;
			case R.id.PTTGetMicLevel:
				Log.d(TAG, "PTTGetMicLevel=====");
				Toast.makeText(this, "麦克风实时音量：" + LTBaseSDK.getInstance(activity).LTBasePttGetMicLevel(),
						Toast.LENGTH_LONG).show();
				break;
			case R.id.PTTGetSpeakerLevel:
				Log.d(TAG, "PTTGetSpeakerLevel=====");
				Toast.makeText(this, "扬声器实时音量：" + LTBaseSDK.getInstance(activity).LTBasePttGetSpeakerLevel(),
						Toast.LENGTH_LONG).show();
				break;
			default:
				break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
	}
	@Override
	protected void onPause() {

		LTBaseSDK.getInstance(this).onPause();
		super.onPause();
	}
	@Override
	protected void onStart() {
		LTBaseSDK.getInstance(this).onStart();
		super.onStart();
	}
	//String [] aa;
	@Override
	protected void onResume() {
		RoomMember.setText(getIdentifierList(new String[6],2));
		LTBaseSDK.getInstance(this).onResume();
		super.onResume();
	}
	@Override
	protected void onNewIntent(Intent newIntent) {

		super.onNewIntent(newIntent);
	}
	@Override
	protected void onStop() {
		LTBaseSDK.getInstance(this).onStop();
		super.onStop();
	}
	@Override
	protected void onDestroy() {
		LTBaseSDK.getInstance(this).onDestroy();
		super.onDestroy();

	}
	@Override
	protected void onRestart() {
		LTBaseSDK.getInstance(this).onRestart();
		super.onRestart();
	}
	@Override
	public void onBackPressed() {

		super.onBackPressed();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState=====");
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.d(TAG, "onRestoreInstanceState");
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onRequestPermissionsResult(int requestCode,
										   String[] permissions, int[] grantResults) {
		Log.d(TAG, "onRequestPermissionsResult");
		LTBaseSDK.getInstance(this).onRequestPermissionsResult(requestCode, permissions, grantResults);
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);

	}
	
	
}
