package com.zswc.creativity.vmodel

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.text.TextUtils
import androidx.databinding.Bindable
import com.zswc.creativity.model.BankCard
import com.zswc.creativity.model.InfoBean
import com.zswc.creativity.utils.ChosePhotoUtils
import com.zswc.creativity.utils.net.NetEngine
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.zswc.creativity.BR


class BankVModel(application: Application) : BViewModel<BRepository>(application) {

   // var image: MutableLiveData<String> = MutableLiveData("")
   var image=""

    @Bindable
    var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    @Bindable
    var number: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.number)
        }

    @Bindable
    var bank: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.bank)
        }

    @Bindable
    var kbank: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.kbank)
        }


    fun discernBank(img:String){//识别银行卡

        repository().rxLreq(NetEngine.service.cvBankCard(img))
                .doOnNext {
                    if (it.ok(true)) {
                        var bean=it.data() as BankCard
                        number=bean.number
                    }

                }
                .subscribe()

     }

    fun toSubmit(){
        if(TextUtils.isEmpty(name)){
            toast("请输入真实姓名")
            return
        }

        if(TextUtils.isEmpty(number)){
            toast("请输入银行卡号")
            return
        }
        if(TextUtils.isEmpty(bank)){
            toast("请输入开户银行")
            return
        }

        if(TextUtils.isEmpty(kbank)){
            toast("请输入开户银行所在支行")
            return
        }

        repository().rxLreq(NetEngine.service.setCardBank(name, number,bank,kbank, image))
                .doOnNext {
                    if (it.ok(true)) {
                        var con = repository().context as Activity
                        con.setResult(Activity.RESULT_OK, Intent())
                        con.finish()
                    }
                }
                .subscribe()

    }

    fun choseImg(){

        //选择照片
        ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)

    }


    fun getInfo(){
        //获取银行卡信息
        repository().rxLreq(NetEngine.service.getInfo("4"))
                .doOnNext {
                    if (it.ok(false)) {
                        var bean=it.data() as InfoBean
                        if(null!=bean){
                            name=bean.name
                            number=bean.number
                            bank=bean.bank
                            kbank=bean.kbank

                        }

                    }
                }
                .subscribe()

    }


}



