package chaoran.business.activity;

/*
 **********************************************
 *      DATE           PERSON       REASON
 *    2021-02-05          FXY        Created
 **********************************************
 */


import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import chaoran.business.R;
import chaoran.business.engine.OfflineVoiceEngine;
import chaoran.business.engine.impl.TekVoiceEngine;
import chaoran.business.engine.VoiceEngine;

/**
 * 播报语音设置
 */
public class VoiceSettingActivity extends AppCompatActivity {

    private EditText testText;
    private RadioButton voiceF, voiceY;
    private SeekBar voiceSize, voiceSpeed, voiceIndicate;
    private Button save, cancel;
    private SharedPreferences sharedPreferences;
    private RadioGroup voiceMemberGroup;

    private OfflineVoiceEngine voiceEngine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_voice);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        setTitle(R.string.title_activity_setting_voice);
        testText = findViewById(R.id.voice_test_text);
        voiceF = findViewById(R.id.voice_member_xiaofeng);
        voiceY = findViewById(R.id.voice_member_xiaoyan);
        voiceSize = findViewById(R.id.voice_size);
        voiceSpeed = findViewById(R.id.voice_speed);
        voiceIndicate = findViewById(R.id.voice_indicate);
        save = findViewById(R.id.save_voice_setting);
        cancel = findViewById(R.id.cancel_voice_setting);
        voiceMemberGroup = findViewById(R.id.voice_member_group);
        voiceEngine = new TekVoiceEngine(this);
    }

    private void initData() {
        sharedPreferences = getSharedPreferences("voiceEngine", MODE_PRIVATE);
        //设置测试文本
        testText.setText(sharedPreferences.getString("testText", testText.getText().toString()));
        //设置播报人员
        String voiceMember = sharedPreferences.getString("voiceMember", "xiaofeng");
        if (voiceMember.equals("xiaofeng")) {
            voiceF.setChecked(true);
            voiceY.setChecked(false);
        } else {
            voiceY.setChecked(true);
            voiceF.setChecked(false);
        }
        //设置功能选项
        voiceSize.setProgress(Integer.parseInt(sharedPreferences.getString("voiceSize", "100")));
        voiceSpeed.setProgress(Integer.parseInt(sharedPreferences.getString("voiceSpeed", "80")));
        voiceIndicate.setProgress(Integer.parseInt(sharedPreferences.getString("voiceIndicate", "50")));
    }

    public void initListener() {
        SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //停止播放
                voiceEngine.stopSpeaking();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //播放语音
                voiceEngine.setParam(voiceF.isChecked() ? "xiaofeng" : "xiaoyan", String.valueOf(voiceSize.getProgress()), String.valueOf(voiceIndicate.getProgress()), String.valueOf(voiceSpeed.getProgress()));
                voiceEngine.startSpeaking(testText.getText().toString());
            }
        };
        voiceSize.setOnSeekBarChangeListener(seekBarChangeListener);
        voiceIndicate.setOnSeekBarChangeListener(seekBarChangeListener);
        voiceSpeed.setOnSeekBarChangeListener(seekBarChangeListener);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        save.setOnClickListener(a -> {
            editor.putString("voiceSize", String.valueOf(voiceSize.getProgress()));
            editor.putString("voiceIndicate", String.valueOf(voiceIndicate.getProgress()));
            editor.putString("voiceSpeed", String.valueOf(voiceSpeed.getProgress()));
            editor.putString("testText", testText.getText().toString());
            editor.putString("voiceMember", voiceF.isChecked() ? "xiaofeng" : "xiaoyan");
            editor.commit();
            voiceEngine.stopSpeaking();
            voiceEngine.destroy();
            Toast.makeText(this, "语音配置保存成功！", Toast.LENGTH_SHORT).show();
            this.finish();
        });
        cancel.setOnClickListener(a -> {
            voiceEngine.stopSpeaking();
            voiceEngine.destroy();
            this.finish();
        });

        voiceMemberGroup.setOnCheckedChangeListener((a, b) -> {
            //播放语音
            voiceEngine.setParam(voiceF.isChecked() ? "xiaofeng" : "xiaoyan", String.valueOf(voiceSize.getProgress()), String.valueOf(voiceIndicate.getProgress()), String.valueOf(voiceSpeed.getProgress()));
            voiceEngine.startSpeaking(testText.getText().toString());
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        //初始化加载一次
        voiceEngine.setParam(voiceF.isChecked() ? "xiaofeng" : "xiaoyan", String.valueOf(voiceSize.getProgress()), String.valueOf(voiceIndicate.getProgress()), String.valueOf(voiceSpeed.getProgress()));
        voiceEngine.startSpeaking(testText.getText().toString());
    }

    @Override
    protected void onDestroy() {
        voiceEngine.stopSpeaking();
        voiceEngine.destroy();
        super.onDestroy();
    }
}
