package com.zswc.creativity.utils

import android.content.Context
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnFocusChangeListener
import android.widget.*
import coil.load
import com.google.gson.Gson
import com.hwangjr.rxbus.RxBus
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.zswc.creativity.R
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.inte.ChoiceData
import com.zswc.creativity.model.bean.PayTypeBen


/**
 * 导航-调用手机自带的 高德/百度
 *
 *on 2020/9/21
 */
object PopuBuyUtils {


    fun show(context: Context, cover: String?, name: String?, price: String?, view: View) {

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_pop_buy, null)
        val llAlipay: LinearLayout = layout.findViewById(R.id.ll_alipay)
        val llWx: LinearLayout = layout.findViewById(R.id.ll_wx)
        val tvPay: TextView = layout.findViewById(R.id.tv_pay)

        val imgCover: ImageView = layout.findViewById(R.id.img_cover)
        val tvTitle: TextView = layout.findViewById(R.id.tv_title)
        val tvPrice: TextView = layout.findViewById(R.id.tv_price)
        val et: EditText = layout.findViewById(R.id.et)
        val tvReduce: TextView = layout.findViewById(R.id.tv_reduce)
        val tvAdd: TextView = layout.findViewById(R.id.tv_add)
        val imgAlipay: ImageView = layout.findViewById(R.id.img_alipay)
        val imgWx: ImageView = layout.findViewById(R.id.img_wx)
        val llWallet: LinearLayout = layout.findViewById(R.id.ll_wallet)
        val imgWallet: ImageView = layout.findViewById(R.id.img_wallet)

        val ll: LinearLayout = layout.findViewById(R.id.ll)
        val butCancel: ImageView = layout.findViewById(R.id.img_close)

        imgCover.load(cover){
            size(150,150)
            placeholder(R.drawable.bg_need_error)
            error(R.drawable.bg_need_error)
        }

        tvTitle.text = name
        tvPrice.text = price

        //设置变量
        var all_nums: String = "1"
        var oldnums: String? = null//保存的数量,防止同样的执行多次


        tvReduce.onClick {

            var s = et.text.toString().trim()
            if (TextUtils.isEmpty(s)) {
                et.setText("1")
                all_nums = "1"
            } else {
                var i = s.toInt()
                i = if (i > 1) {
                    i - 1
                } else {
                    1
                }
                et.setText("" + i)
                all_nums = "" + i
            }

        }
        tvAdd.onClick {

            val s: String = et.getText().toString()
            if (TextUtils.isEmpty(s)) {
                et.setText("1")
                all_nums = "1"
            } else {
                var i = s.toInt()
                i += 1
                et.setText("" + i)
                all_nums = "" + i
            }

        }

        et.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {

                val number = editable.toString()
                if (!TextUtils.isEmpty(number)) {
                    all_nums = number
                } else {
                    all_nums = "0"
                }
                if (oldnums != null && oldnums.equals(all_nums)) {
                    return
                }
                oldnums = all_nums

            }
        })

        et.onFocusChangeListener = OnFocusChangeListener { view, b ->
            if (!b) {
                val number: String =et.text.toString().trim()
                if (!TextUtils.isEmpty(number)) {
                    all_nums = number
                } else {
                    all_nums = "0"
                }
            }
        }

        var choseType = 1//1支付宝 2微信  //默认支付宝  3钱包

        // mNormalPopup.dismiss()

        llAlipay.onClick {
            choseType = 1
            llAlipay.setBackgroundResource(R.drawable.bg_pop_pay_on)
            llWx.setBackgroundResource(R.drawable.bg_pop_pay_off)
            llWallet.setBackgroundResource(R.drawable.bg_pop_pay_off)
            imgAlipay.setImageResource(R.drawable.ct_tr_chose_on)
            imgWx.setImageResource(R.drawable.ct_tr_chose_off)
            imgWallet.setImageResource(R.drawable.ct_tr_chose_off)

        }
        llWx.onClick {
            choseType = 2
            llWx.setBackgroundResource(R.drawable.bg_pop_pay_on)
            llAlipay.setBackgroundResource(R.drawable.bg_pop_pay_off)
            llWallet.setBackgroundResource(R.drawable.bg_pop_pay_off)
            imgAlipay.setImageResource(R.drawable.ct_tr_chose_off)
            imgWx.setImageResource(R.drawable.ct_tr_chose_on)
            imgWallet.setImageResource(R.drawable.ct_tr_chose_off)


        }

        llWallet.onClick {
            choseType = 3
            llWx.setBackgroundResource(R.drawable.bg_pop_pay_off)
            llAlipay.setBackgroundResource(R.drawable.bg_pop_pay_off)
            llWallet.setBackgroundResource(R.drawable.bg_pop_pay_on)
            imgAlipay.setImageResource(R.drawable.ct_tr_chose_off)
            imgWx.setImageResource(R.drawable.ct_tr_chose_off)
            imgWallet.setImageResource(R.drawable.ct_tr_chose_on)
        }


        tvPay.onClick {
            //输入支付密码
            if(choseType==3){//钱包支付

                PopuSureUtils.show(context,view, object :ChoiceData{
                    override fun onRestuse(password: String?) {
                       //todo:
                        var payTypeBen= PayTypeBen(choseType,all_nums,password)

                        RxBus.get().post(ConfigRx.CHOSE_PAY_TYPE,payTypeBen)

                        mNormalPopup.dismiss()

                    }

                })

             return@onClick
            }

           // RxBus.get().post(ConfigRx.CHOSE_PAY_TYPE, choseType.toString())
        }

        butCancel.onClick {
            mNormalPopup.dismiss()
        }
        ll.onClick {
            mNormalPopup.dismiss()
        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)


        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }




}
