package com.adealink.weparty.playmate.saywelcome.fragment

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.util.naviBarHeight
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListDiffUtil
import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.FragmentPlaymateWelcomeListBinding
import com.adealink.weparty.playmate.saywelcome.adapter.SayWelcomeItemViewBinder
import com.adealink.weparty.playmate.saywelcome.data.SayWelcomeItemData
import com.adealink.weparty.playmate.saywelcome.viewmodel.WelcomePageViewModel
import com.adealink.weparty.playmate.saywelcome.viewmodel.WelcomeViewModel
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 WelcomeListFragment : BaseFragment(R.layout.fragment_playmate_welcome_list) {

    companion object {
        const val TAG = "WelcomeListFragment"
    }

    private val binding by viewBinding(FragmentPlaymateWelcomeListBinding::bind)
    private val pageViewModel by activityViewModels<WelcomePageViewModel>()
    private val welcomeViewModel by activityViewModels<WelcomeViewModel>()
    private val listAdapter by fastLazy { MultiTypeListAdapter<SayWelcomeItemData>(BaseListDiffUtil()) }

    override fun initViews() {
        super.initViews()
        binding.btnNew.updateLayoutParams<ConstraintLayout.LayoutParams> {
            bottomMargin = activity.naviBarHeight() + 12.dp()
        }
        binding.btnNew.onClick {
            pageViewModel.clickNewWelcome()
        }
        binding.vRefresh.setEnableRefresh(true)
        binding.vRefresh.setEnableLoadMore(false)
        binding.vRefresh.setEnableAutoLoadMore(false)
        binding.vRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                welcomeViewModel.loadWelcomeInfo()
            }


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

        listAdapter.register(
            SayWelcomeItemViewBinder(
                onItemClick = { item, position ->
                    goWelcomeSetting(item, position)
                }
            ))
        binding.rvList.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvList.adapter = listAdapter
        binding.rvList.addItemDecoration(
            VerticalSpaceItemDecoration(
                12.dp(),
                firstSpaceHeight = 12.dp(),
                lastSpaceHeight = activity.naviBarHeight() + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt() + 24.dp()
            )
        )
    }

    override fun observeViewModel() {
        super.observeViewModel()
        welcomeViewModel.welcomeListRltLD.observeWithoutCache(this) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            showFailedToast(rlt)
        }
        welcomeViewModel.welcomeListLD.observe(this) { list ->
            listAdapter.submitList(list)
        }
    }

    override fun loadData() {
        super.loadData()
        welcomeViewModel.loadWelcomeInfo()
    }

    private fun goWelcomeSetting(item: SayWelcomeItemData, position: Int) {
        val act = activity ?: return
        Router.build(act, Playmate.SayWelcomeSetting.PATH)
            .putExtra(
                Playmate.SayWelcomeSetting.EXTRA_WELCOME_DATA,
                item.data.apply {
                    itemPosition = position
                }
            )
            .start()
    }

}