package cn.yyxx.eyuangame.core.impl.floating

import android.app.Activity
import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import cn.yyxx.eyuangame.base.utils.Logger
import cn.yyxx.support.DensityUtils
import cn.yyxx.support.ResUtils
import cn.yyxx.support.ui.DragViewLayout

/**
 * @author #Suyghur.
 * Created on 2021/06/28
 */
class FloatCenter(val activity: Activity, private val features: MutableList<FloatCenterItem>) : DragViewLayout(activity), View.OnClickListener {

    private lateinit var view: LinearLayout

    //    private lateinit var leftView: View
//    private lateinit var rightView: View
//    private lateinit var leftMenu: LinearLayout
//    private lateinit var rightMenu: LinearLayout
    private lateinit var menu: LinearLayout
    private lateinit var icon: ImageView
    private var menuWidth = 0
    private var menuHeight = 0
    private var iconWidth = 0
    private var iconHeight = 0
    private var isShowMenu = false

    init {
        isClickable = true
//        val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
//        params.height = DensityUtils.dip2px(activity, 40.0f)
//        params.width = DensityUtils.dip2px(activity, 40.0f)
        initView(activity)
    }


    private fun initView(context: Context) {
//        view = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_float_menu", "layout"), null)
//        rightView = LayoutInflater.from(context).inflate(ResUtils.getResId(context, "yyxx_float_right_menu", "layout"), null)

//        val imageView = ImageView(activity)
//        imageView.setImageResource(ResUtils.getResId(activity, "yyxx_float_icon_img", "drawable"))
//        imageView.setOnClickListener(this)
        iconWidth = DensityUtils.dip2px(context, 40f)
        iconHeight = DensityUtils.dip2px(context, 40f)
        Logger.d("width : ${features[0].width}")
        menuWidth = DensityUtils.dip2px(context, 30f) * features.size + iconWidth / 2
        menuHeight = iconHeight

        view = LinearLayout(context)
        view.apply {
            orientation = LinearLayout.HORIZONTAL
            val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            layoutParams = params
        }

        icon = ImageView(context)
        icon.apply {
            setBackgroundResource(ResUtils.getResId(context, "yyxx_float_logo_img", "drawable"))
            val params = LayoutParams(iconWidth, iconHeight)
            layoutParams = params
            setOnClickListener(this@FloatCenter)
        }

        menu = LinearLayout(context)
        menu.apply {
            orientation = LinearLayout.HORIZONTAL
//            setBackgroundColor(Color.TRANSPARENT)
            val params = LayoutParams(menuWidth, menuHeight)
            layoutParams = params
        }

//        leftMenu = view.findViewById(ResUtils.getResId(context, "yyxx_left_menu", "id"))
//        leftMenu.layoutParams = LayoutParams(menuWidth, menuHeight)
//        leftVview.visibility = View.GONE

//        rightMenu = view.findViewById(ResUtils.getResId(context, "yyxx_right_menu", "id"))
//        rightMenu.layoutParams = LayoutParams(menuWidth, menuHeight)
//        rightView.visibility = View.GONE

//        icon = view.findViewById(ResUtils.getResId(context, "yyxx_iv_icon", "id"))
////        icon = ImageView(context)
//        icon.apply {
//            setBackgroundResource(ResUtils.getResId(context, "yyxx_float_logo_img", "drawable"))
//            val params = LayoutParams(iconWidth, iconHeight)
//            layoutParams = params
//            setOnClickListener(this@FloatCenter)
//        }
//        removeAllViews()
//        addView(leftView)
//        addView(rightView)
        view.addView(icon)
        addView(view)

//        setDragViewLayoutListener(object :DragViewLayoutListener {
//
//        })
//        setDragViewLayoutListener(object :Drag)
    }

    override fun updateFloatPosition(p0: Boolean) {
        super.updateFloatPosition(p0)
//        if (!isDrag) {
//            Logger.d("updateFloatPosition isRight : $isRightFloat")
//            if (isRightFloat) {
//                val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
//                params.marginEnd = DensityUtils.dip2px(activity, 65f)
//                icon.layoutParams = params
//                rightMenu.visibility = View.VISIBLE
//            }
//        }
    }

    override fun startAnim() {
        super.startAnim()
//        Logger.d("startAnim mWidth $mWidth")
//        mWidth = iconWidth
//        if (isShowMenu) {
//            icon.apply {
//                val params = LayoutParams(iconWidth, iconHeight)
//                layoutParams = params
//            }
//            leftMenu.visibility = View.GONE
//            leftMenu.visibility = View.GONE
//            updateFloatPosition(false)
//            isShowMenu = false
//        }
    }


    override fun onClick(v: View?) {
        v?.apply {
            Logger.d("FloatCenter onClick , isRight : $isRightFloat")
            if (isDrag) {
                return
            }
//            mWidth = iconWidth
//            icon.apply {
//                val params = LayoutParams(iconWidth, iconHeight)
//                layoutParams = params
//            }
//            updateFloatPosition(false)
            if (isShowMenu) {
//                leftMenu.visibility = View.GONE
//                rightMenu.visibility = View.GONE
                mWidth = iconWidth
                view.removeAllViews()
                view.addView(icon)
                updateViewLayout(view, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
                isShowMenu = false
                updateFloatPosition(false)
                return
            }
            if (isRightFloat) {
//                leftMenu.visibility = View.GONE
//                rightMenu.visibility = View.VISIBLE
//                val rootParam = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
                mWidth = iconWidth
                view.removeAllViews()
                removeAllViews()
                view.addView(menu)
                view.addView(icon)
                addView(view)
                updateViewLayout(view, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
                isShowMenu = true
                updateFloatPosition(false)
            } else {
                mWidth = menuWidth
                view.removeAllViews()
                removeAllViews()
                view.addView(icon)
                view.addView(menu)
                addView(view)
                updateViewLayout(view, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
//                val params = LayoutParams(iconWidth, iconWidth)
//                icon.layoutParams = params
//                val menuParams = LayoutParams(menuWidth, menuHeight)
//                leftMenu.layoutParams = menuParams
//                leftMenu.visibility = View.VISIBLE
//                rightMenu.visibility = View.GONE
//                val rootParam = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
                isShowMenu = true
                updateFloatPosition(false)
            }
        }
    }
}