package com.abbidot.tracker.dialog

import android.content.Intent
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.abbidot.baselibrary.constant.MMKVKey
import com.abbidot.baselibrary.util.MMKVUtil
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseActivity
import com.abbidot.tracker.base.BaseDialog
import com.abbidot.tracker.constant.ConstantInt
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.DialogPackageUpgradeBinding
import com.abbidot.tracker.ui.activity.subscribe.ChoosePlanActivity
import com.abbidot.tracker.vm.SubscriptionManageViewModel

/**
 *Created  by .yzq on 2024/9/27/027.
 * @link
 * @description:升级套餐弹窗
 */
class PackageUpgradeDialog(activity: BaseActivity<*>, deviceId: String) :
    BaseDialog<DialogPackageUpgradeBinding>(DialogPackageUpgradeBinding::inflate, activity) {

    private val mBaseActivity = activity
    private val mDeviceId = deviceId

    override fun initView() {
        mViewBinding.apply {
            val share = MMKVUtil.getInt(MMKVKey.Shared)
            if (share == ConstantInt.NoShare) {
                tvDialogPackageUpgradeLaterBtn.visibility = View.VISIBLE
                btnDialogPackageUpgradeNow.setText(R.string.txt_upgrade_now)
            } else {
                tvDialogPackageUpgradeLaterBtn.visibility = View.GONE
                btnDialogPackageUpgradeNow.setText(R.string.txt_ok)
            }

            setOnClickListenerViews(btnDialogPackageUpgradeNow, tvDialogPackageUpgradeLaterBtn)
        }
    }

    private fun goUpgrade() {
        ViewModelProvider(mBaseActivity)[SubscriptionManageViewModel::class.java].apply {
            mSubscriptionsOrderLiveData.observe(this@PackageUpgradeDialog) {
                mBaseActivity.dealRequestResult(it, object : GetResultCallback {
                    override fun onResult(any: Any) {
                        it.getOrNull()?.let { data ->
                            for (d in data) {
                                if (d.deviceId == mDeviceId) {
                                    val intent =
                                        Intent(mBaseActivity, ChoosePlanActivity::class.java)
                                    intent.putExtra(
                                        ConstantString.Title, "Premium"
                                    )
                                    intent.putExtra(ConstantString.Upgrade, true)
                                    intent.putExtra(ConstantString.LkSetMeal, d)
                                    intent.putExtra(ConstantString.RechargeType, ConstantInt.Type2)
                                    mBaseActivity.startActivity(intent)
                                    dismiss()
                                    break
                                }
                            }
                        }
                    }
                })
                mSubscriptionsOrderLiveData.removeObservers(mBaseActivity)
            }
            getSubscriptionsOrder()
        }
    }

    override fun onClick(v: View) {
        mViewBinding.apply {
            when (v) {
                btnDialogPackageUpgradeNow -> {
                    val share = MMKVUtil.getInt(MMKVKey.Shared)
                    if (share == ConstantInt.NoShare) goUpgrade()
                    else dismiss()
                }

                tvDialogPackageUpgradeLaterBtn -> dismiss()
            }
        }
    }
}