package com.xty.base.fragment

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.xty.common.util.SpannableUtils
import com.xty.common.weight.LoadingView
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.roundToInt

abstract  class IBaseFrag: Fragment() {
    val mainScope by lazy {  MainScope() }
    val bundle by lazy { Bundle() }
    val loadingView by lazy {   LoadingView.Builder(requireContext()).setCancelable(true).cteated() }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return setLayout()
    }

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

    abstract fun setLayout():View
    open fun initView(){
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this)
            EventBus.getDefault().post(this)
        }

    }
    open fun initData(){}


    fun statusBar(view:View){
      /*  view.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            var  statusBarHeight1 = -1;
            //获取status_bar_height资源的ID
            var  resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                val layoutParams = v.layoutParams as ViewGroup.LayoutParams
                statusBarHeight1 = resources.getDimensionPixelSize(resourceId);
                layoutParams.height=statusBarHeight1
                view.layoutParams=layoutParams
            }
        }*/

        view.post {
            var  statusBarHeight1 = -1;
            var  resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                val layoutParams = view.layoutParams as ViewGroup.LayoutParams
                statusBarHeight1 = resources.getDimensionPixelSize(resourceId);
                layoutParams.height=statusBarHeight1
                view.layoutParams=layoutParams
            }

        }

    }

    fun dp2px(dps: Int): Int {
        return (resources.displayMetrics.density * dps).roundToInt()
    }


    fun getColor(@ColorRes id: Int): Int {
        return ContextCompat.getColor(
            requireContext(), id
        )
    }

    fun getDrawable(@DrawableRes id: Int): Drawable {
        return ContextCompat.getDrawable(requireContext(), id)!!
    }

    /**
     *  设置字体span
     *
     *  @param text 内容
     *  @param spanSize 字体大小
     *  @param fontColor 字体颜色
     *  @param startIndex 开始位置
     *  @param endIndex 结束位置
     *
     */
    fun setStrSpan(
        text: String,
        spanSize: Int,
        fontColor: Int,
        styleSpan: Int,
        startIndex: Int,
        endIndex: Int
    ): SpannableString {
        return SpannableUtils.setStrSpan(text, spanSize, fontColor, styleSpan, startIndex, endIndex)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        mainScope.cancel()
    }

    @Subscribe (threadMode = ThreadMode.MAIN)
    fun noneEvent(none: Any?){
    }

}