package com.swago.baseswago.dialog

import android.os.Bundle
import android.view.Gravity
import android.widget.Toast
import androidx.fragment.app.viewModels
import com.android.billingclient.api.Purchase
import com.swago.baseswago.PayVm
import com.swago.baseswago.R
import com.swago.baseswago.databinding.DialogBenefitBagBinding
import com.swago.baseswago.libpay.IPayCallback
import com.swago.baseswago.libpay.PayManager
import com.swago.baseswago.model.pay.PayModel
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.SwagoLoading
import com.swago.baseswago.util.UserInfo

/**
 *@date 2022/1/18 20:29
 *description:
 */
class BenefitBagDialog : BaseXDFragment<DialogBenefitBagBinding>(), IPayCallback {

    private val payVm by viewModels<PayVm>()
    var benefitIconDismissFun:(()->Unit)? = null

    private var productId = ""

    companion object{
        fun newInstance(): BenefitBagDialog {
            val args = Bundle()
            val dialog = BenefitBagDialog()
            dialog.arguments = args
            return dialog
        }
    }

    override fun initOther() {
        PayManager.listener.add(this)
        payVm.getBenefit()
        binding.ivClose.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                dismissAllowingStateLoss()
            }
        })
        binding.tvRecharge.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                activity?.let {
                    SwagoLoading.showLoadingDialog(it)
                    PayManager.queryUnConsumeOrder(productId)
                }
            }
        })
    }

    override fun initLiveData() {
        payVm.benefitLiveData.observe(this){
            binding.tvCoin.text = it.product_coins
            productId = it.product_id
            val payModel = PayModel()
            payModel.google_product_id = it.google_product_id
            payModel.product_coins = it.product_coins
            val list = ArrayList<PayModel>()
            list.add(payModel)
            PayManager.queryProductPrice(list)
        }
    }

    override fun onDestroyView() {
        PayManager.listener.remove(this)
        super.onDestroyView()
    }

    init {
        setCanCancel(false)
        setGravity(Gravity.CENTER)
        setDimAmount(0.5f)
    }

    override fun onConnectedServer() {
    }

    override fun onPaySuccess(data: List<Purchase>?) {
        data?.forEach {
            payVm.payOrderToServer(it, 1)
            UserInfo.getUserInfo()?.is_benefit = 1
            benefitIconDismissFun?.invoke()
        }
    }

    override fun onQueryProductPrice(data: List<PayModel>) {
        SwagoLoading.cancelLoadingDialog()
        binding.tvRecharge.text = data[0].product_price
        PayManager.queryUnConsumeOrder("")
    }

    override fun onPayError(code: Int) {
        SwagoLoading.cancelLoadingDialog()
        Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(R.string.pay_fail)+"-$code", Toast.LENGTH_SHORT).show()
        payVm.payOrderToServer(null, 2)
    }

    override fun onPayCancel() {
        Toast.makeText(AppContext.getContext(), AppContext.getContext().resources.getString(R.string.pay_cancel), Toast.LENGTH_SHORT).show()
        payVm.payOrderToServer(null, 3)
    }

    override fun onUnConsumeOrder(data: List<Purchase>) {
        data.forEach {
            val orderLostDialog = OrderLostDialog.newInstance(it.orderId)
            orderLostDialog.commitLostOrderFun = {
                payVm.payOrderToServer(it,4)
            }
            orderLostDialog.show(childFragmentManager, "OrderLostDialog")
        }
    }

    override fun consumeOrderSuccess() {
        dismissAllowingStateLoss()
    }

    override fun consumeOrderFail(code: Int) {
    }

    override fun toCreateNewOrder(productId: String) {
        payVm.createOrder(productId)
    }
}