package com.all.pdfreader.pdf.reader.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.pdf.reader.R
import com.all.pdfreader.pdf.reader.databinding.DialogDeleteBinding
import com.all.pdfreader.pdf.reader.util.ToastUtils

class PromptDialogFragment() : DialogFragment() {

    companion object {
        private const val ARG_TITLE = "arg_title"
        private const val ARG_DESC = "arg_desc"
        private const val ARG_OK_TEXT = "arg_ok_text"

        fun newInstance(
            title: String,
            desc: String,
            okBtnText: String? = null
        ): PromptDialogFragment {
            val fragment = PromptDialogFragment()
            val args = Bundle()
            args.putString(ARG_TITLE, title)
            args.putString(ARG_DESC, desc)
            args.putString(ARG_OK_TEXT, okBtnText)
            fragment.arguments = args
            return fragment
        }
    }

    var onOkClick: (() -> Unit)? = null
    var onCancelClick: (() -> Unit)? = null

    private val title: String
        get() = arguments?.getString(ARG_TITLE) ?: ""
    private val desc: String
        get() = arguments?.getString(ARG_DESC) ?: ""
    private val okBtnText: String?
        get() = arguments?.getString(ARG_OK_TEXT)

    private lateinit var binding: DialogDeleteBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 系统恢复时直接 dismiss 避免 crash
        if (savedInstanceState != null) {
            dismissAllowingStateLoss()
            return
        }
    }

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

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
            val margin = resources.getDimensionPixelSize(R.dimen.dialog_margin)
            val width = resources.displayMetrics.widthPixels - margin * 2
            setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

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

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

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