package com.ym.game.utils

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.widget.TextView
import com.ym.ddhy.R
import com.ym.library.base.BaseDialog
import com.ym.library.net.NetConfig
import com.ym.library.utils.JumpUtils
import org.jetbrains.anko.act

/**
 * Created by gaoleichao on 2018/8/17.
 */
object CenterDialog:BaseDialog() {

    /**
     * 隐私协议弹窗
     */
    fun showPrivacyProtocol(activity: Activity, disagreelistener: View.OnClickListener,agreelistener: View.OnClickListener?) {
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_privacy_protocol_view,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )
        val content = mDialogView.findViewById<TextView>(R.id.id_dialog_privacy_protocol_content)
        val desc = mDialogView.findViewById<TextView>(R.id.id_dialog_privacy_protocol_desc)

        mDialogView.findViewById<TextView>(R.id.cancel_button)
            .setOnClickListener { activity.finish() }
        val clickUa: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                JumpUtils.h5Jump("隐私政策", NetConfig.H5.WEB_URL_PRIVACY,true,activity)
            }
        }
        val clickPrivat: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                JumpUtils.h5Jump("用户协议", NetConfig.H5.WEB_URL_USER,true,activity)
            }
        }
//        《隐私政策》和《用户协议》，请您在使用前仔细阅读并了解
        val descStr = SpannableStringBuilder("您点击“同意”，即表示您阅读并同意更新后的《用户协议》和《隐私政策》")
        descStr.setSpan(
            clickPrivat, 22, 26,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        descStr.setSpan(
            clickUa, 29, descStr.length - 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        descStr.setSpan(
            ForegroundColorSpan(Color.parseColor("#44a1ff")), 21, 27,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        descStr.setSpan(
            ForegroundColorSpan(Color.parseColor("#44a1ff")), 29, descStr.length - 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        desc.text = descStr
        desc.setMovementMethod(LinkMovementMethod.getInstance());


        val str = "欢迎使用多多花园！为了更好的保护您的隐私和个人信息安全，根据国家相关法律规定和标准更新了"
        val ss = SpannableStringBuilder("查看完整《用户协议》和《隐私政策》，请您在使用前仔细阅读并了解。")

        ss.setSpan(
            clickPrivat, 4, 10,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        ss.setSpan(
            clickUa, 12, 16,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        ss.setSpan(
            ForegroundColorSpan(Color.parseColor("#44a1ff")), 4, 10,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        ss.setSpan(
            ForegroundColorSpan(Color.parseColor("#44a1ff")), 12, 16,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        content.append(str)
        content.append(ss)
        content.setMovementMethod(LinkMovementMethod.getInstance());

        mDialogView.findViewById<TextView>(R.id.privacy_protocol_button).setOnClickListener {
            mDialog.dismiss()
            agreelistener?.onClick(it)
        }
        mDialog.findViewById<TextView>(R.id.cancel_button).setOnClickListener {
            mDialog.dismiss()
            disagreelistener?.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.show()
    }
}