package com.baidu.ai.sample.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.baidu.ai.sample.R;
import com.baidu.ai.sample.util.Utils;
import com.baidu.rtc.RtcParameterSettings;

import org.json.JSONObject;

public class AIClientData implements Parcelable {
    private static final String TAG = AIClientData.class.getSimpleName();
    public String mAppId = "";
    public String mContext = "";
    public String mTokenStr = "";
    public String mInstanceId = "";

    public String mAudioCodec = RtcParameterSettings.AudioCodecId.OPUS;
    /*** 切换不同编码器 */
//    public String mAudioCodec = RtcParameterSettings.AudioCodecId.PCMU;

    public String mLLMName = "";
    public String mLLMUrl = "";
    public String mLLMCfg = "";
    public String mLLMToken = "";

    public String mVoiceConfig = "";
    public String mLang = "";

    public String mRole = "";

    public String mSense = "";


    public String mSay = "";

    public String mFigureId = "";

    public String mLocation = "";

    public String mCoordinate = "";

    // 增强query插入类型包括:不插入、前插、后插、前后双插
    public String mQueryEnhanceType;

    // 增强query前插文本
    public String mPreQuery;

    // 增强query后插文本
    public String mPostQuery;

    /** TTS_END_SPEAKING 延时 200ms */
    public int mTTSEndDelayMs = 200;

    public AIClientData() {
    }
    protected AIClientData(Parcel in) {
        mAppId = in.readString();
        mContext = in.readString();
        mTokenStr = in.readString();
        mInstanceId = in.readString();
        mAudioCodec = in.readString();
        mLLMName = in.readString();
        mLLMUrl = in.readString();
        mLLMCfg = in.readString();
        mLLMToken = in.readString();
        mVoiceConfig = in.readString();
        mLang = in.readString();
        mRole = in.readString();
        mSense = in.readString();
        mSay = in.readString();
        mLocation = in.readString();
        mCoordinate = in.readString();
        mQueryEnhanceType = in.readString();
        mPreQuery = in.readString();
        mPostQuery = in.readString();
        mTTSEndDelayMs = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mAppId);
        dest.writeString(mContext);
        dest.writeString(mTokenStr);
        dest.writeString(mInstanceId);
        dest.writeString(mAudioCodec);
        dest.writeString(mLLMName);
        dest.writeString(mLLMUrl);
        dest.writeString(mLLMCfg);
        dest.writeString(mLLMToken);
        dest.writeString(mVoiceConfig);
        dest.writeString(mLang);
        dest.writeString(mRole);
        dest.writeString(mSense);
        dest.writeString(mSay);
        dest.writeString(mLocation);
        dest.writeString(mCoordinate);
        dest.writeString(mQueryEnhanceType);
        dest.writeString(mPreQuery);
        dest.writeString(mPostQuery);
        dest.writeInt(mTTSEndDelayMs);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<AIClientData> CREATOR = new Creator<AIClientData>() {
        @Override
        public AIClientData createFromParcel(Parcel in) {
            return new AIClientData(in);
        }

        @Override
        public AIClientData[] newArray(int size) {
            return new AIClientData[size];
        }
    };

    /** 读取缓存信息 */
    public void readCacheData(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        mAppId = prefs.getString("key_appid", context.getString(R.string.agent_default_app_id));
        mTokenStr = prefs.getString("key_token", context.getString(R.string.baidu_rtc_token_tmp));

        mInstanceId = prefs.getString("key_instance_id", String.valueOf(Utils.getDeviceId()));
        mLLMName = prefs.getString("key_llm_id", "");
        mLang = prefs.getString("key_lang_id", "");
        mRole = prefs.getString("key_role_id", "");
        mQueryEnhanceType = prefs.getString("key_query_enhance_type", "");
        mPreQuery = prefs.getString("key_pre_query", "");
        mPostQuery = prefs.getString("key_post_query", "");

        if (!TextUtils.isEmpty(mInstanceId)) {
            try {
                SharedPreferences.Editor editor =  prefs.edit();
                editor.putString("key_instance_id", mInstanceId);
                editor.apply();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        checkData(context);
    }

    public void parseIntent(Context context, String config) {
        if (TextUtils.isEmpty(config)) {
            return;
        }
        try {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

            JSONObject root = new JSONObject(config);
            String appId = root.optString("appid", "");
            if (!TextUtils.isEmpty(appId)) {
                SharedPreferences.Editor editor =  prefs.edit();
                editor.putString("key_appid", appId);
                editor.apply();
                mAppId = appId;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void checkData(Context context) {

        if (TextUtils.isEmpty(mAppId)) {
            mAppId = context.getString(R.string.agent_default_app_id);
        }
    }

    public String dumpString() {
        StringBuilder sb = new StringBuilder();
        sb.append("appId:" + mAppId + ", ");
        sb.append("token:" + mTokenStr + ", ");
        sb.append("instanceId:" + mInstanceId + ", ");
        sb.append("config:" + mContext + ", ");
        sb.append("llm:" + mLLMName + ", ");
        sb.append("loc:" + mLocation + ", ");
        sb.append("say:" + mSay + ", ");
        sb.append("lang:" + mLang + ", ");
        sb.append("queryEnhanceType" + mQueryEnhanceType + ", ");
        sb.append("preQuery" + mPreQuery + ", ");
        sb.append("postQuery" + mPostQuery + ", ");


        return sb.toString();
    }
}
