package com.abbidot.tracker.deprecated.ui.activity.profile.recharge

import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
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.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivityPaymentResultBinding
import com.abbidot.tracker.deprecated.ui.fragment.profile.PaymentFailFragment
import com.abbidot.tracker.deprecated.ui.fragment.profile.PaymentSuccessFragment

/**
 * 付款结果显示
 */
class PaymentResultActivity :
    BaseActivity<ActivityPaymentResultBinding>(ActivityPaymentResultBinding::inflate) {

    var mAmount = 0.0

    //成功支付的标识
    private var isPaymentSuccess = false

    override fun getTopBar() = mViewBinding.paymentResultBar.titleTopBar

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

        var type = ConstantInt.Type1
        intent.extras?.apply {
            type = getInt(ConstantString.Type)
            mAmount = getDouble(ConstantString.Amount, 0.0)
        }

        if (type == ConstantInt.Type1) {
            isPaymentSuccess = true
            switchFragment(PaymentSuccessFragment.newInstance(mContext), 0)
        } else {
            switchFragment(PaymentFailFragment.newInstance(mContext), 1)
        }
    }

    override fun leftBackOnClick() {
        XEventBus.post(EventName.PayBack)
        if (isPaymentSuccess) {
            XEventBus.post(EventName.UpdateData + "PaymentSuccess")
        }
        super.leftBackOnClick()
    }

    fun switchFragment(fragment: Fragment, index: Int) {
        supportFragmentManager.commit {
            if (fragment.isAdded) {
                show(fragment)
            } else {
                add(R.id.payment_result_frame, fragment)
            }
        }
    }
}