package com.all.pdfreader.pro.app.ui.dialog

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.DialogFragment
import com.all.pdfreader.pro.app.R
import com.all.pdfreader.pro.app.databinding.DialogDeleteBinding
import com.all.pdfreader.pro.app.util.ToastUtils

class PromptDialogFragment(
    private val title: String,
    private val desc: String,
    private val okBtnText: String? = null,
    private val onOkClick: () -> Unit,
    private val onCancelClick: () -> Unit = {}
) : DialogFragment() {

    private lateinit var binding: DialogDeleteBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = DialogDeleteBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            // 去掉系统默认的背景 padding
            setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
            // 设置宽度为全屏减去 16dp
            val margin = resources.getDimensionPixelSize(R.dimen.dialog_margin) // 16dp
            val width = resources.displayMetrics.widthPixels - margin * 2
            setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (okBtnText.isNullOrEmpty()) {
            binding.okBtnTv.text = getString(R.string.delete)
        } else {
            binding.okBtnTv.text = okBtnText
        }
        binding.deleteTitleTv.text = title
        binding.deleteDescTv.text = desc
        setupOnClick()
    }

    private fun setupOnClick() {
        binding.okBtn.setOnClickListener {
            onOkClick()
            dismiss()
        }
        binding.cancelBtn.setOnClickListener {
            onCancelClick()
            dismiss()
        }
    }

    private fun showToast(message: String) {
        ToastUtils.show(requireActivity(), message)
    }
}