package com.rdiot.yx485.login.view

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.method.ScrollingMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.rdiot.yx485.databinding.DialogPersonalInfoTipsBinding
import com.rdiot.yx485.http.Url
import com.rdiot.yx485.webview.WebViewActivity

class PersonalInfoProtectTips(val context: Activity) : Dialog(context) {
    private val mBinding by lazy { DialogPersonalInfoTipsBinding.inflate(layoutInflater) }
    private var mOnDialogClickListener: OnDialogClickListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.decorView?.setBackgroundColor(Color.TRANSPARENT)
        setContentView(mBinding.root)

        setCancelable(false)

        val content = mBinding.tvContent.text.toString()
        val string = SpannableStringBuilder(content)
        val index0 = content.indexOf("用户协议")
        val index1 = content.indexOf("隐私政策")
        val click0 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                WebViewActivity.newIntent(context, Url.BASE_URL2 + "/#/ServiceAgreement", null)
            }
        }
        val click1 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                WebViewActivity.newIntent(context, Url.BASE_URL2 + "/#/PrivacyPolicy", null)
            }
        }

        string.setSpan(click0, index0, index0 + 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        string.setSpan(click1, index1, index1 + 4, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
        mBinding.tvContent.movementMethod = ScrollingMovementMethod.getInstance()
        mBinding.tvContent.movementMethod = LinkMovementMethod.getInstance()
        mBinding.tvContent.text = string

        mBinding.tvDisagree.setOnClickListener {
            dismiss()
            mOnDialogClickListener?.onCancel()
        }
        mBinding.btnAgree.setOnClickListener {
            dismiss()
            mOnDialogClickListener?.onConfirm()
        }
    }

    fun setOnClickListener(listener: OnDialogClickListener) {
        mOnDialogClickListener = listener
    }
}