package com.jishisongfu.duobibi.auth

import android.content.Context
import android.util.Log
import cn.jiguang.verifysdk.api.JVerificationInterface
import cn.jiguang.verifysdk.api.JVerifyUIConfig
import com.jishisongfu.duobibi.BuildConfig
import com.jishisongfu.duobibi.util.PrivacyPrefs

/**
 * 极光一键登录 SDK 初始化统一入口。两个调用点:
 *   1) [com.jishisongfu.duobibi.DuobibiApplication.onCreate] — 已同意隐私的老用户冷启动
 *   2) [com.jishisongfu.duobibi.MainActivity] 隐私 onAgree 回调 — 首次安装同意
 *
 * 隐私 gate 跟推送/心跳一致。授权页 UI 自定义为多比比蓝青极简风。
 */
object JgInit {

    private const val TAG = "DUOBIBI-AUTH"

    // 多比比品牌色(蓝青)
    private const val TEAL = 0xFF0EA5A4.toInt()
    private const val INK = 0xFF1A1A1A.toInt()
    private const val GRAY = 0xFF666666.toInt()
    private const val BG = 0xFFFAFAFA.toInt()

    fun initIfReady(context: Context) {
        if (!PrivacyPrefs.agreed(context)) {
            Log.i(TAG, "JG.init skipped: privacy not agreed yet")
            return
        }
        if (JVerificationInterface.isInitSuccess()) {
            Log.i(TAG, "JG.init skipped: already initialized")
            applyUiConfig()
            return
        }
        JVerificationInterface.setDebugMode(BuildConfig.DEBUG)
        JVerificationInterface.init(context.applicationContext) { code, result ->
            Log.i(TAG, "JG.init code=$code result=$result")
            if (code == 8000) applyUiConfig()
        }
    }

    /**
     * 授权页 UI 自定义(蓝青极简风),跟 App 主题一致。
     *
     * **public 而非 private**:SDK 每次 loginAuth 完成/取消后会清空 UIConfig 缓存,
     * 所以 OneClickLogin 在每次 loginAuth 前都补一次 [applyUiConfig],保证 UI 不回退默认样式。
     */
    fun applyUiConfig() {
        val cfg = JVerifyUIConfig.Builder()
            .setAuthBGImgPath("jverify_bg")
            .setStatusBarColorWithNav(true)
            .setStatusBarDarkMode(true)
            .setVirtualButtonTransparent(true)
            .setNavColor(BG)
            .setNavText("")
            .setNavReturnImgPath("jverify_back")
            .setLogoImgPath("jverify_logo")
            .setLogoWidth(64)
            .setLogoHeight(64)
            .setLogoOffsetY(60)
            .setNumberColor(INK)
            .setNumberSize(28)
            .setNumFieldOffsetY(160)
            .setSloganTextColor(GRAY)
            .setSloganOffsetY(215)
            .setSloganTextSize(12)
            .setLogBtnText("一键绑定本机号码")
            .setLogBtnTextColor(0xFFFFFFFF.toInt())
            .setLogBtnImgPath("jverify_login_btn")
            .setLogBtnHeight(52)
            .setLogBtnTextSize(16)
            .setLogBtnOffsetY(275)
            .setAppPrivacyOne("多比比隐私政策", "https://duobibi.com/privacy/duobibi.html")
            .setAppPrivacyColor(GRAY, TEAL)
            .setPrivacyText("我已阅读并同意", "并授权本机号码用于注册和登录")
            .setPrivacyTextSize(11)
            .setPrivacyState(false)
            .setPrivacyCheckboxHidden(false)
            .setPrivacyCheckboxSize(22)
            .setPrivacyCheckboxInCenter(true)
            .setPrivacyCheckboxMargin(0, 0, 6, 0)
            .setPrivacyMarginB(24)
            .setPrivacyNavColor(BG)
            .setPrivacyNavTitleTextColor(INK)
            // 用户没勾 checkbox 直接点登录时弹二次确认对话框(替代 SDK 默认丑 toast)。
            // 注意 SDK 方法名大小写 bug:setPrivacyCheckDialoglogBtnTextColor 的 log 是小写 l。
            .enablePrivacyCheckDialog(true)
            .setPrivacyCheckDialogTitleText("温馨提示")
            .setPrivacyCheckDialogTitleTextColor(INK)
            .setPrivacyCheckDialogTitleTextSize(16)
            .setPrivacyCheckDialogContentTextSize(13)
            .setPrivacyCheckDialogContentTextGravity(android.view.Gravity.CENTER)
            .setPrivacyCheckDialogLogBtnText("同意并继续")
            .setPrivacyCheckDialogLogBtnImgPath("jverify_login_btn")
            .setPrivacyCheckDialoglogBtnTextColor(0xFFFFFFFF.toInt())
            .setPrivacyCheckDialogLogBtnHeight(44)
            .setPrivacyCheckDialogLogBtnMarginL(20)
            .setPrivacyCheckDialogLogBtnMarginR(20)
            .setPrivacyCheckDialogLogBtnMarginT(16)
            .setPrivacyCheckDialogLogBtnMarginB(20)
            .build()

        JVerificationInterface.setCustomUIWithConfig(cfg)
        Log.i(TAG, "JG UI config applied")
    }
}
