package com.zj365.dc.weight

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import androidx.core.content.ContextCompat
import com.amap.api.location.AMapLocationClient
import com.hjq.toast.ToastUtils
import com.xty.base.dialog.BaseDialog
import com.xty.common.Const
import com.xty.common.MyLinkedMovementMethod
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.zj365.dc.R
import com.zj365.dc.databinding.DialogPrevBinding


class PrevDialog(context: Context, private var agree: () -> Unit, private var giveUp: () -> Unit) :
    BaseDialog(context) {
    val binding by lazy { DialogPrevBinding.inflate(layoutInflater) }
    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        percentHeight = 3 / 5f
        setDialogPadding(80, 0, 80, 80)

        val txtCol = ContextCompat.getColor(context, R.color.col_009)
        binding.mContent.movementMethod = MyLinkedMovementMethod().getInstance()
        val string = context.getString(R.string.prev_content)
        val span = SpannableString(string)
        span.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val bundle = Bundle()
                bundle.clear()
                bundle.putString("title", "用户协议")
                bundle.putString("url","https://dc.zhongjian365.com/agreement.html")
                RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = txtCol
                ds.isUnderlineText = false
            }

        }, 49, 55, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        span.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val bundle = Bundle()
                bundle.putString("title", "隐私政策")
                bundle.putString("url", "https://dc.zhongjian365.com/privacy.html")
                RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = txtCol
                ds.isUnderlineText = false
                ds.clearShadowLayer()

            }
        }, 56, 62, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val bundle = Bundle()
                bundle.putString("title", "中健三六五健康 APP 权限列表")
                bundle.putString("url", "https://dc.zhongjian365.com/permission.html")
                RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = txtCol
                ds.isUnderlineText = false
                ds.clearShadowLayer()

            }
        }, 72, 78, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val bundle = Bundle()
                bundle.clear()
                bundle.putString("title", "用户协议")
                bundle.putString("url","https://dc.zhongjian365.com/agreement.html")
                RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = txtCol
                ds.isUnderlineText = false
            }

        }, 79, 85, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        /*span.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val bundle = Bundle()
                bundle.putString("title", "隐私政策")
                bundle.putString("url", "https://auprty.com/app/static/app/privacy.html")
                RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = txtCol
                ds.isUnderlineText = false
                ds.clearShadowLayer()

            }
        }, 92, 98, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        span.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val bundle = Bundle()
                bundle.putString("title", "中健三六五健康 APP 权限列表")
                bundle.putString("url", "https://auprty.com/app/static/app/permission.html")
                RouteManager.goAct(ARouterUrl.AGREEMNT_READ_WEB_ACT, bundle)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = txtCol
                ds.isUnderlineText = false
                ds.clearShadowLayer()

            }
        }, 99, 114, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)*/

        binding.mContent.text = span
        binding.mContent.highlightColor = Color.TRANSPARENT
        binding.mClose.setOnClickListener {
            dismiss()
            giveUp.invoke()
        }
        binding.mAgree.setOnClickListener {
            dismiss()
            agree.invoke()
            AMapLocationClient.updatePrivacyShow(context,true,true);
            AMapLocationClient.updatePrivacyAgree(context,true);
        }
    }
}