package com.gh.common.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.common.base.BaseRecyclerViewHolder
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.utils.ImageUtils
import com.gh.gamecenter.common.utils.fromHtml
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.R
import com.gh.gamecenter.common.view.FixLinearLayoutManager
import com.gh.gamecenter.databinding.DialogReserveBinding
import com.gh.gamecenter.databinding.DialogReserveItemBinding
import com.gh.gamecenter.entity.SimpleGameEntity
import com.gh.gamecenter.mygame.MyGameActivity
import com.lightgame.adapter.BaseRecyclerAdapter

class ReserveDialog : BaseDialogFragment() {

    private lateinit var mReserveList: List<SimpleGameEntity>
    private var mDismissListener: (() -> Unit)? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val binding: DialogReserveBinding = DialogReserveBinding.inflate(layoutInflater, null, false)

        mReserveList = arguments?.getParcelableArrayList(RESERVE_LIST) ?: arrayListOf()

        binding.title.text = resources.getString(R.string.dialog_reserve_title, mReserveList.size).fromHtml()
        binding.more.visibility = if (mReserveList.size > 4) {
            View.VISIBLE
        } else View.GONE
        binding.more.setOnClickListener {
            val intent = MyGameActivity.getIntentWithConfig(requireContext(), 2)
            startActivity(intent)
            dismissAllowingStateLoss()
        }
        binding.recyclerView.layoutManager = if (mReserveList.size > 4) {
            GridLayoutManager(context, 4)
        } else {
            FixLinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
        }
        binding.recyclerView.adapter = object : BaseRecyclerAdapter<ReserveDialogItemViewHolder>(context) {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReserveDialogItemViewHolder {
                val inflate = mLayoutInflater.inflate(R.layout.dialog_reserve_item, parent, false)
                return ReserveDialogItemViewHolder(DialogReserveItemBinding.bind(inflate))
            }

            override fun getItemCount(): Int {
                return if (mReserveList.size > 4) 4 else mReserveList.size
            }

            override fun onBindViewHolder(holder: ReserveDialogItemViewHolder, position: Int) {
                val entity = mReserveList[position]
                ImageUtils.display(holder.binding.icon, entity.icon)
                holder.binding.gameNameTv.text = entity.name
                holder.itemView.setOnClickListener {
                    GameDetailActivity.startGameDetailActivity(mContext, entity.id, "(预约弹窗)")
                    dismissAllowingStateLoss()
                }
            }
        }

        dialog?.setCanceledOnTouchOutside(true)
        return binding.root
    }


    fun setOnDismissListener(dismissListener: () -> Unit) {
        mDismissListener = dismissListener
    }

    override fun onDestroy() {
        super.onDestroy()
        mDismissListener?.invoke()
    }

    companion object {
        const val RESERVE_LIST = "reserve_list"

        @JvmStatic
        fun getInstance(reserveList: List<SimpleGameEntity>) = ReserveDialog().apply {
            arguments = Bundle()
            arguments?.putParcelableArrayList(RESERVE_LIST, ArrayList(reserveList))
        }
    }
}

class ReserveDialogItemViewHolder(val binding: DialogReserveItemBinding) : BaseRecyclerViewHolder<Any>(binding.root)