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.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.example.base.utils.ScreenUtils
import com.cheng.blzb.R
import com.cheng.blzb.databinding.DialogPermissionTipBinding

class PermissionTipDialog : DialogFragment() {

    private var content: CharSequence? = null

    lateinit var binding: DialogPermissionTipBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = (ScreenUtils.getWindowSize().x * 0.9).toInt()
        windowParams?.gravity = Gravity.TOP
        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_permission_tip, null)

        binding = DialogPermissionTipBinding.bind(view)

        content = arguments?.getCharSequence("content")

        if (!TextUtils.isEmpty(content)) {
            binding.tvContent.text = content
        }

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

    companion object {
        fun newInstance(
            content: CharSequence,
        ): PermissionTipDialog {
            val arg = Bundle()
            val fragment = PermissionTipDialog()
            arg.putCharSequence("content", content)
            fragment.arguments = arg
            return fragment
        }
    }
}