package cn.yyxx.eyuangame.core.ui.floatview

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import cn.yyxx.eyuangame.core.entity.FloatFeature
import cn.yyxx.support.DensityUtils
import cn.yyxx.support.ResUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/30
 */
class FloatingBallMenu(val context: Context, private val callback: FloatingBallMenuCallback) {

    lateinit var contentView: View
    private lateinit var containerLayout: LinearLayout
    private lateinit var menuView: GridView
    private var items = mutableListOf<FloatingBallMenuItem>()

    init {
        initView()
        initData()
    }

    private fun initView() {
        contentView = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_floating_ball_menu", "layout"), null)

        containerLayout = contentView.findViewById(ResUtils.getResId(context, "yyxx_ll_container", "id"))
        containerLayout.gravity = Gravity.CENTER_VERTICAL or Gravity.RIGHT

        menuView = contentView.findViewById(ResUtils.getResId(context, "yyxx_gv_menu", "id"))
        //重新设置menuView宽高
        val menuWidth = DensityUtils.dip2px(context, callback.onInitMenuData().size * 45f)
        val menuParams = LinearLayout.LayoutParams(menuWidth, LinearLayout.LayoutParams.WRAP_CONTENT)
        menuParams.marginEnd = DensityUtils.dip2px(context, 5f)
        menuView.layoutParams = menuParams
    }

    private fun initData() {
        items = callback.onInitMenuData()
        menuView.numColumns = items.size
        menuView.adapter = FloatingBallMenuAdapter(context, items)
    }

    fun updateLayout(isLeftLocation: Boolean) {
        menuView.bringToFront()
        if (isLeftLocation) {
            containerLayout.gravity = Gravity.CENTER_VERTICAL or Gravity.RIGHT
        } else {
            containerLayout.gravity = Gravity.CENTER_VERTICAL or Gravity.LEFT
        }
//        updateViewBackground(isLeftLocation)
    }

    interface FloatingBallMenuCallback {

        fun onInitMenuData(): MutableList<FloatingBallMenuItem>

        fun onDismissMenu()

        fun onMenuItemClick(item: FloatingBallMenuItem, pos: Int)
    }

    data class FloatingBallMenuItem(val type: FloatFeature, val iconUrl: String, val url: String)

    private inner class FloatingBallMenuAdapter(val context: Context, val items: MutableList<FloatingBallMenuItem>) : BaseAdapter() {

        override fun getCount(): Int {
            return items.size
        }

        override fun getItem(position: Int): Any {
            return items[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val item = items[position]
            val view = View.inflate(context, ResUtils.getResId(context, "yyxx_floating_ball_item", "layout"), null)
            val ivIcon = view.findViewById<ImageView>(ResUtils.getResId(context, "yyxx_iv_icon", "id"))
            val tvDesc = view.findViewById<TextView>(ResUtils.getResId(context, "yyxx_tv_desc", "id"))
            when (item.type) {
                FloatFeature.FEATURE_MEMBER -> {
                    if (TextUtils.isEmpty(item.iconUrl)) {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_member_img", "drawable"))
                    } else {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_member_img", "drawable"))
                    }
                    tvDesc.text = ResUtils.getResString(context, "yyxx_tv_float_member_desc")
                }

                FloatFeature.FEATURE_GIF -> {
                    if (TextUtils.isEmpty(item.iconUrl)) {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_gif_img", "drawable"))
                    } else {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_gif_img", "drawable"))
                    }
                    tvDesc.text = ResUtils.getResString(context, "yyxx_tv_float_gif_desc")
                }

                FloatFeature.FEATURE_GM -> {
                    if (TextUtils.isEmpty(item.iconUrl)) {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_gm_img", "drawable"))
                    } else {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_gm_img", "drawable"))
                    }
                    tvDesc.text = ResUtils.getResString(context, "yyxx_tv_float_gm_desc")
                }

                FloatFeature.FEATURE_CHARGE -> {
                    if (TextUtils.isEmpty(item.iconUrl)) {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_charge_img", "drawable"))
                    } else {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_charge_img", "drawable"))
                    }
                    tvDesc.text = ResUtils.getResString(context, "yyxx_tv_float_charge_desc")
                }

                FloatFeature.FEATURE_INVITE -> {
                    if (TextUtils.isEmpty(item.iconUrl)) {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_invite_img", "drawable"))
                    } else {
                        ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_invite_img", "drawable"))
                    }
                    tvDesc.text = ResUtils.getResString(context, "yyxx_tv_float_invite_desc")
                }
            }
            view.setOnClickListener {
                callback.onMenuItemClick(item, position)
            }
            return view
        }

    }
}