package com.cooleshow.teacher.ui.splash

import android.net.Uri
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.TextUtils
import android.text.style.ClickableSpan
import android.view.View
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
import com.alibaba.android.arouter.launcher.ARouter
import com.cooleshow.base.bean.RouteBean
import com.cooleshow.base.common.BaseApplication
import com.cooleshow.base.common.ConstantKey
import com.cooleshow.base.common.WebConstants
import com.cooleshow.base.router.RouterPath
import com.cooleshow.base.ui.activity.BaseActivity
import com.cooleshow.base.utils.GsonUtils
import com.cooleshow.base.utils.JumpUtils
import com.cooleshow.base.utils.SPUtils
import com.cooleshow.base.utils.ToastUtil
import com.cooleshow.base.widgets.dialog.PrivacyTipDialog
import com.cooleshow.teacher.databinding.ActivitySplashLayoutBinding
import com.cooleshow.usercenter.constants.UserConstants
import com.cooleshow.usercenter.helper.UserHelper
import com.cooleshow.usercenter.ui.activity.VerifyCodeLoginActivity
import com.umeng.commonsdk.UMConfigure


/**
 * Author by pq, Date on 2022/4/19.
 */
class SplashActivity : BaseActivity<ActivitySplashLayoutBinding>() {
    override fun getLayoutView(): ActivitySplashLayoutBinding {
        return ActivitySplashLayoutBinding.inflate(layoutInflater)
    }

    override fun initView() {

    }

    override fun initData() {
        super.initData()
        var privacyMode = UserHelper.getAppPrivacyMode()
        if (privacyMode == 0) {
            //显示隐私协议提示弹窗
            showPrivacyTipDialog()
        } else {
            goIntent()
        }
    }

    private fun showPrivacyTipDialog() {
        val contentStart = "亲爱的用户，感谢您的信任并使用酷乐秀学院APP!\n    在使用我们的产品和服务前，请您先阅读并了解"
        val userProtocol = "《用户协议》"
        val privacy = "和《隐私政策》"
        val contentEnd = """
            。请您在点击同意之前充分理解相关条款，其中的重点条款已为您标注，方便您了解自己的权利。
            我们将通过《隐私政策》向您说明 ：
            1.为了您可以更好的学习器乐，我们会需要您填写您的 基本信息，我们会根据您填写内容，收集和使用对应的必要信息（如手机号，声部信息等）。
            2.您可以对上述信息进行访问、更正、删除以及注销账户，我们也将提供专门的个人信息保护联系方式。
            3.未经您的授权同意，我们不会将上述信息共享给第三方或用于您未授权的其他用途。
            
            我们将严格按照上述协议为您提供服务，保护您的信息安全，点击“同意”即表示您已阅读并同意全部条款，可以继续使用我们的产品和服务。
            
            """.trimIndent()

        val spannableStringBuilder =
            SpannableStringBuilder(contentStart + userProtocol + privacy + contentEnd)
        val userProtocolSpan: ClickableSpan = object : ClickableSpan() {
            override fun updateDrawState(ds: TextPaint) {
                ds.linkColor = resources.getColor(com.cooleshow.base.R.color.color_2dc7aa)
                super.updateDrawState(ds)
            }

            override fun onClick(view: View) {
                //跳转用户协议
                ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.REGISTRATION_AGREEMENT)
                    .navigation()
            }
        }
        val privacySpan: ClickableSpan = object : ClickableSpan() {
            override fun updateDrawState(ds: TextPaint) {
                ds.linkColor = resources.getColor(com.cooleshow.base.R.color.color_2dc7aa)
                super.updateDrawState(ds)
            }

            override fun onClick(view: View) {
                //跳转隐私协议
                ARouter.getInstance()
                    .build(RouterPath.WebCenter.ACTIVITY_HTML)
                    .withString(WebConstants.WEB_URL, WebConstants.PRIVACY_AGREEMENT)
                    .navigation()
            }
        }

        val startIndex = contentStart.length
        spannableStringBuilder.setSpan(
            userProtocolSpan,
            startIndex,
            startIndex + 6,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        spannableStringBuilder.setSpan(
            privacySpan,
            startIndex + 7,
            startIndex + 13,
            Spannable.SPAN_EXCLUSIVE_INCLUSIVE
        )
        var dialog: PrivacyTipDialog = PrivacyTipDialog(this)
        dialog.show()
        dialog.setContent(spannableStringBuilder)
        dialog.setOnCancelClickListener {
            ToastUtil.getInstance().showShort("不同意用户协议及隐私协议将无法正常使用酷乐秀学院哦")
            finish()
        }
        dialog.setOnConfirmClickListener {
            UserHelper.setAppPrivacyModeIsAgree()

            goIntent()
        }
    }

    private fun goIntent() {
        val isFirstLaunch = SPUtils.getInstance().getBoolean(ConstantKey.IS_FIRST_LAUNCH, false)
        if (isFirstLaunch) {
            if (UserHelper.isLogin()) {
                //接收h5跳转意图
                val uri: Uri? = intent.data
                if (uri != null && uri.isHierarchical) {
                    val params: String = uri.getQueryParameter("url").toString()
                    if (!TextUtils.isEmpty(params)) {
                        val routeBean = GsonUtils.fromJson(params, RouteBean::class.java)
                        if (JumpUtils.jump(routeBean)) {
                            return
                        }
                    }
                }
                jumpMain()
            } else {
                jumpLogin()
            }
        } else {
            goGuide();
        }

        finish()
    }

    private fun goGuide() {
        ARouter.getInstance().build(RouterPath.SplashCenter.SPLASH_GUIDE).navigation()
    }

    private fun jumpMain() {
        ARouter.getInstance().build(RouterPath.APPCenter.PATH_HOME).navigation()
    }

    private fun jumpLogin() {
        ARouter.getInstance().build(RouterPath.UserCenter.PATH_VERIFY_LOGIN)
            .withInt(UserConstants.LOGIN_ITEM, VerifyCodeLoginActivity.TEACHER_ITEM)
            .navigation()
    }

}