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

import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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.isSuccess
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListItemData
import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.profile.Profile
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.FragmentSearchAllBinding
import com.adealink.weparty.profile.relation.viewmodel.FollowViewModel
import com.adealink.weparty.profile.search.adapter.SearchAllRoomItemViewBinder
import com.adealink.weparty.profile.search.adapter.SearchAllUserItemViewBinder
import com.adealink.weparty.profile.search.adapter.SearchRoomItemViewBinder
import com.adealink.weparty.profile.search.adapter.SearchUserItemViewBinder
import com.adealink.weparty.profile.search.viewmodel.SearchAllViewModel
import com.adealink.weparty.profile.search.viewmodel.SearchViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class SearchAllFragment : BaseFragment(R.layout.fragment_search_all),
    SearchUserItemViewBinder.OnUserResultClickListener,
    SearchRoomItemViewBinder.OnRoomResultClickListener {

    private val binding by viewBinding(FragmentSearchAllBinding::bind)

    private val resultAdapter by fastLazy { MultiTypeListAdapter<BaseListItemData>() }

    private val searchViewModel by activityViewModels<SearchViewModel>()
    private val searchAllViewModel by viewModels<SearchAllViewModel>()
    private val followViewModel by activityViewModels<FollowViewModel>()


    override fun initViews() {
        super.initViews()
        binding.vRefresh.setEnableRefresh(true)
        binding.vRefresh.setEnableLoadMore(false)
        binding.vRefresh.setEnableAutoLoadMore(false)
        binding.vRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                searchAllViewModel.reSearch()
            }


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

        resultAdapter.register(SearchAllUserItemViewBinder(this) {
            searchViewModel.clickMoreUser()
        })
        resultAdapter.register(SearchAllRoomItemViewBinder(this) {
            searchViewModel.clickMoreRoom()
        })

        binding.rvResult.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvResult.adapter = resultAdapter
        binding.rvResult.addItemDecoration(
            VerticalSpaceItemDecoration(20.dp())
        )
    }

    override fun observeViewModel() {
        super.observeViewModel()
        searchViewModel.keywordLD.observe(viewLifecycleOwner) {
            searchAllViewModel.search(it)
        }
        searchViewModel.followChangedLD.observeWithoutCache(viewLifecycleOwner) {
            searchAllViewModel.notifyFollowChanged(it.first, it.second)
        }
        searchAllViewModel.searchRltLD.observeWithoutCache(this) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
        }
        searchAllViewModel.searchResultLD.observeWithoutCache(this) { list ->
            resultAdapter.submitList(list, true)
        }
    }

    override fun goProfile(uid: String) {
        val act = activity ?: return
        Router.build(act, Profile.UserProfile.PATH)
            .putExtra(Profile.Common.EXTRA_UID, uid)
            .start()
    }

    override fun follow(uid: String) {
        followViewModel.follow(uid).observe(this) {
            if (it.isSuccess) {
                searchViewModel.notifyFollowChanged(uid, true)
            } else {
                showFailedToast(it)
            }
        }
    }

    override fun unFollow(uid: String) {
        followViewModel.unFollow(uid).observe(this) {
            if (it.isSuccess) {
                searchViewModel.notifyFollowChanged(uid, false)
            } else {
                showFailedToast(it)
            }
        }
    }

    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()
    }

}