package com.tencent.gme.audio.apiexample;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.gme.TMG.ITMGContext;
import com.gme.av.sdk.AVError;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    static final String g_APPID = AppID To Replace Here https://cloud.tencent.com/document/product/607/10782;
    static final String g_APPKEY = AppKey To Replace Here https://cloud.tencent.com/document/product/607/10782;

    private EditText mAppIdView, mKeyView, mOpenIdView;
    private Button mRealTimeVoiceBtn, mPttBtn;
    private Button mTextToSpeechBtn;
    private Button mTranslateTextBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        mAppIdView.setText(g_APPID);
        mKeyView.setText(g_APPKEY);
        mOpenIdView.setText(String.valueOf(System.currentTimeMillis() % 10000 + 10000));

        //启动一个service用于提高APP的后台优先级，避免部分机型使用openssl时播放声音异常
        Intent intent = new Intent(this, AudioService.class);
        startService(intent);

        if (Build.VERSION.SDK_INT >= 23) {
            int REQUEST_CODE_CONTACT = 101;
            String[] permissions = { Manifest.permission.RECORD_AUDIO };
            //验证是否许可权限
            for (String str : permissions) {
                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
                    //申请权限
                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                }
            }
        }
    }

    private void initViews() {
        mAppIdView = (EditText) findViewById(R.id.etAppId);
        mKeyView = findViewById(R.id.etKey);
        mOpenIdView = findViewById(R.id.etOpenId);
        findViewById(R.id.btnInit).setOnClickListener(this);
        findViewById(R.id.btnUnInit).setOnClickListener(this);
        mRealTimeVoiceBtn = findViewById(R.id.btnRealTimeVoice);
        mRealTimeVoiceBtn.setOnClickListener(this);
        mPttBtn = findViewById(R.id.btnPTT);
        mPttBtn.setOnClickListener(this);
        findViewById(R.id.ll_root).setOnClickListener(this);
        mTextToSpeechBtn = findViewById(R.id.btnTextToSpeech);
        mTextToSpeechBtn.setOnClickListener(this);
        mTranslateTextBtn = findViewById(R.id.btnTranslateText);
        mTranslateTextBtn.setOnClickListener(this);

        mRealTimeVoiceBtn.setEnabled(false);
        mPttBtn.setEnabled(false);
        mTextToSpeechBtn.setEnabled(false);
        mTranslateTextBtn.setEnabled(false);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnInit) {
            onInitClick();
        }else if(id == R.id.btnUnInit)
        {
            onUnInitClick();
        }else if (id == R.id.btnRealTimeVoice)
        {
            onRealTimeVoiceClick();
        }else if (id == R.id.btnPTT)
        {
            onVoiceMessageRecognitionClick();
        } else if (id == R.id.btnTextToSpeech) {
            onTextToSpeechClick();
        } else if (id == R.id.btnTranslateText) {
            onTranslateTextClick();
        }
    }

    private void onTranslateTextClick() {
        Intent i = new Intent(this, TranslateTextActivity.class);
        this.startActivity(i);
    }

    // handle Init button click
    private void onInitClick()
    {
        String appId = mAppIdView.getText().toString().trim();
        if (TextUtils.isEmpty(appId))
        {
            showToast("Please input AppId");
            return;
        }
        String key = mKeyView.getText().toString().trim();
        if (TextUtils.isEmpty(key))
        {
            showToast("Please input Key");
            return;
        }

        String openId = mOpenIdView.getText().toString().trim();
        if (TextUtils.isEmpty(openId))
        {
            showToast("Please input OpenId");
            return;
        }
        // step 1/11: get ITMGContext
        // https://cloud.tencent.com/document/product/607/15210#.E8.8E.B7.E5.8F.96.E5.8D.95.E4.BE.8B
        ITMGContext tmgContext = ITMGContext.GetInstance(this);

        // step 2/11: Set GME CallBack
        //https://cloud.tencent.com/document/product/607/15210#.E6.B3.A8.E5.86.8C.E5.9B.9E.E8.B0.83
        tmgContext.SetTMGDelegate(TMGCallbackDispatcher.getInstance());

        // Step 3/11 Init GME
        // https://cloud.tencent.com/document/product/607/15210#.3Ca-id.3D.22init.22.3E.E5.88.9D.E5.A7.8B.E5.8C.96-sdk.3C.2Fa.3E
        int nRet = tmgContext.Init(appId, openId);
        if (nRet == AVError.AV_OK )
        {
            GMEAuthBufferHelper.getInstance().setGEMParams(appId, key, openId);

            // Step 4/11: Poll to trigger callback
            // https://cloud.tencent.com/document/product/607/15210#.3Ca-id.3D.22poll.22.3E.E8.A7.A6.E5.8F.91.E4.BA.8B.E4.BB.B6.E5.9B.9E.E8.B0.83.3C.2Fa.3E
            EnginePollHelper.createEnginePollHelper();

            showToast("Init success");
        }else if (nRet == AVError.AV_ERR_HAS_IN_THE_STATE) // 已经初始化过了,可以认为本次操作是成功的
        {
            showToast("Init success");
        }else
        {
            showToast("Init error errorCode:" + nRet);
        }

        mRealTimeVoiceBtn.setEnabled(true);
        mPttBtn.setEnabled(true);
        mTextToSpeechBtn.setEnabled(true);
        mTranslateTextBtn.setEnabled(true);
    }

    // handle unInit button click
    private void onUnInitClick()
    {
        // 反初始化
        // https://cloud.tencent.com/document/product/607/15210#.3Ca-id.3D.22uninit.22.3E.E5.8F.8D.E5.88.9D.E5.A7.8B.E5.8C.96-sdk.3C.2Fa.3E
        ITMGContext.GetInstance(this).Uninit();

        mRealTimeVoiceBtn.setEnabled(false);
        mPttBtn.setEnabled(false);
        mTextToSpeechBtn.setEnabled(false);
        mTranslateTextBtn.setEnabled(false);
    }

    // handle Real time voice button click
    private void onRealTimeVoiceClick()
    {
        Intent i = new Intent(this, RealTimeVoiceActivity.class);
        this.startActivity(i);
    }

    // handle voice message & recognition button click
    private void onVoiceMessageRecognitionClick()
    {
        Intent i = new Intent(this, VoiceMessageRecognitionActivity.class);
        this.startActivity(i);
    }

    // handle Text To Speech button click
    private void onTextToSpeechClick() {
        Intent i = new Intent(this, TextToSpeechActivity.class);
        this.startActivity(i);
    }
}