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

import android.content.Intent
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.RechargeDeviceBean
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.ActivityRechargeDeviceDetailCnBinding
import com.abbidot.tracker.deprecated.ui.activity.vm.RechargeDeviceViewModel
import com.abbidot.tracker.util.ViewUtil

/**
 * 设备充值国内详情
 */
class RechargeDeviceDetailCNActivity : BaseActivity<ActivityRechargeDeviceDetailCnBinding>(
        ActivityRechargeDeviceDetailCnBinding::inflate) {

    private val mRechargeDeviceViewModel: RechargeDeviceViewModel by viewModels()

    private var mDeviceId = ""

    //传递给下个页面需要用的值
    private var mRechargeDeviceBean: RechargeDeviceBean? = null

    override fun getTopBar() = mViewBinding.rechargeDeviceDetailCnBar.titleTopBar

    override fun initData() {
        super.initData()
        mDeviceId = intent.extras?.getString(ConstantString.DeviceId, "").toString()
        setTopBarTitle(mDeviceId)

        ViewUtil.instance.setShapeRadius(mViewBinding.rechargeDeviceDetailCnDevice.root, 6)

        mViewBinding.rechargeDeviceDetailCnHistory.apply {
            ViewUtil.instance.showQMUICommonListItemView(mContext!!, this, {
                val intent = Intent(mContext, RechargeHistoryCNActivity::class.java)
                intent.putExtra(ConstantString.DeviceId, mDeviceId)
                startActivity(intent)
            }, getString(R.string.txt_card_history))
            detailText = ""
        }

        setOnClickListenerViews(mViewBinding.btnRechargeDeviceDetailCnRecharge)

        mRechargeDeviceViewModel.getCards(this)
    }

    override fun liveDataObserve() {
        mRechargeDeviceViewModel.mDeviceCardLiveData.observe(this) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    val data = it.getOrNull()!!
                    setData(data)
                }
            })
        }

        //充值成功刷新天数
        XEventBus.observe(this, EventName.UpdateData + "PaymentSuccess") {
            mRechargeDeviceViewModel.getCards(this)
        }
    }

    private fun setData(list: MutableList<RechargeDeviceBean>) {
        for (i in 0 until list.size) {
            val rechargeDeviceBean = list[i]
            if (mDeviceId == rechargeDeviceBean.deviceId) {
                mRechargeDeviceBean = rechargeDeviceBean
                mTopBar?.setTitle(rechargeDeviceBean.deviceOutId)
                mViewBinding.rechargeDeviceDetailCnDevice.apply {
                    itemCardDeviceName.text = rechargeDeviceBean.deviceOutId
                    itemCardDayNumber.text = rechargeDeviceBean.surplusDays.toString()
                    val overdueDate = rechargeDeviceBean.overdueDate
                    val asOfString = if (TextUtils.isEmpty(overdueDate)) {
                        ""
                    } else {
                        String.format(getString(R.string.txt_card_list_end), overdueDate)
                    }
                    itemCardAsOf.text = asOfString
                    if (null == rechargeDeviceBean.pet) {
                        itemCardPetHead.appHeadImage.visibility = View.GONE
                        itemCardPetName.visibility = View.GONE
                    } else {
                        itemCardPetHead.appHeadImage.let {
                            it.visibility = View.VISIBLE
                            ViewUtil.instance.imageLoadUrl(it, rechargeDeviceBean.pet!!.imgurl)
                        }
                        itemCardPetName.let {
                            it.visibility = View.VISIBLE
                            it.text = rechargeDeviceBean.pet!!.petName
                        }
                    }
                }

                mViewBinding.rechargeDeviceDetailCnDescribe.let {
                    if (TextUtils.isEmpty(rechargeDeviceBean.period)) {
                        rechargeDeviceBean.period = "0"
                    }
                    val subscriptionPlanString =
                        String.format(getString(R.string.txt_surplus_day_how),
                                rechargeDeviceBean.surplusDays)
                    it.tvDescriptionSurplusDayContent.text = subscriptionPlanString
                    it.tvDescriptionExpiryDateContent.text = rechargeDeviceBean.overdueDate
                }
                break
            }
        }
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.btnRechargeDeviceDetailCnRecharge -> {
                if (null != mRechargeDeviceBean) {
                    val intent = Intent(mContext, RechargeAmountCNActivity::class.java)
                    intent.putExtra(ConstantString.RechargeDevice, mRechargeDeviceBean)
                    startActivity(intent)
                }
            }
        }
    }
}