package com.abbidot.tracker.ui.activity.subscribe

import android.text.TextUtils
import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.constant.EventName
import com.abbidot.baselibrary.eventbus.XEventBus
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.SubscriptionsOrderBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityRequestRefundBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.vm.SubscriptionManageViewModel

class RequestRefundActivity :
    BaseActivity<ActivityRequestRefundBinding>(ActivityRequestRefundBinding::inflate) {

    private val mSubscriptionViewModel: SubscriptionManageViewModel by viewModels()

    //套餐
    private var mSetMealBean: SubscriptionsOrderBean? = null

    override fun getTopBar() = mViewBinding.ilRequestRefundTopBar.titleTopBar

    override fun initData() {
        super.initData()
        setTopBarTitle(R.string.txt_request_refund)
        setLeftBackImage(R.drawable.icon_white_back_svg)

        mSetMealBean = Util.getParcelableAdaptive(
            intent, ConstantString.SetMeal, SubscriptionsOrderBean::class.java
        )

        mViewBinding.apply {
            mSetMealBean?.let {
                val orderId = getString(R.string.txt_order_id) + " ${it.orderNum}"
                tvRequestRefundOrderId.text = orderId
                val planName = getString(R.string.txt_plan) + " ${it.mealName}"
                tvRequestRefundPlanName.text = planName
                val refundRefund = getString(R.string.txt_refund) + ": " + String.format(
                    mContext.getString(R.string.txt_money_unit), "${it.totalAmountWithTax}"
                )
                tvRequestRefundRefund.text = refundRefund
            }

            setOnClickListenerViews(btnRequestRefundRecharge)
        }
    }

    override fun liveDataObserve() {
        mSubscriptionViewModel.apply {
            mCommitRefundListLiveData.observe(this@RequestRefundActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        it.getOrNull()?.apply {
                            if (isCanRefund == ConstantInt.Type0) {
                                XEventBus.post(EventName.RefreshPackage)
                                showToast(R.string.txt_successful, true)
                            } else {
                                mSetMealBean?.let { meal ->
                                    mSubscriptionViewModel.refund(meal.orderNum)
                                }
                            }
                        }
                    }

                    override fun onRequestError(exceptionCode: String?) {
                        setButtonEnabled(mViewBinding.btnRequestRefundRecharge)
                    }
                })
            }

            mRefundLiveData.observe(this@RequestRefundActivity) {
                dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        XEventBus.post(EventName.RefreshPackage)
                        showToast(R.string.txt_successful, true)
                    }
                })
            }
        }
    }

    private fun commitRefund() {
        val typeMessage = mViewBinding.etRequestRefundTypeMessage.text.toString()
        if (TextUtils.isEmpty(typeMessage)) {
            showToast(R.string.txt_not_null)
            setButtonEnabled(mViewBinding.btnRequestRefundRecharge)
            return
        }
        mSetMealBean?.apply {
            mSubscriptionViewModel.commitRefund(
                orderNum,
                transactionId,
                (totalAmountWithTax * 100).toInt(),
                subscriptionId,
                mealName,
                typeMessage,
                deviceId,
                deviceOutId,
                payType
            )
        }
    }

    override fun onClick(v: View?) {
        setButtonEnabled(v!!)
        mViewBinding.apply {
            when (v) {
                btnRequestRefundRecharge -> commitRefund()
            }
        }
    }
}