package com.dahe.gldriver.utils

import android.content.Context
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.dahe.gldriver.event.RefreshEvent
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.temp.AuthPageConfig
import com.dahe.gldriver.temp.CustomXmlConfig
import com.dahe.gldriver.temp.DialogPortConfig
import com.dahe.gldriver.temp.FullPortConfig
import com.dahe.gldriver.ui.HomeActivity
import com.dahe.gldriver.ui.account.LoginActivity
import com.dahe.gldriver.ui.account.SelectRoleActivity
import com.dahe.glex.bean.UserBean
import com.dahe.mylibrary.base.SingletonNoPHolder
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.google.gson.Gson
import com.umeng.umverify.UMResultCode
import com.umeng.umverify.UMVerifyHelper
import com.umeng.umverify.listener.UMTokenResultListener
import com.umeng.umverify.model.UMTokenRet
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import org.greenrobot.eventbus.EventBus

/**
 * @ClassName OneKeyLoginUtils
 * @Author john
 * @Date 2024/3/14 09:49
 * @Description TODO
 */
class OneKeyLoginUtils private constructor() {
    companion object : SingletonNoPHolder<OneKeyLoginUtils>(::OneKeyLoginUtils)


    lateinit var mPhoneNumberAuthHelper: UMVerifyHelper
    lateinit var mUIConfig: AuthPageConfig
    fun oneLogin(context: Context, activity: AppCompatActivity) {

        //一键登录环境判断
        mPhoneNumberAuthHelper =
            UMVerifyHelper.getInstance(context, object : UMTokenResultListener {
                override fun onTokenSuccess(p0: String?) {
                    mPhoneNumberAuthHelper.setAuthListener(object : UMTokenResultListener {
                        override fun onTokenSuccess(p0: String?) {
                            var tokenRet: UMTokenRet? = null
                            try {
                                mUIConfig.release()
                                tokenRet = UMTokenRet.fromJson(p0)
                                if (UMResultCode.CODE_GET_TOKEN_SUCCESS == tokenRet.code) {
                                    DataManager.getInstance().oneClickLogin(tokenRet.token)
                                        .flatMap {
                                            //两个接口token字段名称不一致，先进行保存
                                            SPUtils.instance.setUserToken(context,it.data.access_token)
                                            DataManager.getInstance().getUserInfo()
                                        }
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(BaseObserver(context,
                                            object : RxHttpCallBack<UserBean>() {
                                                override fun onSuccess(t: CommonResponseBean<UserBean>) {
                                                    super.onSuccess(t)
                                                    mPhoneNumberAuthHelper.quitLoginPage()

                                                    UserUtils.instance.loginIn(context,t.data)

                                                }
                                            }))
                                }
                            } catch (e: Exception) {
                                e.printStackTrace();
                            }
                        }

                        override fun onTokenFailed(p0: String?) {
                            try {
                                mUIConfig.release()
                                var tokenRet = UMTokenRet.fromJson(p0)
                                if (UMResultCode.CODE_ERROR_USER_CANCEL == tokenRet.getCode()) {
                                    //模拟的是必须登录 否则直接退出app的场景
//                                finish()
                                } else {
                                    Toast.makeText(
                                        context,
                                        "一键登录失败切换到其他登录方式",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                    //获取token失败，去获取验证码页面
                                    EventBus.getDefault().post(RefreshEvent(1))
                                }
                            } catch (e: java.lang.Exception) {
                                e.printStackTrace()
                            }
                        }
                    })
                    mUIConfig!!.configAuthPage()
                    mPhoneNumberAuthHelper?.getLoginToken(context, 5000)
                }

                override fun onTokenFailed(p0: String?) {
                    try {
                        var tokenRet = UMTokenRet.fromJson(p0)
                        if (UMResultCode.CODE_ERROR_USER_CANCEL == tokenRet.getCode()) {
                            //模拟的是必须登录 否则直接退出app的场景
//                                finish()
                        } else {
                            Toast.makeText(
                                context,
                                "一键登录失败切换到其他登录方式",
                                Toast.LENGTH_SHORT
                            ).show()
                            //获取token失败，去获取验证码页面
                            EventBus.getDefault().post(RefreshEvent(1))
                        }
                    } catch (e: java.lang.Exception) {
                        e.printStackTrace()
                    }
                }
            }).apply {
                checkEnvAvailable(UMVerifyHelper.SERVICE_TYPE_LOGIN)
            }

        mUIConfig = FullPortConfig(activity, mPhoneNumberAuthHelper)
//        mUIConfig = DialogPortConfig(activity, mPhoneNumberAuthHelper)
    }

    private open class CheckListener(context: Context) : UMTokenResultListener {
        var context: Context

        init {
            this.context = context
        }

        override fun onTokenSuccess(p0: String?) {
            TODO("Not yet implemented")
        }

        override fun onTokenFailed(p0: String?) {
            try {
                var tokenRet = UMTokenRet.fromJson(p0)
                if (UMResultCode.CODE_ERROR_USER_CANCEL == tokenRet.getCode()) {
                    //模拟的是必须登录 否则直接退出app的场景
//                                finish()
                } else {
                    Toast.makeText(
                        context,
                        "一键登录失败切换到其他登录方式",
                        Toast.LENGTH_SHORT
                    ).show()
                    //获取token失败，去获取验证码页面
                    EventBus.getDefault().post(RefreshEvent(1))
                }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }
    }
}