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

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.bean.SetMealBean
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.databinding.ActivitySubscriptionDetailBinding
import com.abbidot.tracker.util.Util
import com.abbidot.tracker.vm.CountDownTimerViewModel

class SubscriptionDetailActivity :
    BaseActivity<ActivitySubscriptionDetailBinding>(ActivitySubscriptionDetailBinding::inflate) {

    private val mCountDownTimerViewModel: CountDownTimerViewModel by viewModels()

    private var mSetMealBean: SetMealBean? = null

    override fun getTopBar() = mViewBinding.ilSubscribeDetailTopBar.titleTopBar

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

        intent.extras?.apply {
            mSetMealBean =
                Util.getParcelableAdaptive(intent, ConstantString.SetMeal, SetMealBean::class.java)
        }

        mViewBinding.apply {
            mSetMealBean?.let {
                tvSubscribeDetailDeviceOutId.text = it.mealId
                tvSubscribeDetailPlanType.text = if (it.hotType == ConstantInt.Type0) {
                    btnSubscribeDetailEnd.visibility = View.VISIBLE
                    ilSubscribeDetailCountDown.root.visibility = View.GONE
                    btnSubscribeDetailRecharge.visibility = View.GONE
                    getString(R.string.txt_auto_renew)
                } else {
                    btnSubscribeDetailRecharge.visibility = View.VISIBLE
                    ilSubscribeDetailCountDown.root.visibility = View.VISIBLE
                    btnSubscribeDetailEnd.visibility = View.GONE
                    mCountDownTimerViewModel.startCountDown(
                        (it.createdDate.toLong() - System.currentTimeMillis()) / 1000, 60
                    )
                    it.name
                }
                tvSubscribeDetailRenewYear.text =
                    String.format(mContext.getString(R.string.txt_renew_year), "${it.accountFee}")
                tvSubscribeDetailRenewsOn.text =
                    Utils.formatTime(it.createdDate.toLong(), Utils.DATE_FORMAT_PATTERN_EN6)
            }
            setOnClickListenerViews(btnSubscribeDetailRecharge, btnSubscribeDetailEnd)
        }
    }

    override fun liveDataObserve() {
        mCountDownTimerViewModel.mCountDowningLiveData.observe(this) {
            mViewBinding.ilSubscribeDetailCountDown.apply {
                val times = Utils.getSecondToDayHourMin(it)
                tvAddSuccessDeviceExpiresDays.text = times[0]
                tvAddSuccessDeviceExpiresHours.text = times[1]
                tvAddSuccessDeviceExpiresMin.text = times[2]
            }
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnSubscribeDetailEnd -> finish()
                btnSubscribeDetailRecharge -> startActivity(
                    Intent(
                        mContext, SubscriptionPlanActivity::class.java
                    )
                )
            }
        }
    }
}