package com.dahe.gldriver.temp

import android.app.Activity
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Build
import android.util.Log
import android.view.View
import android.widget.Toast
import com.dahe.gldriver.R
import com.dahe.gldriver.event.RefreshEvent
import com.umeng.umverify.UMResultCode
import com.umeng.umverify.UMVerifyHelper
import com.umeng.umverify.view.UMAuthRegisterViewConfig
import com.umeng.umverify.view.UMAuthUIConfig
import org.greenrobot.eventbus.EventBus
import org.json.JSONException
import org.json.JSONObject

class FullPortConfig(activity: Activity?, authHelper: UMVerifyHelper?) :
    BaseUIConfig(activity!!, authHelper!!) {
    private val TAG = "全屏竖屏样式"
    override fun configAuthPage() {
        mAuthHelper.setUIClickListener { code, context, jsonString ->
            try {
                val jsonObj = JSONObject(jsonString)
                when (code) {
                    UMResultCode.CODE_ERROR_USER_CANCEL -> {
                        Log.e(TAG, "点击了授权页默认返回按钮")
                        mAuthHelper.quitLoginPage()
                        mActivity.finish()
                    }

                    UMResultCode.CODE_ERROR_USER_SWITCH -> Log.e(
                        TAG,
                        "点击了授权页默认切换其他登录方式"
                    )

                    UMResultCode.CODE_ERROR_USER_LOGIN_BTN -> if (!jsonObj.getBoolean("isChecked")) {
                        Toast.makeText(mContext, "同意服务条款才可以登录", Toast.LENGTH_SHORT)
                            .show()
                    }

                    UMResultCode.CODE_ERROR_USER_CHECKBOX -> Log.e(
                        TAG,
                        "checkbox状态变为" + jsonObj.getBoolean("isChecked")
                    )

                    UMResultCode.CODE_ERROR_USER_PROTOCOL_CONTROL -> Log.e(
                        TAG,
                        "点击协议，" + "name: " + jsonObj.getString("name") + ", url: " + jsonObj.getString(
                            "url"
                        )
                    )

                    else -> {}
                }
            } catch (e: JSONException) {
            }
        }
        mAuthHelper.removeAuthRegisterXmlConfig()
        mAuthHelper.removeAuthRegisterViewConfig()
        //        //添加自定义切换其他登录方式
        mAuthHelper.addAuthRegistViewConfig("switch_msg", UMAuthRegisterViewConfig.Builder()
            .setView(initSwitchView(350))
            .setRootViewId(UMAuthRegisterViewConfig.RootViewId.ROOT_VIEW_ID_BODY)
            .setCustomInterface { //                        Toast.makeText(mContext, "切换到短信登录方式", Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(RefreshEvent(1))
                mAuthHelper.quitLoginPage()
            }.build()
        )
        var authPageOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
        if (Build.VERSION.SDK_INT == 26) {
            authPageOrientation = ActivityInfo.SCREEN_ORIENTATION_BEHIND
        }
        val builder = UMAuthUIConfig.Builder()
            .setAppPrivacyOne("《自定义隐私协议》", "https://test.h5.app.tbmao.com/user")
            .setAppPrivacyTwo("《百度》", "https://www.baidu.com")
            .setAppPrivacyColor(Color.GRAY, Color.parseColor("#002E00")) //隐藏默认切换其他登录方式
            .setSwitchAccHidden(true) //隐藏默认Toast
            .setLogBtnToastHidden(true)
            .setNavHidden(true)
            .setLogBtnBackgroundDrawable(mContext.getDrawable(R.drawable.bg_btn)) //沉浸式状态栏
            .setStatusBarColor(Color.TRANSPARENT)
            .setStatusBarUIFlag(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
            .setLightColor(true)
            .setWebNavTextSizeDp(20) //图片或者xml的传参方式为不包含后缀名的全称 需要文件需要放在drawable或drawable-xxx目录下 in_activity.xml, mytel_app_launcher.png
            .setAuthPageActIn("in_activity", "out_activity")
            .setAuthPageActOut("in_activity", "out_activity")
            .setVendorPrivacyPrefix("《")
            .setVendorPrivacySuffix("》")
            .setPageBackgroundPath("page_background_color") //                .setLogoImgPath("mytel_app_launcher")
            //一键登录按钮三种状态背景示例login_btn_bg.xml
            .setLogBtnBackgroundPath("login_btn_bg")
            .setScreenOrientation(authPageOrientation)
        val res = mContext.resources
        val logoDrawable = res.getDrawable(R.drawable.diver_logo)
        builder.setLogoImgDrawable(logoDrawable)
        mAuthHelper.setAuthUIConfig(builder.create())
    }
}
