package com.quansu.heifengwuliu.vmodel

import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.lifecycle.MutableLiveData
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinHelper
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.util.QMUIResHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.model.DataInfoBean
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.quansu.heifengwuliu.view.PasswordView
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel


open class SourceDetailsVModel : BViewModel<BRepository>() {

    val info: MutableLiveData<DataInfoBean> = MutableLiveData<DataInfoBean>()

    fun getInfo(info_id: String) {
        repository().lreq(NetEngine.service.infoGet(info_id))
                .doOnNext() {
                    if (it.ok()) {

                        info.value = it.data() as DataInfoBean

                    }
                }
                .subscribe()
    }


    fun getInfoPay(info_id: String,pay_pwd:String) {
        repository().lreq(NetEngine.service.infoGetPay(info_id,pay_pwd))
                .doOnNext() {
                    if (it.ok()) {
                        toast(it.msg())
                    }
                }
                .subscribe()
    }

    fun showPayDialog(info_id: String,view:LinearLayout){
        var mNormalPopup = QMUIPopups.fullScreenPopup(repository().context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(repository().context)
        frameLayout.background = QMUIResHelper.getAttrDrawable(repository().context, R.attr.qmui_skin_support_popup_bg)
        builder.background(R.attr.qmui_skin_support_popup_bg)
        QMUISkinHelper.setSkinValue(frameLayout, builder)
        frameLayout.radius = QMUIDisplayHelper.dp2px(repository().context, 12)
        val padding = QMUIDisplayHelper.dp2px(repository().context, 10)
        frameLayout.setPadding(padding, padding, padding, padding)

        //自定义布局
        val layoutInflater = repository().context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_apy, null)
        val butCancel: QMUIRoundButton = layout.findViewById(R.id.but_cancel)
        val butSure: QMUIRoundButton = layout.findViewById(R.id.tv_sure)
        val payPwdEditText: PasswordView = layout.findViewById(R.id.ppet)
        var password=""

        payPwdEditText.setPasswordListener(object : PasswordView.PasswordListener {
            override fun keyEnterPress(pwd: String?, isComplete: Boolean) {
                password= pwd!!

            }

            override fun passwordChange(changeText: String?) {
            }

            override fun passwordComplete() {

            }

        })

        butSure.setOnClickListener {

            if(TextUtils.isEmpty(password)){
                toast("请输入支付密码！")
                return@setOnClickListener
            }
            getInfoPay(info_id,password)

            mNormalPopup.dismiss()
        }

        butCancel.setOnClickListener {
            mNormalPopup.dismiss()
        }
        builder.release();

        val size = QMUIDisplayHelper.dp2px(repository().context, 220)

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

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

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

    }

    fun setCancelOrder(info_id: String){
        //取消发布

        repository().lreq(NetEngine.service.infoCancel(info_id))
                .doOnNext() {
                    if (it.ok()) {

                        var con = repository().context as Activity
                        con.finish()

                    }
                }
                .subscribe()
    }

    fun anotherOne(){
        //再来一单
        var con = repository().context as Activity
        con.finish()
    }


}
