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.example.base.browser.BrowserActivity
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.g.gysdk.GYManager
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogLoginTipBinding
import com.cheng.blzb.utils.UrlHelper

class LoginTipDialog :DialogFragment(){
    private val type by lazy { arguments?.getInt("type") ?: 0 }

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

    lateinit var binding: DialogLoginTipBinding

    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_login_tip, null)

        binding = DialogLoginTipBinding.bind(view)

        binding.tvTitle.typeface = Constants.douyinsansB

        if (type == 0){
            initLoginPrivacyTv()
        } else {
            initOneKeyLoginPrivacyTv()
        }

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

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

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

    private fun initLoginPrivacyTv() {
        SpanUtils.with(binding.tvTip)
            .append("登录之前须先查看并同意")
            .append("《用户协议》")
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startUserAgreement(requireContext())
            }
            .append("和")
            .append("《隐私政策》")
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startPrivacyPolicy(requireContext())
            }
            .create()
    }
    private fun initOneKeyLoginPrivacyTv() {
        val preLoginResult = GYManager.getInstance().preLoginResult
        SpanUtils.with(binding.tvTip)
            .append("登录之前须先查看并同意")
            .append("《${preLoginResult.privacyName}》")
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                BrowserActivity.start(requireContext(), preLoginResult.privacyName, preLoginResult.privacyUrl)
            }
            .append("、")
            .append("《用户协议》")
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startUserAgreement(requireContext())
            }
            .append("、")
            .append("《隐私政策》")
            .setClickSpan(getColor(R.color.color_125ffe), false) {
                UrlHelper.startPrivacyPolicy(requireContext())
            }
            .create()
    }

    companion object {
        fun newInstance(type: Int = 0): LoginTipDialog {
            val arg = Bundle()
            val fragment = LoginTipDialog()
            arg.putInt("type", type)
            fragment.arguments = arg
            return fragment
        }
    }
}