package cn.yyxx.commsdk.core.entity;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.yyxx.support.JsonUtils;

public class SdkLoginInfo {

    private static SdkLoginInfo mInstance;

    public String authentication = "",user_center= "",solo_email = "",login_token= "",rn_si= "",uname= "",channel_uid ="",token= "";
    public boolean isVefityRealName = false;
    private SdkLoginInfo() {

    }

    public static SdkLoginInfo getInstance() {
        if (mInstance == null) {
            synchronized (SdkLoginInfo.class) {
                if (mInstance == null) {
                    mInstance = new SdkLoginInfo();
                }
            }
        }
        return mInstance;
    }

    public void  reset(){
        authentication = "";
        user_center= "";
        solo_email = "";
        login_token= "";
        rn_si= "";
        uname= "";
        token= "";
        channel_uid = "";
        isVefityRealName = false;

    }


    public void initLoginInfo(String json){

        reset();

        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject links = jsonObject.optJSONObject("links");
            if (JsonUtils.hasJsonKey(links, "authentication")) {
                authentication = links.optString("authentication");
                if (TextUtils.isEmpty(authentication)){
                    isVefityRealName = true;
                }
            }
            if (JsonUtils.hasJsonKey(links, "user_center")) {
                user_center = links.optString("user_center");
            }
            if (JsonUtils.hasJsonKey(links, "solo_email")) {
                solo_email = links.optString("solo_email");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "login_token")) {
                login_token = jsonObject.optString("login_token");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "rn_si")) {
                rn_si = jsonObject.optString("rn_si");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "uname")) {
                uname = jsonObject.optString("uname");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "token")) {
                token = jsonObject.optString("token");
            }
            if (JsonUtils.hasJsonKey(jsonObject, "channel_uid")) {
                channel_uid = jsonObject.optString("channel_uid");
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    @Override
    public String toString() {
        return "SdkLoginInfo{" +
                "authentication='" + authentication + '\'' +
                ", user_center='" + user_center + '\'' +
                ", solo_email='" + solo_email + '\'' +
                ", login_token='" + login_token + '\'' +
                ", rn_si='" + rn_si + '\'' +
                ", uname='" + uname + '\'' +
                ", channel_uid='" + channel_uid + '\'' +
                ", token='" + token + '\'' +
                ", isVefityRealName=" + isVefityRealName +
                '}';
    }
}
