package com.duben.microtribe.ui.widgets

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextPaint
import android.text.TextUtils
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.duben.microtribe.R
import com.duben.microtribe.common.Constant
import com.duben.microtribe.ui.activitys.WebActivity
import com.duben.microtribe.utils.SpanUtils

/**
 * 仿苹果自定义dialog选择框
 */
class CustomDialogAgreement @JvmOverloads constructor(
    private val context: Context,
    private var listener: DialogListener? = null
) : BaseDialog(
    context, R.style.dialog
) {
    private val lp: WindowManager.LayoutParams
    private val dialog_tv_title: TextView
    private val dialog_tv_content: TextView
    private val dialog_btn_left: Button
    private val dialog_btn_right: Button
    private val dialog_v_line: View

    init {
        if (listener != null) {
            listener!!.setDialog(this)
        }
        setContentView(R.layout.dialog_main)
        // 设置window属性
        lp = window!!.attributes
        lp.gravity = Gravity.CENTER
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp.windowAnimations = R.style.DialogAnimBottom
        window!!.attributes = lp

        // 设置外部不可关闭
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        setOnKeyListener { dialog: DialogInterface?, keyCode: Int, event: KeyEvent? -> keyCode == KeyEvent.KEYCODE_BACK }

        // 查找View
        dialog_tv_title = findViewById(R.id.dialog_tv_title)
        dialog_tv_content = findViewById(R.id.dialog_tv_content)
        dialog_btn_left = findViewById(R.id.dialog_btn_left)
        dialog_btn_right = findViewById(R.id.dialog_btn_right)
        dialog_v_line = findViewById(R.id.dialog_v_line)

        initListener()
    }

    private fun initListener() {
        if (listener != null) {
            dialog_btn_left.setOnClickListener(listener)
            dialog_btn_right.setOnClickListener(listener)
        }
    }

    fun setDialogListener(listener: DialogListener?) {
        this.listener = listener
    }

    /**
     * Dialog显示标题
     *
     * @param s
     */
    fun setTitle(s: String?) {
        dialog_tv_title.visibility = View.VISIBLE
        dialog_tv_title.text = s
    }

    /**
     * Dialog显示标题
     *
     * @param size
     */
    fun setTitleSize(size: Int) {
        dialog_tv_title.textSize = size.toFloat()
    }

    /**
     * Dialog显示内容
     *
     * @param s
     */
    fun setContent(s: String) {
        SpanUtils.with(dialog_tv_content)
            .append("为了更好的保护您的合法权益，请您阅读并同意以下协议")
            .append("《${s}》").setClickSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    val bundle = Bundle()
                    bundle.putString(WebActivity.WEB_TITLE, s)
                    bundle.putString(WebActivity.WEB_URL, Constant.MEMBERS_URL)
                    readyGo(context,WebActivity::class.java, bundle)
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = ContextCompat.getColor(context, R.color.main_mints)
                    ds.isUnderlineText = false
                }
            })
            .create()
    }

    /**
     * Dialog显示content文字
     *
     * @param color
     */
    fun setContentColor(color: Int) {
        dialog_tv_content.setTextColor(ContextCompat.getColor(context, color))
    }

    /**
     * content文字大小
     *
     * @param size
     */
    fun setContentSize(size: Int) {
        dialog_tv_content.textSize = size.toFloat()
    }

    fun setContentGravity(gravity: Int) {
        dialog_tv_content.gravity = gravity
    }

    fun setLeftClickListener(listener: DialogListener?) {
        dialog_btn_left.setOnClickListener(listener)
    }

    fun setRightClickListener(listener: DialogListener?) {
        dialog_btn_right.setOnClickListener(listener)
    }

    /**
     * Dialog显示left按钮文字
     *
     * @param s
     */
    fun setLeft(s: String?) {
        dialog_btn_left.text = s
        if (TextUtils.isEmpty(dialog_btn_right.text)) {
            dialog_btn_right.visibility = View.GONE
            dialog_v_line.visibility = View.GONE
        } else {
            dialog_btn_left.visibility = View.VISIBLE
            dialog_v_line.visibility = View.VISIBLE
        }
    }

    /**
     * 只显示一个按钮
     *
     * @param
     */
    fun setSingleBtn() {
        dialog_btn_left.visibility = View.VISIBLE
        dialog_v_line.visibility = View.GONE
        dialog_btn_right.visibility = View.GONE
    }

    /**
     * Dialog显示left按钮文字颜色
     *
     * @param color
     */
    fun setLeftColor(color: Int) {
        dialog_btn_left.setTextColor(ContextCompat.getColor(context, color))
    }

    /**
     * Dialog显示right按钮文字
     *
     * @param s
     */
    fun setRight(s: String?) {
        dialog_btn_right.text = s
        if (TextUtils.isEmpty(dialog_btn_left.text)) {
            dialog_btn_left.visibility = View.GONE
            dialog_v_line.visibility = View.GONE
        } else {
            dialog_btn_right.visibility = View.VISIBLE
            dialog_v_line.visibility = View.VISIBLE
        }
    }

    /**
     * Dialog显示right按钮文字
     *
     * @param s
     */
    fun setRightDelay(s: String, second: Int) {
        dialog_btn_right.text = s
        if (TextUtils.isEmpty(dialog_btn_left.text)) {
            dialog_btn_left.visibility = View.GONE
            dialog_v_line.visibility = View.GONE
        } else {
            dialog_btn_right.visibility = View.VISIBLE
            dialog_v_line.visibility = View.VISIBLE
            dialog_btn_right.isEnabled = false
            val countDownTimer: CountDownTimer =
                object : CountDownTimer((second * 1000).toLong(), 1000) {
                    override fun onTick(millisUntilFinished: Long) {
                        val value = (millisUntilFinished / 1000 + 1).toInt().toString()
                        dialog_btn_right.text = "$s($value)"
                    }

                    override fun onFinish() {
                        dialog_btn_right.text = s
                        dialog_btn_right.isEnabled = true
                    }
                }
            countDownTimer.start()
        }
    }

    /**
     * Dialog显示right按钮文字颜色
     *
     * @param color
     */
    fun setRightColor(color: Int) {
        dialog_btn_right.setTextColor(ContextCompat.getColor(context, color))
    }

    protected fun readyGo(context: Context,clazz: Class<*>?, bundle: Bundle?) {
        val intent: Intent = Intent(context, clazz)
        if (null != bundle) {
            intent.putExtras(bundle)
        }
        context.startActivity(intent)
    }
}