package com.xyzshell.myphoneinfo.dialog

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.DialogFragment
import com.xyzshell.myphoneinfo.databinding.DialogAppClickBinding

class AppDialogFragment : DialogFragment() {

    private var _binding: DialogAppClickBinding? = null
    private val baseBinding get() = _binding!!


    open fun onPositiveClick() {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DialogAppClickBinding.inflate(inflater, container, false)
        return baseBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        baseBinding.textCancel.setOnClickListener {
            onPositiveClick()
            dismiss()
        }
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.let { window ->
            window.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())

            val params = window.attributes
            val displayMetrics = resources.displayMetrics
            val margin = (10 * displayMetrics.density).toInt()
            params.width = displayMetrics.widthPixels - margin * 2
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
            window.attributes = params
            window.setGravity(Gravity.CENTER)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}