package com.zswc.jpush.componnent

import android.util.Log
import android.widget.Toast
import cn.jiguang.verifysdk.api.*
import cn.jpush.android.api.JPushInterface
import com.billy.cc.core.component.BuildConfig
import com.billy.cc.core.component.CC
import com.billy.cc.core.component.CCResult
import com.billy.cc.core.component.IComponent
import com.zswc.jpush.utils.Alias
import com.ysnows.base.ccextension.BIComponent
import com.ysnows.base.ccretrofit.CCRetrofit
import com.ysnows.base.route.IApp


/**
 *Created by shihuiyun
 *on 2020/9/9
 */
class CpJpush : BIComponent(), IComponent {


    fun init(cc: CC): Boolean {
        Log.d("jpush", "初始化一键登录")

//      JVerificationInterface.init(cc.getContext().getApplicationContext())
        JVerificationInterface.init(cc.getContext().getApplicationContext(),
                RequestCallback { code, msg ->
                    Log.d("shy", "code = $code msg = $msg")


                })

        JVerificationInterface.setDebugMode(BuildConfig.DEBUG)
        CC.sendCCResult(cc.callId, CCResult.success())

        return false
    }

    fun isLogin(cc: CC): Boolean {

        Log.d("jpush", "一键登录开始")

        val uiConfig = JVerifyUIConfig.Builder()
                .setAuthBGImgPath("main_bg")
                .setNavColor(-0x14208e)
                .setNavText("登录")
                .setNavTextColor(-0x1)
                .setNavReturnImgPath("umcsdk_return_bg")
                .setLogoWidth(80)
                .setLogoHeight(80)
                .setLogoHidden(false)
                .setNumberColor(-0xcccccd)
                .setLogBtnText("本机号码一键登录")
                .setLogBtnTextColor(-0x1)
                .setLogBtnHeight(50)
                .setLogBtnTextSize(18)
                .setLogBtnImgPath("umcsdk_login_btn_bg")
                .setAppPrivacyColor(-0x99999a, -0xff7a30)
                .setCheckedImgPath("umcsdk_check_image")
                .setUncheckedImgPath("umcsdk_uncheck_image")
                .setSloganTextColor(-0x666667) //                .setPrivacyCheckboxInCenter(true)
                //                .setPrivacyCheckboxHidden(true)
                .setSloganTextSize(13)
                .setLogoOffsetY(50)
                .setLogoImgPath("logo_cm")
                .setNumFieldOffsetY(190)
                .setSloganOffsetY(220)
                .setLogBtnOffsetY(254)
                .setNumberSize(22)
                .setPrivacyState(true)
                .setNavTransparent(false)
                .setPrivacyOffsetY(30).build()
        JVerificationInterface.setCustomUIWithConfig(uiConfig)

        //设置授权页事件监听
        val context = cc.context
        val settings = LoginSettings()
        settings.isAutoFinish = true //设置登录完成后是否自动关闭授权页
        settings.timeout = 15 * 1000 //设置超时时间，单位毫秒。 合法范围（0，30000],范围以外默认设置为10000
        settings.authPageEventListener = object : AuthPageEventListener() {
            override fun onEvent(cmd: Int, msg: String) {
                //do something...
            }
        }
        JVerificationInterface.loginAuth(context, settings) { code, content, operator ->
            if (code == 6000) {

                CCRetrofit.instance()
                        .create(IApp::class.java)
                        .setLogin(content)
                        .subscribe()
                Log.d("shy", "code=$code, token=$content ,operator=$operator")
            } else {
                Log.d("shy", "code=$code, message=$content")

                if (code === 2016) {

                    Toast.makeText(cc.context,"当前网络环境不支持认证",Toast.LENGTH_SHORT).show()

                    return@loginAuth
                }
                if (code === 2017) {

                    Toast.makeText(cc.context,"运营商配置错误",Toast.LENGTH_SHORT).show()

                    return@loginAuth
                }
                if (code === 2018) {

                    Toast.makeText(cc.context,"本地不支持的运营商",Toast.LENGTH_SHORT).show()

                    return@loginAuth
                }
                if (code === 3002) {

                    Toast.makeText(cc.context,"无效电话号码",Toast.LENGTH_SHORT).show()


                    return@loginAuth
                }
                if (code === 3003) {

                    Toast.makeText(cc.context,"前后两次请求少于设定时间",Toast.LENGTH_SHORT).show()

                    return@loginAuth
                }
                if (code === 6002) {
                    //Toasts.toast(getContext(), "用户取消一键登录");
                    return@loginAuth
                }
               // Toast.makeText(cc.context,content,Toast.LENGTH_SHORT).show()

                Toast.makeText(cc.context,"一键登录失败",Toast.LENGTH_SHORT).show()

            }
        }
        CC.sendCCResult(cc.callId, CCResult.success())
        return false
    }


    fun initPush(cc: CC): Boolean {//初始化极光推送


        JPushInterface.setDebugMode(true) //打印log
        JPushInterface.init(cc.context)

        CC.sendCCResult(cc.callId, CCResult.success())
        return false
    }

    fun setPushAlias(cc: CC, uid: String): Boolean {//别名推送
//        极光推送针对的是设备
//        设置的别名是跟设备的registrationID绑定

//        val rid = JPushInterface.getRegistrationID(cc.context)
//        Log.e("-shy-", "rid=: "+rid )
//        if (rid.isEmpty()) {
        Alias.alias = "XYBD_$uid"
        Alias.tag = uid.toInt()
        JPushInterface.resumePush(cc.context)
        JPushInterface.setAlias(cc.context, uid.toInt(), "XYBD_$uid")
        CC.sendCCResult(cc.callId, CCResult.success())
//        }
        return false
    }

    fun setClearAlias(cc: CC, uid: String): Boolean {//清空别名

        JPushInterface.deleteAlias(cc.context, uid.toInt())
        JPushInterface.stopPush(cc.context)
        CC.sendCCResult(cc.callId, CCResult.success())

        return false
    }


}
