package com.adealink.weparty.order.center.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.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.CommonButton
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.module.playmate.data.PlaymateBusinessData
import com.adealink.weparty.module.wallet.Wallet
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.order.center.viewmodel.OrderCenterViewModel
import com.adealink.weparty.order.databinding.LayoutOrderCenterDashboardBinding
import com.adealink.weparty.order.viewmodel.OrderViewModelFactory
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.R as APP_R

class OrderCenterDashboardComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutOrderCenterDashboardBinding,
    val onLoadData: () -> Unit
) : BaseViewComponent(lifecycleOwner) {

    private val playmateViewModel by fastLazy { PlaymateModule.getPlaymateViewModel(requireActivity()) }
    private val orderCenterViewModel by activityViewModels<OrderCenterViewModel> { OrderViewModelFactory() }

    override fun initViews() {
        super.initViews()
        binding.clIncome.onClick {
            goIncome()
        }
        binding.clUserPush.onClick {
            goUserPush()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        orderCenterViewModel.businessDataLD.observe(viewLifecycleOwner) { business ->
            updateBusiness(business)
        }
    }

    private fun updateBusiness(business: PlaymateBusinessData?) {
        binding.tvIncome.text = formatNumberStr(business?.weekIncome ?: 0f, false)
        binding.tvExposure.text = formatNumberStr(business?.todayExposure ?: 0, false)
        binding.tvUserPush.text = formatNumberStr(business?.userPush ?: 0, false)
        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 -> {
                    onLoadData.invoke()
                }
            }
        }
    }


    private fun goIncome() {
        val act = activity ?: return
        Router.build(act, Wallet.Recharge.PATH)
            .putExtra(Wallet.Common.EXTRA_CURRENCY, Currency.BEAN.type)
            .start()
    }

    private fun goUserPush() {
        val act = activity ?: return
        Router.build(act, Playmate.UserPush.PATH).start()
    }

}