package com.datarecovery.master.sdk.quicklogin;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;

import com.atmob.common.logging.AtmobLog;
import com.atmob.common.runtime.ContextUtil;
import com.datarecovery.master.BuildConfig;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.Constants;
import com.netease.nis.quicklogin.QuickLogin;
import com.netease.nis.quicklogin.helper.UnifyUiConfig;
import com.netease.nis.quicklogin.listener.QuickLoginPreMobileListener;
import com.netease.nis.quicklogin.listener.QuickLoginTokenListener;

public class QuickLoginHelper {

    private static final String TAG = "QuickLoginHelper";

    private static final String BUSINESS_ID = BuildConfig.QUICK_LOGIN_ID;
    private static final String USER_AGREEMENT = Constants.USER_AGREEMENT;
    private static final String PRIVACY_POLICY = Constants.PRIVACY_POLICY;

    private QuickLoginHelper() {
    }


    public static void init(Context context) {
        QuickLogin quickLogin = QuickLogin.getInstance();
        quickLogin.init(context, BUSINESS_ID);
    }


    @SuppressLint("UseCompatLoadingForDrawables")
    private static UnifyUiConfig getUnifyUiConfig() {
        return new UnifyUiConfig.Builder()
                .setStatusBarDarkColor(true)
                .setHideNavigation(false)
                .setHideNavigationBackIcon(false)
                .setNavigationIconDrawable(ContextUtil.getContext().getDrawable(R.drawable.icon_back))
                .setNavigationBackIconWidth(24)
                .setNavigationBackIconHeight(24)
                .setNavigationHeight(56)
                .setLogoIconDrawable(ContextUtil.getContext().getDrawable(R.mipmap.ic_launcher))
                .setLogoWidth(65)
                .setLogoHeight(65)
                .setLogoTopYOffset(50)
                .setNavigationTitle(" ")
                .setLoginBtnText("本机号码一键登录")
                .setLoginBtnTextColor(Color.parseColor("#FFFFFF"))
                .setLoginBtnTextSize(16)
                .setLoginBtnBackgroundDrawable(ContextUtil.getContext().getDrawable(R.drawable.bg_common_btn))
                .setCheckedImageDrawable(ContextUtil.getContext().getDrawable(R.drawable.icon_login_check_box_checked))
                .setUnCheckedImageDrawable(ContextUtil.getContext().getDrawable(R.drawable.icon_login_check_box_unchecked))
                .setPrivacyCheckBoxWidth(24)
                .setPrivacyCheckBoxHeight(24)
                .setPrivacyState(false)
                .setPrivacyTextStart("我已阅读并同意")
                .setProtocolText("《用户协议》")
                .setProtocolLink(USER_AGREEMENT)
                .setProtocol2Text("《隐私政策》")
                .setProtocol2Link(PRIVACY_POLICY)
                .setPrivacyTextColor(Color.parseColor("#121313"))
                .setPrivacyProtocolColor(ContextUtil.getContext().getResources().getColor(R.color.colorPrimary))
                .setPrivacyMarginLeft(12)
                .setPrivacyMarginRight(12)
                .setPrivacyBottomYOffset(20)
                .setPrivacyDialogAuto(true)
                .build(ContextUtil.getContext());
    }


    public static void openQuickLoginActivity(QuickLoginListener listener) {
        if (listener == null) {
            return;
        }
        QuickLogin quickLogin = QuickLogin.getInstance();
        quickLogin.prefetchMobileNumber(new QuickLoginPreMobileListener() {
            @Override
            public void onGetMobileNumberSuccess(String s, String s1) {
                AtmobLog.d(TAG, "onGetMobileNumberSuccess: " + s + ", " + s1);
                quickLogin.setUnifyUiConfig(getUnifyUiConfig());
                listener.onOpenQuickLoginActivity();
                quickLogin.onePass(new QuickLoginTokenListener() {
                    @Override
                    public void onGetTokenSuccess(String YDToken, String accessCode) {
                        quickLogin.quitActivity();
                        listener.onGetTokenSuccess(YDToken, accessCode);
                    }

                    @Override
                    public void onGetTokenError(String YDToken, int code, String msg) {
                        AtmobLog.e(TAG, "onGetTokenError: " + code + ", " + msg);
                        quickLogin.quitActivity();
                        listener.onGetTokenError(msg);
                    }

                    @Override
                    public void onCancelGetToken() {
                        quickLogin.quitActivity();
                    }
                });
            }

            @Override
            public void onGetMobileNumberError(String YDToken, String msg) {
                AtmobLog.e(TAG, "onGetMobileNumberError: " + msg);
            }
        });
    }

    public interface QuickLoginListener {

        void onGetTokenSuccess(String YDToken, String accessToken);

        void onGetTokenError(String msg);

        void onOpenQuickLoginActivity();
    }

}
