package com.cheng.blzb.ui.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogPayTipBinding
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.utils.UrlHelper
import com.example.base.extensions.getColor
import com.example.base.extensions.onClick
import com.example.base.utils.ScreenUtils
import com.example.base.utils.SpanUtils

class PayTipDialog : DialogFragment() {
    private val showPolicy by lazy { arguments?.getBoolean("show_policy") ?: false }
    private val showAgreement by lazy { arguments?.getBoolean("show_agreement") ?: false }
    private val showRenew by lazy { arguments?.getBoolean("show_renew") ?: false }

    private var mOnBackListener: ((DialogEnum) -> Unit)? = null     //回调事件

    lateinit var binding: DialogPayTipBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = (ScreenUtils.getWindowSize().x * 0.8).toInt()
        dialog?.window?.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_pay_tip, null)

        binding = DialogPayTipBinding.bind(view)

        binding.tvTitle.typeface = Constants.douyinsansB

        binding.tvOk.onClick {
            mOnBackListener?.invoke(DialogEnum.CLICK_OK)
            dismiss()
        }

        binding.tvClose.onClick { dismiss() }
        binding.ivClose.onClick { dismiss() }

        initAgreement()

        val dialog = Dialog(requireContext())
        dialog.setContentView(view)
        return dialog
    }

    private fun initAgreement() {
        val spanUtils = SpanUtils.with(binding.tvContent)
        if (showPolicy) {
            spanUtils.append("我已阅读并同意")
                .append("《隐私协议》")
                .setClickSpan(getColor(R.color.color_125ffe), false) {
                    UrlHelper.startPrivacyPolicy(requireContext())
                }
        } else if (showAgreement) {
            spanUtils.append("我已阅读并同意")
                .append("《会员服务协议规则》")
                .setClickSpan(getColor(R.color.color_125ffe), false) {
                    UrlHelper.startUserAgreement(requireContext(), "会员服务协议规则")
                }
        }
        if (showRenew) {
            spanUtils.append("和")
            spanUtils.append("《自动续费服务规则》")
            spanUtils.setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startRenewAgreement(requireContext())
            }
        }
        spanUtils.create()
    }

    fun setOnSelectListener(listener: ((DialogEnum) -> Unit)) {
        mOnBackListener = listener
    }

    companion object {
        fun newInstance(showPolicy: Boolean = false, showAgreement: Boolean = false, showRenew: Boolean = false): PayTipDialog {
            val arg = Bundle()
            arg.putBoolean("show_policy", showPolicy)
            arg.putBoolean("show_agreement", showAgreement)
            arg.putBoolean("show_renew", showRenew)
            val fragment = PayTipDialog()
            fragment.arguments = arg
            return fragment
        }
    }
}