package com.abbidot.tracker.ui.fragment.subscribe

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.viewModels
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentPaypalPaymentBinding
import com.abbidot.tracker.ui.activity.WebViewActivity
import com.abbidot.tracker.ui.activity.subscribe.PaymentMethodActivity
import com.abbidot.tracker.vm.SubscriptionPayViewModel


/**
 * Paypal支付方式
 */
class PaypalPaymentFragment :
    BaseFragment<FragmentPaypalPaymentBinding>(FragmentPaypalPaymentBinding::inflate) {

    private val mSubscriptionPayViewModel: SubscriptionPayViewModel by viewModels()

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = PaypalPaymentFragment().apply {
            mContext = context
        }
    }

    override fun initData() {
        mViewBinding.apply {
            root.setBackgroundResource(R.color.transparent)
            setOnClickListenerViews(btnSureRedirectPaypal)
            ilPaypalPaymentNoData.btnSureSubscribePlan1Continue.visibility = View.GONE

            getHostActivity(PaymentMethodActivity::class.java)?.let {
                if (null == it.mOrderBean) {
                    tvPaypalPaymentGoTip.visibility = View.GONE
                    btnSureRedirectPaypal.visibility = View.GONE
                    ivSureRedirectPaypalBg.visibility = View.GONE
                } else {
                    ilPaypalPaymentNoData.root.visibility = View.GONE
                }

                if (null == it.mPaymentIDBean) {
                    root.visibility = View.GONE
                } else {
                    showPaypalInfo()
                }
            }
        }
    }

    fun showPaypalInfo() {
        getHostActivity(PaymentMethodActivity::class.java)?.let {
            it.mPaymentIDBean?.let { pId ->
                mViewBinding.apply {
                    root.visibility = View.VISIBLE
                    tvPaypalPaymentEmail.text = pId.paypalMethodInfo.email
                    ilPaypalPaymentNoData.root.visibility =
                        if (TextUtils.isEmpty(pId.paypalMethodInfo.email)) {
                            tvPaypalPaymentEmail.visibility = View.GONE
                            View.VISIBLE
                        } else {
                            tvPaypalPaymentEmail.visibility = View.VISIBLE
                            View.GONE
                        }
                }
            }
        }
    }

    override fun liveDataObserve() {
        mSubscriptionPayViewModel.mCreatePaypalOrderLiveData.observe(viewLifecycleOwner) {
            showNoCancelableLoading(false)
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    it.getOrNull()?.apply {
                        getHostActivity(PaymentMethodActivity::class.java)?.let { p ->
                            val intent = Intent(mContext, WebViewActivity::class.java)
                            intent.putExtra(
                                ConstantString.Title,
                                getString(R.string.txt_pay_order_payment_title)
                            )
                            intent.putExtra(ConstantString.Url, url)
                            rechargeType = p.mRechargeType
                            deviceType = p.mType
                            p.mOrderBean?.let { o ->
                                deviceId = o.deviceId
                            }
                            intent.putExtra(ConstantString.Amount, this)
                            p.startActivity(intent)
                        }
                    }
                }
            })
        }
    }

    override fun onClick(v: View?) {
        mViewBinding.apply {
            when (v!!) {
                btnSureRedirectPaypal -> getHostActivity(PaymentMethodActivity::class.java)?.mOrderBean?.let {
                    showNoCancelableLoading(true)
                    mSubscriptionPayViewModel.createPaypalOrder(it)
                }
            }
        }
    }
}