package com.videogo.ui.login;

import com.google.gson.Gson;
import com.videogo.global.DemoConfig;
import com.videogo.global.ValueKeys;
import com.videogo.util.SpTool;

public class SdkInitParams {

    public String appKey;
    // accessToken 或 tkToken
    public String token;
    public int serverAreaId;
    public String openApiServer;
    public String openAuthApiServer;
    public String specifiedDevice;
    public boolean usingGlobalSDK;

    private SdkInitParams(){}

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }

    public static SdkInitParams createBy(ServerAreasEnum serverArea){
        SdkInitParams sdkInitParams = new SdkInitParams();
        if (serverArea != null){
            sdkInitParams.appKey = serverArea.defaultOpenAuthAppKey;
            sdkInitParams.serverAreaId = serverArea.id;
            sdkInitParams.openApiServer = serverArea.openApiServer;
            sdkInitParams.openAuthApiServer = serverArea.openAuthApiServer;
            sdkInitParams.usingGlobalSDK = serverArea.usingGlobalSDK;
            sdkInitParams.specifiedDevice = "";
        }
        return sdkInitParams;
    }

    /**
     * 创建默认的 SDK 初始化参数
     */
    public static SdkInitParams createDefault() {
        SdkInitParams params = SdkInitParams.createBy(null);
        params.appKey = DemoConfig.APPKEY_DEV;
        params.openApiServer = "https://open.ys7.com";
        params.openAuthApiServer = "https://openauth.ys7.com";
        return params;
    }

    /**
     * 从本地存储加载上次的 SDK 初始化参数
     *
     * @return 上次保存的参数，如果不存在则返回 null
     */
    public static SdkInitParams loadLast() {
        String lastSdkInitParamsStr = SpTool.obtainValue(ValueKeys.SDK_INIT_PARAMS);
        if (lastSdkInitParamsStr == null) {
            return null;
        }
        return new Gson().fromJson(lastSdkInitParamsStr, SdkInitParams.class);
    }

}
