package com.mogo.cloud.passport;

import android.text.TextUtils;
import android.util.Log;

import com.elegant.log.simplelog.LogLevel;
import com.mogo.cloud.passport.location.ICurrentLocation;

/**
 * SDK 中的参数
 */
public class MoGoAiCloudClientConfig {
    private static final String TAG = "MoGoAiCloudClientConfig";

    private MoGoAiCloudClientConfig() {
    }

    private static final class MMoGoAiCloudClientConfigHolder {
        static final MoGoAiCloudClientConfig mMoGoAiCloudClientConfig = new MoGoAiCloudClientConfig();
    }

    public static MoGoAiCloudClientConfig getInstance() {
        return MMoGoAiCloudClientConfigHolder.mMoGoAiCloudClientConfig;
    }

    /**
     * 研发环境
     */
    public static final int HTTP_DNS_ENV_DEV = 1;

    /**
     * 测试环境
     */
    public static final int HTTP_DNS_ENV_QA = 2;

    /**
     * 线上环境
     */
    public static final int HTTP_DNS_ENV_RELEASE = 3;

    /**
     * 演示环境
     */
    public static final int HTTP_DNS_ENV_DEMO = 4;

    /**
     * 网络模式
     */
    private int sNetMode = HTTP_DNS_ENV_RELEASE;

    /**
     * 设备ID
     */
    private String thirdPartyDeviceId = "";
    /**
     * passport APP key
     */
    private String thirdPartyAppKey = "";
    /**
     * （可选，有则传，有默认值；为后续扩展使用，第三方车机可能使用自定义的pub_key, 此key由server端分配）
     */
    private String authPubKey = "";

    // 必传, Socket长链接时做校验
    private String secretKey = "";

    /**
     * 应用安全校验值，用于header
     */
    private String securityKey = "";

    /**
     * 签名信息
     */
    private String thirdPartySignSecret = "";

    /**
     * 是否是第三方登录
     */
    private boolean thirdLogin;

    /**
     * passport url
     */
    private String passportUrl;

    /**
     * socket base url
     */
    private String socketBaseUrl;

    /**
     * socket tech url
     */
    private String socketTechUrl;

    /**
     * sdk中默认120秒，此处不设default=120
     */
    private long heartBeatInterval;

    /**
     * 是否打印日志
     */
    private boolean isShowDebugLog = false;

    /**
     * 是否打印网络日志
     */
    private boolean isShowNetDebugLog = false;

    /**
     * 是否Toast日志
     */
    private boolean isShowToastLog = true;

    /**
     * 因为ZeGo的房间限制，这里需要区分出，SDK提供给的是观众还是主播
     * true-主播，false-观众
     */
    private boolean isAnchor = true;

    /**
     * 服务器分配给应用的令牌，用于网络请求校验
     */
    private volatile String token = "";

    /**
     * 服务器根据设备ID 分配的SN，用于网络请求校验
     */
    private volatile String sn = "";

    /**
     * 是否为高精定位设备
     */
    private boolean mIsAccuracyDevice;

    /**
     * 应用服务AppId,包括长链、鉴权
     */
    private String mServiceAppId;

    /**
     * 是否使用旧长链接链路
     */
    private boolean isUseOriginSocket = false;

    /**
     * 设置位置监听
     */
    private ICurrentLocation mIHttpCurrentLocation;

    /**
     * 获取三方设备唯一ID
     *
     * @return 三方设备唯一ID
     */
    public String getThirdPartyDeviceId() {
        return thirdPartyDeviceId;
    }

    /**
     * 设置三方设备唯一ID
     *
     * @param thirdPartyDeviceId 三方设备唯一ID
     */
    public void setThirdPartyDeviceId(String thirdPartyDeviceId) {
        this.thirdPartyDeviceId = thirdPartyDeviceId;
    }

    /**
     * 获取AI云平台分配给三方应用的AppKey，需要从AI云平台申请
     *
     * @return AppKey
     */
    public String getThirdPartyAppKey() {
        return thirdPartyAppKey;
    }

    /**
     * 设置AI云平台分配给三方应用的AppKey，需要从AI云平台申请
     *
     * @param thirdPartyAppKey AppKey
     */
    public void setThirdPartyAppKey(String thirdPartyAppKey) {
        this.thirdPartyAppKey = thirdPartyAppKey;
    }

    /**
     * 获取授权公钥匙
     *
     * @return 授权公钥匙
     */
    public String getAuthPubKey() {
        return authPubKey;
    }

    /**
     * 设置授权公钥匙
     *
     * @param authPubKey 授权公钥匙
     */
    public void setAuthPubKey(String authPubKey) {
        this.authPubKey = authPubKey;
    }


    /**
     * 获取长链接公钥匙
     *
     * @return 授权公钥匙
     */
    public String getSecretKey() {
        return secretKey;
    }

    /**
     * 设置长链接公钥匙
     *
     * @param secretKey 授权公钥匙
     */
    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getSecurityKey() {
        return securityKey;
    }

    public void setSecurityKey(String securityKey) {
        this.securityKey = securityKey;
    }

    /**
     * 获取AI云平台分配给三方应用的签名密钥，需要从AI云平台申请
     *
     * @return 签名密钥
     */
    public String getThirdPartySignSecret() {
        return thirdPartySignSecret;
    }

    /**
     * 设置AI云平台分配给三方应用的签名密钥，需要从AI云平台申请
     *
     * @param thirdPartySignSecret 签名密钥
     */
    public void setThirdPartySignSecret(String thirdPartySignSecret) {
        this.thirdPartySignSecret = thirdPartySignSecret;
    }

    /**
     * 获取是否是第三放应用登录
     *
     * @return true-是，false-否
     */
    public boolean isThirdLogin() {
        return thirdLogin;
    }

    /**
     * 设置是否是第三方应用登录
     *
     * @param thirdLogin true-是，false-否
     */
    public void setThirdLogin(boolean thirdLogin) {
        this.thirdLogin = thirdLogin;
    }

    public String getPassportUrl() {
        return passportUrl;
    }

    public void setPassportUrl(String passportUrl) {
        this.passportUrl = passportUrl;
    }

    public String getSocketBaseUrl() {
        return socketBaseUrl;
    }

    public void setSocketBaseUrl(String socketBaseUrl) {
        this.socketBaseUrl = socketBaseUrl;
    }

    public String getSocketTechUrl() {
        return socketTechUrl;
    }

    public void setSocketTechUrl(String socketTechUrl) {
        this.socketTechUrl = socketTechUrl;
    }

    public long getHeartBeatInterval() {
        return heartBeatInterval;
    }

    public void setHeartBeatInterval(long heartBeatInterval) {
        this.heartBeatInterval = heartBeatInterval;
    }

    /**
     * 获取当前网络环境
     *
     * @return 当前的网络环境是那个
     * @see #HTTP_DNS_ENV_DEV 研发环境
     * @see #HTTP_DNS_ENV_QA 测试环境
     * @see #HTTP_DNS_ENV_RELEASE  线上环境
     * @see #HTTP_DNS_ENV_DEMO 演示环境
     */
    public int getNetMode() {
        return sNetMode;
    }

    /**
     * 设置当前网络环境
     *
     * @param sNetMode 当前的网络环境
     * @see #HTTP_DNS_ENV_DEV 研发环境
     * @see #HTTP_DNS_ENV_QA 测试环境
     * @see #HTTP_DNS_ENV_RELEASE  线上环境
     * @see #HTTP_DNS_ENV_DEMO 演示环境
     */
    public void setNetMode(int sNetMode) {
        this.sNetMode = sNetMode;
    }

    /**
     * 获取从AI云平台获取的Token
     *
     * @return Token信息
     */
    public String getToken() {
        if (token != null && !token.isEmpty()) {
            return token;
        }
        if (TextUtils.isEmpty(SpStorage.getToken())) {
            Log.e(TAG, "本地 Token 获取失败……");
        }
        return SpStorage.getToken();
    }

    /**
     * 获取从AI云平台获取的SN
     *
     * @return SN信息
     */
    public String getSn() {
        if (sn != null && !sn.isEmpty()) {
            return sn;
        }
        if (TextUtils.isEmpty(SpStorage.getSn())) {
            Log.e(TAG, "本地 SN 获取失败……");
        }
        return SpStorage.getSn();
    }

    /**
     * 设置Token
     *
     * @param token 平台分配的token信息
     */
    public void setToken(String token) {
        this.token = token;
        // 存储本地
        SpStorage.setToken(token);
    }

    /**
     * 设置SN
     *
     * @param sn 平台分配的sn信息
     */
    public void setSn(String sn) {
        this.sn = sn;
        // 存储本地
        SpStorage.setSn(sn);
    }

    /**
     * 是否打印日志
     *
     * @return true-打印日志，false-不打印日志
     */
    public boolean isShowDebugLog() {
        return isShowDebugLog;
    }

    /**
     * 是否打印网络日志
     *
     * @return true-打印日志，false-不打印日志
     */
    public boolean isShowNetDebugLog() {
        return isShowNetDebugLog;
    }

    /**
     * 设置是否打印日志
     *
     * @param showDebugLog true-打印日志，false-不打印日志
     */
    public void setShowDebugLog(boolean showDebugLog) {
        isShowDebugLog = showDebugLog;
        if (showDebugLog) {
            // 打开鉴权模块的日志
            com.zhidao.account.sdk.utils.Logger.init(true);
            // 打开所有SDK中的日志
            com.elegant.log.simplelog.Logger.init(LogLevel.DEBUG);
        } else {
            // 关闭鉴权模块的日志
            com.zhidao.account.sdk.utils.Logger.init(false);
            // 关闭所有SDK中的日志
            com.elegant.log.simplelog.Logger.init(LogLevel.OFF);
        }
    }

    /**
     * 设置是否打印网络日志
     *
     * @param showNetDebugLog true-打印日志，false-不打印日志
     */
    public void setShowNetDebugLog(boolean showNetDebugLog) {
        isShowNetDebugLog = showNetDebugLog;
    }

    /**
     * 是否打Toast日志
     *
     * @return true-打印日志，false-不打印日志
     */
    public boolean isShowToastLog() {
        return isShowToastLog;
    }


    /**
     * 设置是否Toast日志
     *
     * @param showToastLog true-打印日志，false-不打印日志
     */
    public void setShowDebugToast(boolean showToastLog) {
        isShowToastLog = showToastLog;
    }

    /**
     * 获取是否为高精定位设备
     *
     * @return true-使用，false-使用sdk的定位数据
     */
    public boolean getIsAccuracyDevice() {
        return mIsAccuracyDevice;
    }

    /**
     * 设置是否为高精定位设备
     *
     * @param isAccuracyDevice true-属于高精定位设备，false-不属于高精定位设备
     */
    public void setIsAccuracyDevice(boolean isAccuracyDevice) {
        mIsAccuracyDevice = isAccuracyDevice;
    }

    /**
     * 获取应用服务AppId
     *
     * @return String AppId
     */
    public String getServiceAppId() {
        return mServiceAppId;
    }

    /**
     * 设置应用服务AppId,包括长链、鉴权,三方应用无需设置
     *
     * @param serviceAppId 设置应用服务AppId
     */
    public void setServiceAppId(String serviceAppId) {
        mServiceAppId = serviceAppId;
    }

    /**
     * @return true-主播，false-观众
     */
    public boolean isAnchor() {
        return isAnchor;
    }

    /**
     * 设置是否是主播
     *
     * @param anchor true-主播，false-观众
     */
    public void setAnchor(boolean anchor) {
        isAnchor = anchor;
    }

    /**
     * 返回位置信息，必须设置，否则将无法使用网络请求
     *
     * @return 返回位置信息的回调
     */
    public ICurrentLocation getIHttpCurrentLocation() {
        return mIHttpCurrentLocation;
    }

    /**
     * 返回位置信息，必须设置，否则将无法使用网络请求
     *
     * @param IHttpCurrentLocation 返回位置信息的回调
     */
    public void setIHttpCurrentLocation(ICurrentLocation IHttpCurrentLocation) {
        mIHttpCurrentLocation = IHttpCurrentLocation;
    }

    public boolean isUseOriginSocket() {
        return isUseOriginSocket;
    }

    public void setUseOriginSocket(boolean useOriginSocket) {
        isUseOriginSocket = useOriginSocket;
    }

    @Override
    public String toString() {
        return "MoGoAiCloudClientConfig{" +
                "sNetMode=" + sNetMode +
                ", thirdPartyDeviceId='" + thirdPartyDeviceId + '\'' +
                ", thirdPartyAppKey='" + thirdPartyAppKey + '\'' +
                ", authPubKey='" + authPubKey + '\'' +
                ", secretKey='" + secretKey + '\'' +
                ", securityKey='" + securityKey + '\'' +
                ", thirdPartySignSecret='" + thirdPartySignSecret + '\'' +
                ", thirdLogin=" + thirdLogin +
                ", passportUrl='" + passportUrl + '\'' +
                ", socketBaseUrl='" + socketBaseUrl + '\'' +
                ", socketTechUrl='" + socketTechUrl + '\'' +
                ", heartBeatInterval='" + heartBeatInterval + '\'' +
                ", isShowDebugLog=" + isShowDebugLog +
                ", isShowNetDebugLog=" + isShowNetDebugLog +
                ", isShowToastLog=" + isShowToastLog +
                ", isAnchor=" + isAnchor +
                ", token='" + token + '\'' +
                ", sn='" + sn + '\'' +
                ", mIsAccuracyDevice=" + mIsAccuracyDevice +
                ", mServiceAppId='" + mServiceAppId + '\'' +
                ", isUseOriginSocket=" + isUseOriginSocket +
                ", mIHttpCurrentLocation=" + mIHttpCurrentLocation +
                '}';
    }
}
