package com.mogo.cloud.passport.third;

import android.content.Context;
import android.util.Log;

import com.mogo.cloud.passport.IPassport;
import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.passport.MoGoAiCloudClientConfig;
import com.zhidao.thirdlogin.Environment;
import com.zhidao.thirdlogin.LoginManager;
import com.zhidao.thirdlogin.model.ThirdLoginParam;
import com.zhidao.thirdlogin.model.TokenData;
import com.zhidao.thirdlogin.network.LoginCallback;

import static com.mogo.cloud.passport.MoGoAiCloudClient.TAG;
import static com.mogo.cloud.passport.MoGoAiCloudClientConfig.HTTP_DNS_ENV_DEV;
import static com.mogo.cloud.passport.MoGoAiCloudClientConfig.HTTP_DNS_ENV_QA;

public class ThirdPassportManager {

    private static volatile ThirdPassportManager mInstance;
    private IPassport mPassport;

    private ThirdPassportManager() {

    }

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

    public void refreshToken(Context context, IPassport passport) {
        this.mPassport = passport;
        MoGoAiCloudClientConfig mAiCloudClientConfig = MoGoAiCloudClient.getInstance().getAiCloudClientConfig();
        if (mAiCloudClientConfig != null) {
            ThirdLoginParam thirdLoginParam = ThirdLoginParam.of(
                    mAiCloudClientConfig.getThirdPartyDeviceId(),
                    mAiCloudClientConfig.getThirdPartyAppKey()
            );

            LoginCallback loginCallback = new LoginCallback() {
                @Override
                public void onSuccess(TokenData.TokenResult result) {
                    if (mPassport != null) {
                        mPassport.onSuccess(result.token, result.sn);
                    }
                }

                @Override
                public void onFailure(int code, String msg) {
                    if (mPassport != null) {
                        mPassport.onError(code, msg);
                    }
                }
            };

            if (mAiCloudClientConfig.isShowDebugLog()) {
                Log.w(TAG, "loginCallback：" + loginCallback);
            }

            /**
             * 演示环境(SHOW环境)和线上环境(RELEASE)均走 Environment.release
             * @return {@link Environment}
             */
            Environment environment;
            switch (mAiCloudClientConfig.getNetMode()) {
                case HTTP_DNS_ENV_DEV:
                    environment = Environment.dev;
                    break;
                case HTTP_DNS_ENV_QA:
                    environment = Environment.qa;
                    break;
                default:
                    environment = Environment.release;
                    break;
            }
            LoginManager.getInstance().registerTokenRefresh(result -> {
                if (mPassport != null) {
                    mPassport.onSuccess(result.token, result.sn);
                }
            });
            LoginManager.getInstance().login(
                    context,
                    mAiCloudClientConfig.getPassportUrl(),
                    environment,
                    thirdLoginParam,
                    loginCallback);
        } else {
            Log.e(TAG, "═════════════════════════════════════");
            Log.e(TAG, "║ MoGo 鉴权失败 ");
            Log.e(TAG, "║ 请配置 MoGoAiCloudClientConfig 信息");
            Log.e(TAG, "═════════════════════════════════════");
        }
    }
}
