package com.adealink.weparty.order

import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
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.VerticalSpaceItemDecoration
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.order.Order
import com.adealink.weparty.order.adapter.PlaymateListItemViewBinder
import com.adealink.weparty.order.data.PlaymateOrderListItemData
import com.adealink.weparty.order.databinding.ActivityPlaymateOrderListBinding
import com.adealink.weparty.order.viewmodel.OrderViewModelFactory
import com.adealink.weparty.order.viewmodel.PlaymateOrderViewModel
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 = [Order.PlaymateList.PATH], desc = "陪玩列表")
class PlaymateOrderListActivity : BaseActivity() {

    companion object {
        private const val TAG = "PlaymateOrderListActivity"
    }

    private val binding by viewBinding(ActivityPlaymateOrderListBinding::inflate)

    private val viewModel by viewModels<PlaymateOrderViewModel> { OrderViewModelFactory() }

    private val listAdapter by fastLazy { MultiTypeListAdapter<PlaymateOrderListItemData>() }

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = this@PlaymateOrderListActivity.statusBarHeight()
        }

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


            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMoreOrderList()
            }
        })

        listAdapter.register(PlaymateListItemViewBinder())
        binding.rvOrder.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        binding.rvOrder.adapter = listAdapter
        binding.rvOrder.addItemDecoration(
            VerticalSpaceItemDecoration(
                20.dp(),
                firstSpaceHeight = 16.dp()
            )
        )
    }

    override fun loadData() {
        super.loadData()
        viewModel.pullOrderList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.orderListLD.observe(this) {
            listAdapter.submitList(it)
            if (it.isNullOrEmpty()) {
                binding.rvOrder.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                binding.rvOrder.show()
                binding.vErrorView.gone()
            }
        }
        viewModel.orderListRltLD.observe(this) { rlt ->
            binding.vRefresh.finishRefresh()
            binding.vRefresh.finishLoadMore()
            showFailedToast(rlt)
        }
    }

}