package com.gh.gamecenter.common.base

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.facebook.drawee.view.SimpleDraweeView
import com.gh.gamecenter.common.R
import com.gh.gamecenter.common.utils.dimOnDarkMode

class CustomLayoutInflaterFactory(
    private val mAppCompatActivity: AppCompatActivity
) : LayoutInflater.Factory2 {

    override fun onCreateView(
        parent: View?,
        name: String,
        context: Context,
        attrs: AttributeSet
    ): View? {

        val view: View? = try {
            mAppCompatActivity.delegate.createView(parent, name, context, attrs)
                ?: mAppCompatActivity.onCreateView(parent, name, context, attrs)
                ?: mAppCompatActivity.layoutInflater.createView(name, null, attrs)
        } catch (e: Exception) {
            try {
                if (e is ClassNotFoundException) {
                    mAppCompatActivity.layoutInflater.createView(name, "android.widget.", attrs)
                }
            } catch (e2: Exception) {
                return null
            }
            return null
        }

        val n = attrs.attributeCount
        for (i in 0 until n) {
            val attributeName = attrs.getAttributeName(i).toString()
            when (attributeName) {
                "background" -> view?.setTag(TAG_BACKGROUND_ID, attrs.getAttributeValue(i))
                "textColor" -> view?.setTag(TAG_TEXT_COLOR_ID, attrs.getAttributeValue(i))
                "textColorHint" -> view?.setTag(TAG_TEXT_COLOR_HINT_ID, attrs.getAttributeValue(i))
                "src", "srcCompat" -> view?.setTag(TAG_SRC_ID, attrs.getAttributeValue(i))
                "drawableLeft", "drawableStart", "drawableLeftCompat", "drawableStartCompat"-> view?.setTag(
                    TAG_DRAWABLE_LEFT_ID,
                    attrs.getAttributeValue(i)
                )
                "drawableTop", "drawableTopCompat" -> view?.setTag(TAG_DRAWABLE_TOP_ID, attrs.getAttributeValue(i))
                "drawableRight", "drawableEnd", "drawableRightCompat", "drawableEndCompat" -> view?.setTag(
                    TAG_DRAWABLE_RIGHT_ID,
                    attrs.getAttributeValue(i)
                )
                "drawableBottom", "drawableBottomCompat" -> view?.setTag(
                    TAG_DRAWABLE_BOTTOM_ID,
                    attrs.getAttributeValue(i)
                )
            }
        }

        if (view is SimpleDraweeView) {
            view.dimOnDarkMode()
        }

        // 为每个Fragment的子view添加对应Fragment的Tag
        parent?.getTag(androidx.fragment.R.id.fragment_container_view_tag)?.let {
            view?.setTag(androidx.fragment.R.id.fragment_container_view_tag, it)
        }

        return view
    }

    override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
        return mAppCompatActivity.onCreateView(name, context, attrs)
    }

    companion object {
        var TAG_BACKGROUND_ID = R.string.background_id
        var TAG_TEXT_COLOR_ID = R.string.text_color_id
        var TAG_TEXT_COLOR_HINT_ID = R.string.text_color_hint_id
        var TAG_SRC_ID = R.string.src_id
        var TAG_DRAWABLE_LEFT_ID = R.string.drawable_left_id
        var TAG_DRAWABLE_TOP_ID = R.string.drawable_top_id
        var TAG_DRAWABLE_RIGHT_ID = R.string.drawable_right_id
        var TAG_DRAWABLE_BOTTOM_ID = R.string.drawable_bottom_id
    }

}