package com.yidian.shenghuoquan.newscontent.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
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.view.WindowManager
import com.orhanobut.hawk.Hawk
import com.yidian.common.AppConfig.Companion.privacyPolicyStatement
import com.yidian.common.AppConfig.Companion.userProtocol
import com.yidian.common.HawkConfig
import com.yidian.common.XRouterPathConstants
import com.yidian.nightmode.widget.YdTextView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.ui.ProtocolActivity
import com.yidian.xpage.XPageManager


class ProtocolDialog(private val activity: Activity, themeResId: Int, private val paramsMap: HashMap<String, Any?>?): Dialog(activity, themeResId) {
    private lateinit var tvPrivacyPolicyTwo: YdTextView
    private lateinit var tvAgree: YdTextView
    private lateinit var tvDisagree: YdTextView
    private var lastClickTime = 0L

    init {
        setDialog()
    }

    private fun setDialog(){
        this.setCanceledOnTouchOutside(false)
        this.setContentView(R.layout.protocol_dialog)
        this.setOwnerActivity(activity)
        tvPrivacyPolicyTwo = findViewById(R.id.tv_privacyPolicyTwo)
        tvAgree = findViewById(R.id.tv_agree)
        tvDisagree = findViewById(R.id.tv_disagree)
        setSpannableString()
        setClickListener()
    }

    private fun setSpannableString(){
        tvPrivacyPolicyTwo.highlightColor = Color.TRANSPARENT
        val textSpannedBuilder = SpannableStringBuilder()
        val textSpanned1 = SpannableString(activity.getString(R.string.privacy_policy_two_first))
        val textSpanned2 = SpannableString(activity.getString(R.string.privacy_policy_statement))
        textSpanned2.setSpan(ForegroundColorSpan(activity.resources.getColor(R.color.lightBlueColor)), 0, textSpanned2.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        textSpanned2.setSpan(object: ClickableSpan(){
            override fun onClick(view: View) {
                val currentClickTime = System.currentTimeMillis()
                if(currentClickTime - lastClickTime < 1000) return
                lastClickTime = currentClickTime
                val urlMap = HashMap<String, String>()
                urlMap[ProtocolActivity.ProtocolUrlKey] = privacyPolicyStatement
                XPageManager.push(XRouterPathConstants.PROTOCOL, urlMap)
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.isUnderlineText = false
            }
        }, 0, textSpanned2.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        val textSpanned3 = SpannableString(activity.getString(R.string.and))
        textSpanned3.setSpan(ForegroundColorSpan(activity.resources.getColor(R.color.mainBlackColor)), 0, textSpanned3.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        val textSpanned4 = SpannableString(activity.getString(R.string.user_protocol))
        textSpanned4.setSpan(ForegroundColorSpan(activity.resources.getColor(R.color.lightBlueColor)), 0, textSpanned4.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        textSpanned4.setSpan(object: ClickableSpan(){
            override fun onClick(view: View) {
                val currentClickTime = System.currentTimeMillis()
                if(currentClickTime - lastClickTime < 1000) return
                lastClickTime = currentClickTime
                val urlMap = HashMap<String, String>()
                urlMap[ProtocolActivity.ProtocolUrlKey] = userProtocol
                XPageManager.push(XRouterPathConstants.PROTOCOL, urlMap)
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.isUnderlineText = false
            }
        }, 0, textSpanned4.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        val textSpanned5 = SpannableString(activity.getString(R.string.privacy_policy_two_second))
        textSpanned5.setSpan(ForegroundColorSpan(activity.resources.getColor(R.color.mainBlackColor)), 0, textSpanned5.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        tvPrivacyPolicyTwo.movementMethod = LinkMovementMethod.getInstance()
        tvPrivacyPolicyTwo.text = textSpannedBuilder.append(textSpanned1).append(textSpanned2).append(textSpanned3).append(textSpanned4).append(textSpanned5)
    }

    private fun setClickListener(){
        tvAgree.setOnClickListener{
            Hawk.put(HawkConfig.ProtocolIsAgree, true)
            dismiss()
            Handler().postDelayed({
                XPageManager.push(XRouterPathConstants.NEWS_MAIN, paramsMap)
                activity.finish()
            }, 1000)
        }

        tvDisagree.setOnClickListener{
            dismiss()
            val protocolHintDialog = ProtocolHintDialog(activity, R.style.normal_dialog, paramsMap)
            protocolHintDialog.show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val params: WindowManager.LayoutParams? = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.attributes?.gravity = Gravity.BOTTOM
    }
}