package com.adealink.weparty.room.list

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.naviBarHeight
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.room.Room
import com.adealink.weparty.module.room.data.RoomInfo
import com.adealink.weparty.module.room.data.RoomType
import com.adealink.weparty.module.room.data.toEnterRoomInfo
import com.adealink.weparty.network.data.NoMoreDataError
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.FragmentHomeRoomListBinding
import com.adealink.weparty.room.list.adapter.RoomItemViewBinder
import com.adealink.weparty.room.list.data.RoomListItemData
import com.adealink.weparty.room.list.viewmodel.RoomListViewModel
import com.adealink.weparty.util.applyImmersive
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Room.HomeList.PATH],
    desc = "首页房间列表"
)
class RoomHomeListFragment : BaseFragment(R.layout.fragment_home_room_list), RoomItemViewBinder.OnRoomClickListener {

    private val binding by viewBinding(FragmentHomeRoomListBinding::bind)

    private val resultAdapter by fastLazy { MultiTypeListAdapter<RoomListItemData>() }
    private val roomViewModel by viewModels<RoomListViewModel>()
    override fun initViews() {
        super.initViews()
        activity.applyImmersive { top ->
            binding.tvTitle.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }

        binding.vRefresh.setEnableRefresh(true)
        binding.vRefresh.setEnableLoadMore(false)
        binding.vRefresh.setEnableAutoLoadMore(true)
        binding.vRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                roomViewModel.reload()
            }


            override fun onLoadMore(refreshLayout: RefreshLayout) {
                roomViewModel.loadMore()
            }
        })

        resultAdapter.register(RoomItemViewBinder(this))
        binding.rvResult.layoutManager = GridLayoutManager(context, 2)
        binding.rvResult.adapter = resultAdapter
        binding.rvResult.addItemDecoration(
            GridSpacingItemDecoration(
                2, 13.dp(), 14.dp(),
                false,
                extraTop = 0,
                extraBottom = activity.naviBarHeight() + getCompatDimension(APP_R.dimen.main_bottom_bar_height).toInt()
            )
        )
    }

    override fun observeViewModel() {
        super.observeViewModel()
        roomViewModel.listRltLD.observeWithoutCache(this) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            binding.vRefresh.setEnableLoadMore(roomViewModel.hasMoreData())
            if (rlt is Rlt.Failed) {
                if (rlt.error is NoMoreDataError) {
                    return@observeWithoutCache
                }
                showFailedToast(rlt)
            }
        }
        roomViewModel.roomItemListLD.observeWithoutCache(this) { list ->
            if (list.isEmpty()) {
                binding.rvResult.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                binding.rvResult.show()
                binding.vErrorView.gone()
            }
            resultAdapter.submitList(list, true)
        }
    }

    override fun loadData() {
        super.loadData()
        roomViewModel.load(listOf(RoomType.DISPATCH_CENTER.type))
    }

    override fun goRoom(roomInfo: RoomInfo) {
        val act = activity ?: return
        Router.build(act, Room.Room.PATH)
            .putExtra(Room.Room.EXTRA_ENTER_ROOM_INFO, roomInfo.toEnterRoomInfo())
            .start()
    }


}