package chaoran.business.engine;

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

import android.content.Context;
import android.content.SharedPreferences;
import chaoran.business.engine.entity.Setting;
import chaoran.business.engine.entity.VoiceSetting;

/**
 * 离线语音引擎
 */
public abstract class OfflineVoiceEngine implements VoiceEngine {

    protected Context context;

    public OfflineVoiceEngine(Context context) {
        this.context = context;
    }

    /**
     * 持久化配置到文件
     *
     * @param setting
     * @return
     */
    public boolean saveSetting(Setting setting) {
        VoiceSetting voiceSetting = (VoiceSetting) setting;
        SharedPreferences.Editor editor = context.getSharedPreferences("voiceEngine", Context.MODE_PRIVATE).edit();
        editor.putString("voiceSize", voiceSetting.getVoiceSize());
        editor.putString("voiceIndicate", voiceSetting.getVoiceIndicate());
        editor.putString("voiceSpeed", voiceSetting.getVoiceSpeed());
        editor.putString("voiceMember", voiceSetting.getVoiceMember());
        editor.commit();
        return true;
    }

    /**
     * 设置参数，用于动态改变语音设置
     *
     * @param voiceMember
     * @param voiceSize
     * @param voiceIndicate
     * @param voiceSpeed
     */
    public abstract void setParam(String voiceMember, String voiceSize, String voiceIndicate, String voiceSpeed);


    /**
     * 获取语音配置参数
     *
     * @return
     */
    public Setting getSetting() {
        SharedPreferences sharedPreferences = context.getSharedPreferences("voiceEngine", Context.MODE_PRIVATE);

        //设置发音人资源路径
        String voiceMember = sharedPreferences.getString("voiceMember", "xiaofeng");

        //设置合成语速
        String voiceSpeed = sharedPreferences.getString("voiceSpeed", "80");

        //设置合成音调
        String voiceIndicate = sharedPreferences.getString("voiceIndicate", "50");

        //设置合成音量
        String voiceSize = sharedPreferences.getString("voiceSize", "100");

        VoiceSetting voiceSetting = new VoiceSetting();
        voiceSetting.setVoiceMember(voiceMember);
        voiceSetting.setVoiceSize(voiceSize);
        voiceSetting.setVoiceIndicate(voiceIndicate);
        voiceSetting.setVoiceSpeed(voiceSpeed);
        return voiceSetting;
    }

}
