package com.adealink.weparty.profile.me.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.viewmodel.activityViewModels
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseViewComponent
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.CommonButton
import com.adealink.weparty.module.order.Order
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.module.playmate.data.PlaymateBusinessData
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.profile.data.UserInfo
import com.adealink.weparty.profile.databinding.LayoutMeOrderCenterBinding
import com.adealink.weparty.profile.viewmodel.ProfileViewModel
import com.adealink.weparty.profile.viewmodel.ProfileViewModelFactory
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.R as APP_R

class OrderCenterComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutMeOrderCenterBinding
) : BaseViewComponent(lifecycleOwner) {

    private val profileViewModel by activityViewModels<ProfileViewModel> {
        ProfileViewModelFactory()
    }

    private val playmateViewModel by fastLazy { PlaymateModule.getPlaymateViewModel(requireActivity()) }
    private var businessData: PlaymateBusinessData? = null

    override fun initViews() {
        super.initViews()
        binding.root.onClick {
            goOrderCenter()
        }
    }

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

    override fun observeViewModel() {
        profileViewModel.userInfoLD.observe(viewLifecycleOwner) { user ->
            updateUI(user)
        }
    }

    override fun loadData() {
        updateUI(ProfileModule.getMyUserInfo())
        playmateViewModel?.getBusinessInfo()?.observe(viewLifecycleOwner) { rlt ->
            businessData = (rlt as? Rlt.Success)?.data
            updateBusiness(businessData)
        }
    }


    private fun updateUI(userInfo: UserInfo?) {
        userInfo ?: return
        if (userInfo.isPlaymate()) {
            binding.root.show()
        } else {
            binding.root.gone()
        }
    }

    private fun updateBusiness(business: PlaymateBusinessData?) {
        binding.tvIncome.text = formatNumberStr(business?.weekIncome ?: 0f)
        binding.tvExposure.text = formatNumberStr(business?.todayExposure ?: 0)
        binding.tvUserPush.text = formatNumberStr(business?.userPush ?: 0)

        if (business == null) {
            binding.btnService.buttonType = CommonButton.BUTTON_NORMAL_1
            binding.btnService.isEnabled = false
            binding.tvService.text = getCompatString(APP_R.string.common_accept_order_open_service)
            binding.tvServiceTips.text =
                getCompatString(APP_R.string.common_accept_order_open_service_tips)
        } else if (business.businessOpen) {
            binding.btnService.buttonType = CommonButton.BUSINESS_OPEN
            binding.btnService.isEnabled = true
            binding.tvService.text =
                getCompatString(APP_R.string.common_accept_order_opening_service)
            binding.tvServiceTips.text =
                getCompatString(APP_R.string.common_accept_order_opening_service_tips)
            binding.btnService.onClick {
                switchBusinessOpen(false)
            }
        } else {
            binding.btnService.buttonType = CommonButton.BUTTON_NORMAL_1
            binding.btnService.isEnabled = true
            binding.tvService.text = getCompatString(APP_R.string.common_accept_order_open_service)
            binding.tvServiceTips.text =
                getCompatString(APP_R.string.common_accept_order_open_service_tips)
            binding.btnService.onClick {
                switchBusinessOpen(true)
            }
        }
    }

    private fun switchBusinessOpen(open: Boolean) {
        showLoading()
        playmateViewModel?.openBusiness(open)?.observe(viewLifecycleOwner) { rlt ->
            dismissLoading()
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    loadData()
                }
            }
        }
    }

    private fun goOrderCenter() {
        val act = activity ?: return
        Router.build(act, Order.OrderCenter.PATH).start()
    }

}