package com.swago.room.game

import android.graphics.Rect
import android.os.Bundle
import android.view.Gravity
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.swago.baseswago.baseroom.SwagoRoomManager
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.model.live.game.GameModel
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.LogUtil
import com.swago.room.adapter.GameAdapter
import com.swago.room.bean.UserRoomModel
import com.swago.room.databinding.DialogGameListBinding

class GameListDialog : BaseXDFragment<DialogGameListBinding>() {

    init {
        setCanCancel(true)
        setGravity(Gravity.BOTTOM)
        setDimAmount(0f)
    }

    var openDialogFun: (() -> Unit)? = null

    companion object {
        fun newInstance(data: ArrayList<GameModel>): GameListDialog {
            val args = Bundle()
            args.putParcelableArrayList("data", data)
            val fragment = GameListDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        arguments?.let {
            val data: ArrayList<GameModel>? = it.getParcelableArrayList("data")
            data?.let {
                activity?.let { activity ->
                    binding.rvGameList.layoutManager = GridLayoutManager(activity, 4)
                    val adapter = GameAdapter()
                    binding.rvGameList.addItemDecoration(GameGridItemDecoration())
                    binding.rvGameList.adapter = adapter
                    adapter.setNewData(it)

                    adapter.setOnItemClickListener { _, _, position ->
                        SwagoRoomManager.iRoomInfo?.let {  iRoomInfo ->
                            if (iRoomInfo is UserRoomModel && it.size>position) {
                                iRoomInfo.roomModel?.game_api_url = it[position].game_api_url
                                iRoomInfo.roomModel?.game_down_url = it[position].game_down_url
                                iRoomInfo.roomModel?.game_type = it[position].game_type
                                iRoomInfo.roomModel?.game_version = it[position].game_version
                                iRoomInfo.roomModel?.game_icon_url = it[position].game_icon_url
                                iRoomInfo.roomModel?.game_url = it[position].game_url
                                iRoomInfo.roomModel?.is_full_game = it[position].is_full_game
                                openDialogFun?.invoke()
                                dismissAllowingStateLoss()
                            }
                        }
                    }
                }
            }
        }
    }

    override fun initLiveData() {

    }


    inner class GameGridItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition

            val margin = DpPxUtil.dip2px(20f)
            val middle = DpPxUtil.dip2px(16f)
            when (itemPosition % 4) {
                0 -> {
                    outRect.set(margin, 0, middle, middle)
                }
                1 -> {
                    outRect.set(middle, 0, middle, middle)
                }
                2 -> {
                    outRect.set(middle, 0, middle, middle)
                }
                3 -> {
                    outRect.set(middle, 0, margin, middle)
                }
            }
        }
    }
}