package com.quyunshuo.androidbaseframemvvm.base.mvvm.v

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.launcher.ARouter
import com.quyunshuo.androidbaseframemvvm.base.R
import com.quyunshuo.androidbaseframemvvm.base.mvvm.vm.BaseViewModel
import com.quyunshuo.androidbaseframemvvm.base.utils.EventBusUtils
import com.quyunshuo.androidbaseframemvvm.base.utils.RegisterEventBus

abstract class BaseFrameDialogFragment<VB : ViewBinding, VM : BaseViewModel> : DialogFragment(),FrameView<VB> {
    private  val TAG = "BaseFrameDialogFragment"
    protected lateinit var mContext: Context

    private var _binding: VB? = null
    protected abstract val mViewModel: VM

    protected val mBinding get() = _binding!!

    /**
     * 是否有 [RegisterEventBus] 注解，避免重复调用 [Class.isAnnotationPresent]
     */
    private var mHaveRegisterEventBus = false


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mContext = requireContext()
        // ARouter 依赖注入
        ARouter.getInstance().inject(this)
        var className=javaClass.name
        Log.d("backinfo","类名:"+className)
        // 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
        if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
            mHaveRegisterEventBus = true
            EventBusUtils.register(this)
        }
        _binding?.initView()
        initObserve()
        initRequestData()
    }

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

    @RequiresApi(Build.VERSION_CODES.R)
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window
        if (window != null) {
            // 设置对话框宽高和位置

            val display = window.windowManager.defaultDisplay
            window.setLayout(display.width, ViewGroup.LayoutParams.MATCH_PARENT)
            Log.d(TAG, "display.width: "+display.width)
//            val animType = getAnimationType()
//            when (animType) {
//                ANIM_CENTER_IN_OUT -> {
                    window.setGravity(Gravity.CENTER)
            window.setWindowAnimations(R.style.center_in_out_dialog_style)

//                    // 设置动画（如果需要）
//                }
//
//                ANIM_BOTTOM_IN_OUT -> {
//                    window.setGravity(Gravity.BOTTOM)
//                    // 设置动画（如果需要）
//                }
//
//                ANIM_LEFT_IN_OUT -> {
//                    window.setGravity(Gravity.LEFT)
//                    // 设置动画（如果需要）
//                }
//            }
        }
    }

    // 抽象方法，用于子类指定动画类型
    protected abstract fun getAnimationType(): Int

    // 其他方法...

    override fun onDestroyView() {
        super.onDestroyView()
        // 清理资源
        if (mHaveRegisterEventBus) {
            EventBusUtils.unRegister(this)
        }
    }

    companion object {
        // 动画类型常量
        const val ANIM_CENTER_IN_OUT: Int = 0
        const val ANIM_BOTTOM_IN_OUT: Int = 1
        const val ANIM_LEFT_IN_OUT: Int = 2
    }

    override fun getTheme(): Int {
        return R.style.base_DialogBgD
    }



}