package com.adealink.weparty.profile.search.page

import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
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.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.toEnterRoomInfo
import com.adealink.weparty.network.data.NoMoreDataError
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.FragmentSearchRoomBinding
import com.adealink.weparty.profile.search.adapter.SearchRoomItemViewBinder
import com.adealink.weparty.profile.search.data.SearchRoomResultItemData
import com.adealink.weparty.profile.search.viewmodel.SearchRoomViewModel
import com.adealink.weparty.profile.search.viewmodel.SearchViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.adealink.weparty.R as APP_R

class SearchRoomFragment : BaseFragment(R.layout.fragment_search_room),
    SearchRoomItemViewBinder.OnRoomResultClickListener {

    private val binding by viewBinding(FragmentSearchRoomBinding::bind)

    private val resultAdapter by fastLazy { MultiTypeListAdapter<SearchRoomResultItemData>() }
    private val searchViewModel by activityViewModels<SearchViewModel>()
    private val searchRoomViewModel by viewModels<SearchRoomViewModel>()
    override fun initViews() {
        super.initViews()
        binding.vRefresh.setEnableRefresh(true)
        binding.vRefresh.setEnableLoadMore(false)
        binding.vRefresh.setEnableAutoLoadMore(true)
        binding.vRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                searchRoomViewModel.reSearch()
            }


            override fun onLoadMore(refreshLayout: RefreshLayout) {
                searchRoomViewModel.searchMore()
            }
        })

        resultAdapter.register(SearchRoomItemViewBinder(this))
        binding.rvResult.layoutManager = GridLayoutManager(context, 2)
        binding.rvResult.adapter = resultAdapter
        binding.rvResult.addItemDecoration(
            GridSpacingItemDecoration(2, 13.dp(), 16.dp(), false)
        )
    }

    override fun observeViewModel() {
        super.observeViewModel()
        searchViewModel.keywordLD.observe(viewLifecycleOwner) {
            searchRoomViewModel.search(it)
        }
//        searchViewModel.followChangedLD.observeWithoutCache(viewLifecycleOwner) {
//            searchUserViewModel.notifyFollowChanged(it.first, it.second)
//        }
        searchRoomViewModel.searchRltLD.observeWithoutCache(this) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            binding.vRefresh.setEnableLoadMore(searchRoomViewModel.hasMoreData())
            if (rlt is Rlt.Failed) {
                if (rlt.error is NoMoreDataError) {
                    return@observeWithoutCache
                }
                showFailedToast(rlt)
            }
        }
        searchRoomViewModel.searchResultLD.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 goRoom(roomInfo: RoomInfo) {
        val act = activity ?: return
        Router.build(act, Room.Room.PATH)
            .putExtra(Room.Room.EXTRA_ENTER_ROOM_INFO, roomInfo.toEnterRoomInfo())
            .start()
    }

}