package com.adealink.weparty.order.center

import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
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.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.module.order.Order
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.order.center.comp.OrderCenterDashboardComp
import com.adealink.weparty.order.center.comp.OrderCenterRecordComp
import com.adealink.weparty.order.center.comp.OrderCenterSkillComp
import com.adealink.weparty.order.center.viewmodel.OrderCenterViewModel
import com.adealink.weparty.order.databinding.ActivityOrderCenterBinding
import com.adealink.weparty.order.viewmodel.OrderViewModelFactory
import com.adealink.weparty.util.applyImmersive

@RouterUri(path = [Order.OrderCenter.PATH], desc = "接单中心")
class OrderCenterActivity : BaseActivity() {

    private val binding by viewBinding(ActivityOrderCenterBinding::inflate)

    private val playmateViewModel by fastLazy { PlaymateModule.getPlaymateViewModel(this) }

    private val orderCenterViewModel by viewModels<OrderCenterViewModel> { OrderViewModelFactory() }

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    override fun initComponents() {
        super.initComponents()
        OrderCenterDashboardComp(this, binding.vDashboard) {
            loadData()
        }.attach()
        OrderCenterSkillComp(this, binding.vSkills).attach()
        OrderCenterRecordComp(this, binding.vRecord).attach()
    }

    override fun loadData() {
        super.loadData()
        playmateViewModel?.getBusinessInfo()?.observe(this) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    orderCenterViewModel.updateBusinessData(rlt.data)
                }
            }
        }
    }


}