package cn.yyxx.eyuancomm.impl.channel.huawei.floatview

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import cn.yyxx.support.DensityUtils
import cn.yyxx.support.ResUtils

/**
 * @author #Suyghur.
 * Created on 2021/06/30
 */
class FloatBallMenu(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<FloatBallMenuItem>()
    private var hasReverse = false

    init {
        initView()
        initData()
    }

    private fun initView() {
        contentView = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_float_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 * 48f)
        val menuParams = LinearLayout.LayoutParams(menuWidth, LinearLayout.LayoutParams.WRAP_CONTENT)
        menuParams.marginEnd = DensityUtils.dip2px(context, 2.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) {
            if (hasReverse) {
                items.reverse()
                hasReverse = false
            }
            containerLayout.gravity = Gravity.CENTER_VERTICAL or Gravity.RIGHT
        } else {
            items.reverse()
            hasReverse = true
            containerLayout.gravity = Gravity.CENTER_VERTICAL or Gravity.LEFT
        }
    }

    interface FloatingBallMenuCallback {

        fun onInitMenuData(): MutableList<FloatBallMenuItem>

        fun onDismissMenu()

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

    data class FloatBallMenuItem(val type: FloatFeature)

    private inner class FloatingBallMenuAdapter(val context: Context, val items: MutableList<FloatBallMenuItem>) : 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_float_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_SWITCH -> {
                    ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_switch_img", "drawable"))
                    tvDesc.text = "切換賬號"
                }
                FloatFeature.FEATURE_LINK -> {
                    ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_link_img", "drawable"))
                    tvDesc.text = "賬號關聯"
                }
                FloatFeature.FEATURE_CLOSE -> {
                    ivIcon.setBackgroundResource(ResUtils.getResId(context, "yyxx_float_close_img", "drawable"))
                    tvDesc.text = "隱藏"
                }

            }
            view.setOnClickListener {
                callback.onMenuItemClick(item, position)
            }
            return view
        }
    }
}