package com.ccwangluo.accelerator.utils

import android.app.Application
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.Log
import android.util.TypedValue
import android.widget.RelativeLayout
import android.widget.TextView
import com.ccwangluo.accelerator.R
import com.ccwangluo.accelerator.ui.login.PhoneLoginFragment

import com.chuanglan.shanyan_sdk.OneKeyLoginManager
import com.chuanglan.shanyan_sdk.tool.ShanYanUIConfig
import com.xuexiang.xpage.base.XPageFragment
import com.xuexiang.xpage.core.PageOption
import org.json.JSONObject





object PhoneUtils {

    private val TAG: String = PhoneUtils.javaClass.name

    fun initSdk(application: Application, callback: (Boolean) -> Unit = {}) {
        val appid = "tqXxGGCH";
        OneKeyLoginManager.getInstance().init(
            application, appid
        ) { code, result ->
            if (code == 1022) {
                callback(true)
            } else {
                callback(false)
            }
        }
    }

    fun getPhoneInfo(callback: (Boolean, String) -> Unit = { t1, t2 -> {} }) {
        OneKeyLoginManager.getInstance().getPhoneInfo { code, result ->
            if (code == 1022) {
                callback(true, result)
            } else {
                callback(false, "")
            }
        }
    }



    fun openLoginAuth(xPageFragment: XPageFragment, callback: (Boolean, String) -> Unit) {
        xPageFragment.context?.let {
            OneKeyLoginManager.getInstance().setAuthThemeConfig(getEConfig(it, xPageFragment))

            OneKeyLoginManager.getInstance().openLoginAuth(false,
                { code, result ->
                    if (code != 1000) {
                        callback(false, "")
                    }
                    Log.i(TAG, "openLoginAuth: $result")
                }
            ) { code, result ->
                if (code == 1000) {
                    val json = JSONObject(result)
                    callback(true, json.getString("token"))
                }
            }
        }
    }

    fun getEConfig(context: Context, xPageFragment: XPageFragment): ShanYanUIConfig? {
        /************************************SDK固有控件 */

        //登录按钮背景
//        val mLoginBt: Drawable =
//            context.getResources().getDrawable(R.drawable.login_demo_auth_bt_cus_e)
        //协议复选框选中时图标
        val mCheckedImg: Drawable =
            context.getResources().getDrawable(R.mipmap.login_check_cus)
        //协议复选框未选中时图标
        val mCunheckedImg: Drawable =
            context.getResources().getDrawable(R.mipmap.login_uncheck_cus)
        //自定义控件（右上角“使用其他手机>”）
        val mOtherTv = TextView(context)
        mOtherTv.text = "其他方式登录"
        mOtherTv.setTextColor(Color.parseColor("#FF408DFF"))
        mOtherTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
        val otherLayoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        val screenHeight = AbScreenUtils.getScreenHeight(context, false)
        val mOtherTvTop = AbScreenUtils.dp2px(context,  100f)
        otherLayoutParams.setMargins(0, screenHeight - mOtherTvTop, 0, 0)
        otherLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
        mOtherTv.layoutParams = otherLayoutParams

//        val mOtherTv2 = TextView(context)
//        mOtherTv2.text = "新用户尊享3天免费VIP"
//        mOtherTv2.setTextColor(Color.parseColor("#FFD0964E"))
//        mOtherTv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
//        val otherLayoutParams2 = RelativeLayout.LayoutParams(
//            RelativeLayout.LayoutParams.WRAP_CONTENT,
//            RelativeLayout.LayoutParams.WRAP_CONTENT
//        )
//        otherLayoutParams2.setMargins(0, AbScreenUtils.dp2px(context, 350f), 0, 0)
//        otherLayoutParams2.addRule(RelativeLayout.CENTER_HORIZONTAL)
//        mOtherTv.layoutParams = otherLayoutParams2


        return ShanYanUIConfig.Builder()
            /**设置弹窗样式
             * @param isdialogTheme 是否是弹窗主题
             * @param dialogWidth 弹窗宽度
             * @param dialogHeight 弹窗高度
             * @param dialogX 弹窗距离屏幕左测边距（左右等距）
             * @param dialogY 弹窗距离屏幕顶部测边距
             * @param isDialogBottom 弹窗是否显示到屏幕底部
             */
            .setNavText("")
            .setSloganHidden(true)
            //导航栏标题设置成空
            .setLogoHeight(130)
            .setLogoWidth(130)
            .setLogoOffsetY(30)
            .setLogoImgPath(context.resources.getDrawable(R.mipmap.ic_launcher))
//            .addCustomView(
//                mOtherTv2, true, false, null
//            )
            .setNumFieldOffsetBottomY(330)
            .setNumberSize(25)
            .setLogBtnOffsetBottomY(230)
            .setLogBtnWidth(AbScreenUtils.getScreenWidth(context, true) - 60)
            .setLogBtnText("登录")
            .setLogBtnTextSize(16)
            .setLogBtnImgPath(context.resources.getDrawable(R.mipmap.login_btn_onekey))
            .addCustomView(
                mOtherTv, true, false
            ) { context, view -> PageOption.to(PhoneLoginFragment::class.java).open(xPageFragment) }
            .setPrivacyState(false)
            .setCheckedImgPath(mCheckedImg)
            .setUncheckedImgPath(mCunheckedImg)
            .setCheckBoxWH(20, 20)
            .setPrivacyOffsetX(20)
            .setcheckBoxOffsetXY(0, 5)
            .setPrivacyOffsetBottomY(174)
            .setPrivacyOffsetGravityLeft(true)
            .setPrivacyTextSize(12)
            .setOperatorPrivacyAtLast(true)
            .setPrivacySmhHidden(true)
            .setAppPrivacyColor(
                Color.parseColor("#919095"),
                Color.parseColor("#FF408DFF")
            )
            .setAppPrivacyOne(
                "《用户协议》",
                "https://api.253.com/api_doc/yin-si-zheng-ce/wei-hu-wang-luo-an-quan-sheng-ming.html"
            )
            .setAppPrivacyTwo(
                "《隐私政策》",
                "https://api.253.com/api_doc/yin-si-zheng-ce/ge-ren-xin-xi-bao-hu-sheng-ming.html"
            ) //设置隐私条款2名称和URL(名称，url)
            //设置协议外部文字描述
            .setPrivacyText("你已阅读并同意", "、", "和", "", "")
            .build()
        /**************************授权页配置（示例配置自上而下，可调整顺序） */
    }


}