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

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.res.Resources
import android.os.Looper
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.WindowManager
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
import com.quyunshuo.androidbaseframemvvm.base.utils.network.AutoRegisterNetListener
import me.jessyan.autosize.AutoSizeCompat

abstract class BaseFrameDialog<VB : ViewBinding>(
    protected var mContext: Context,
) :
    Dialog(mContext, R.style.base_DialogBgD), FrameView<VB> {


    protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) { createVB() }

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

    init {
        setContentView(mBinding.root)
        // ARouter 依赖注入
        ARouter.getInstance().inject(this)

        // 根据子类是否有 RegisterEventBus 注解決定是否进行注册 EventBus
        if (javaClass.isAnnotationPresent(RegisterEventBus::class.java)) {
            mHaveRegisterEventBus = true
            EventBusUtils.register(this)
        }

        mBinding.initView()
        initObserve()
        initRequestData()
    }


    override fun onStart() {
        super.onStart()
        val window = window
        if (window != null) {
            //dialog宽高(横向是屏幕宽度,纵向是布局高度)
            val displayMetrics = DisplayMetrics()
            window.windowManager.defaultDisplay.getMetrics(displayMetrics)
            //  window.setLayout(displayMetrics.widthPixels, window.attributes.height)
            window.setLayout(displayMetrics.widthPixels,displayMetrics.heightPixels)
            //            dialog位置、动画
            val animType = 0
            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)
//                    window.setWindowAnimations(R.style.bottom_int_out_dialog_style)
                }

                ANIM_LEFT_IN_OUT -> {
                    window.setGravity(Gravity.LEFT)
//                    window.setWindowAnimations(R.style.left_int_out_dialog_style)
                }
            }
        }
    }


    fun stopTimeDown() {
    }


    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 onDetachedFromWindow() {
        if (mHaveRegisterEventBus) {
            EventBusUtils.unRegister(this)
        }
        super.onDetachedFromWindow()
    }

}
