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.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 ProtocolHintDialog(private val activity: Activity, themeResId: Int, private val paramsMap: HashMap<String, Any?>?): Dialog(activity, themeResId) {
    private lateinit var tvContent: 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_hint_dialog)
        this.setOwnerActivity(activity)
        tvContent = findViewById(R.id.tv_content)
        tvAgree = findViewById(R.id.tv_agree)
        tvDisagree = findViewById(R.id.tv_disagree)
        setSpannableString()
        setClickListener()
    }

    private fun setSpannableString(){
        tvContent.highlightColor = Color.TRANSPARENT
        val textSpannedBuilder = SpannableStringBuilder()
        val textSpanned1 = SpannableString(activity.getString(R.string.protocol_hint_content_one))
        val textSpanned2 = SpannableString(activity.getString(R.string.privacy_policy))
        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.protocol_hint_content_two))
        textSpanned3.setSpan(ForegroundColorSpan(activity.resources.getColor(R.color.mainBlackColor)), 0, textSpanned3.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        tvContent.movementMethod = LinkMovementMethod.getInstance()
        tvContent.text = textSpannedBuilder.append(textSpanned1).append(textSpanned2).append(textSpanned3)
    }

    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()
            Handler().postDelayed({
                activity.finish()
            }, 200)
        }
    }

    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
    }
}