package com.cheng.blzb.ui.dialog

import android.annotation.SuppressLint
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.example.base.extensions.getColor
import com.example.base.extensions.onClick
import com.example.base.utils.ScreenUtils
import com.example.base.utils.SpanUtils
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogPrivacyPolicyBinding
import com.cheng.blzb.manager.DialogEnum
import com.cheng.blzb.utils.UrlHelper

class PrivacyPolicyDialog : DialogFragment() {

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

    lateinit var binding: DialogPrivacyPolicyBinding

    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))
        dialog?.setCancelable(false)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    @SuppressLint("SetTextI18n")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_privacy_policy, null)

        binding = DialogPrivacyPolicyBinding.bind(view)
        binding.tvTitle.typeface = Constants.douyinsansB

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

        binding.btnDisagree.onClick {
            mOnBackListener?.invoke(DialogEnum.CLICK_CANCEL)
            dismiss()
        }

        initAgreement()

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

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

    private fun initAgreement() {
        SpanUtils.with(binding.tvContent)
            .append("请你务必审慎阅读、充分理解")
            .append("《服务协议》")
            .setBold()
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startUserAgreement(requireContext())
            }
            .append("和")
            .append("《隐私政策》")
            .setBold()
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startPrivacyPolicy(requireContext())
            }
            .append("各条款，包括但不限于:为了更好的向你提供服务，我们需要访问你的相册、位置信息等。你可阅读")
            .append("《隐私政策》")
            .setBold()
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startPrivacyPolicy(requireContext())
            }
            .append("了解详细信息。如果你同意，请点击下面同意按钮开始接受我们的服务。")
            .create()
    }
}