package com.xyzshell.myphoneinfo.base

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 androidx.viewbinding.ViewBinding
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.databinding.DialogBaseBinding

abstract class BaseDialogFragment<VB : ViewBinding>(
    private val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : DialogFragment() {

    private var _binding: DialogBaseBinding? = null
    private val baseBinding get() = _binding!!
    private var _contentBinding: VB? = null
    protected val binding get() = _contentBinding!!

    abstract fun getTitle(): String

    abstract fun getIconRes(): Int?

    open fun getDoubleBtn(): Boolean? = true


    open fun onPositiveClick() {}
    open fun onNegativeClick() {}

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DialogBaseBinding.inflate(inflater, container, false)

        // inflate 子类内容到 contentContainer
        _contentBinding = bindingInflater(inflater, baseBinding.contentContainer, true)

        return baseBinding.root
    }

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

        baseBinding.dialogTitle.text = getTitle()
        getIconRes()?.let {iconRes ->
            when (iconRes) {
                1 -> baseBinding.imageIcon.setImageResource(R.mipmap.battery_ic) // 替换为实际的资源ID
                2 -> baseBinding.imageIcon.setImageResource(R.mipmap.wifi)
                3 -> baseBinding.imageIcon.setImageResource(R.mipmap.app_full_ic)
                4 -> baseBinding.imageIcon.setImageResource(R.mipmap.display)
                5 -> baseBinding.imageIcon.setImageResource(R.mipmap.blue_tooth_ic)
                6 -> baseBinding.imageIcon.setImageResource(R.mipmap.mem_ic)
                7-> baseBinding.imageIcon.setImageResource(R.mipmap.setting_ic)
                8 -> baseBinding.imageIcon.setImageResource(R.mipmap.storyge)
                9 -> baseBinding.imageIcon.setImageResource(R.mipmap.camera_ic)
                10 -> baseBinding.imageIcon.setImageResource(R.mipmap.input_ic)
                // 添加更多数字对应的图片...
                else -> baseBinding.imageIcon.setImageResource(R.mipmap.main_logo)
            }
            baseBinding.imageIcon.visibility = View.VISIBLE
        }
        if(getDoubleBtn() == true){
            baseBinding.textCancel.visibility = View.VISIBLE
            baseBinding.textSettings.visibility = View.VISIBLE
            baseBinding.textOk.visibility = View.GONE
        }else{
            baseBinding.textCancel.visibility = View.GONE
            baseBinding.textSettings.visibility = View.GONE
            baseBinding.textOk.visibility = View.VISIBLE
        }
        baseBinding.textCancel.setOnClickListener {
            onPositiveClick()
            dismiss()
        }
        baseBinding.textSettings.setOnClickListener {
            onNegativeClick()
            dismiss()
        }
        baseBinding.textOk.setOnClickListener {
            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
        _contentBinding = null
    }
}


