package com.quansu.heifengwuliu.adapter

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.hwangjr.rxbus.RxBus
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.config.ConfigRx
import com.quansu.heifengwuliu.databinding.ItemOutcarBinding
import com.quansu.heifengwuliu.inte.ChoiceData
import com.quansu.heifengwuliu.inte.ChoiceListData
import com.quansu.heifengwuliu.model.SelectData
import com.quansu.heifengwuliu.model.bean.ReceiveBean
import com.quansu.heifengwuliu.utils.PopuListUtils
import com.quansu.heifengwuliu.utils.PopupUtils
import com.quansu.heifengwuliu.vmodel.OwnerMoreVModel
import com.ysnows.base.base.BAdapter
import java.text.DecimalFormat

class CarEnterAdapter(val vm: OwnerMoreVModel) : BAdapter<ReceiveBean, BaseDataBindingHolder<ItemOutcarBinding>>(R.layout.item_outcar) {

    var goodsTypeList = ArrayList<SelectData>()
    var numsTypeList = ArrayList<String>()

    var oldnumber=""
    var oldprice=""
    var oldtotal=""

    fun setData( list:ArrayList<SelectData>,unitlist:ArrayList<String>){
        this.goodsTypeList=list
        this.numsTypeList=unitlist

    }
    override fun convert(holder: BaseDataBindingHolder<ItemOutcarBinding>, item: ReceiveBean) {

        holder.setIsRecyclable(false)
        holder.dataBinding?.item = item
        holder.dataBinding?.vm=vm
        holder.dataBinding?.position=holder.adapterPosition


        if(holder.adapterPosition==0){
            holder.dataBinding?.llDelete!!.visibility =  View.GONE
        }else{
            holder.dataBinding?.llDelete!!.visibility =  View.VISIBLE
        }


        holder.dataBinding?.tvChoseTime!!.setOnClickListener {
            //选择时间
            PopupUtils.setChoseTime(context, object : ChoiceData {
                override fun onRestuse(time: String?) {
                    if (time != null) {
                        holder.dataBinding?.tvChoseTime!!.text = time
                        item.time=time
                    }
                }
            })

        }

        holder.dataBinding?.tvDelete!!.setOnClickListener {

            //删除
            QMUIDialog.MessageDialogBuilder(context)
                    .setTitle("提示")
                    .setMessage("确定要删除该地址么")
                    .addAction("取消", QMUIDialogAction.ActionListener(){
                        dialog, index ->
                        dialog.dismiss()
                    })
                    .addAction("确定", QMUIDialogAction.ActionListener(){
                        dialog, index ->
                        var list=data as ArrayList<ReceiveBean>
                        list.removeAt(holder.adapterPosition)
                        notifyItemRemoved(holder.adapterPosition)
                        RxBus.get().post(ConfigRx.SUM_AGAIN, "")
                        dialog.dismiss()
                    })
                    .show()
        }


        holder.dataBinding?.llGoodsType!!.setOnClickListener {

            PopuListUtils.showQMUIPopup(context, holder.dataBinding?.tvGoodsType!!, goodsTypeList, object : ChoiceListData {
                override fun onRestuse(code: String?,name: String?) {
                    item.goods_type_name=name!!
                    item.goods_type=code!!
                    holder.dataBinding?.tvGoodsType!!.text=name

                   // notifyItemChanged(holder.adapterPosition)

                }
            })

        }

        holder.dataBinding?.llChoseUnits!!.setOnClickListener {
            //选择单位
            PopuListUtils.showQMUIPopup(context, holder.dataBinding?.llChoseUnits!!, numsTypeList, object : ChoiceData {
                override fun onRestuse(name: String?) {
                    item.nums_type=name!!
                    holder.dataBinding?.tvChoseUnits!!.text=name
                }
            })

        }
        holder.dataBinding?.etSingCarNums!!.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) {
                var nums = editable.toString().trim()
                if (TextUtils.isEmpty(nums)) {
                    nums = "0"
                }
                if(!TextUtils.isEmpty(oldnumber)&& oldnumber == nums){
                    return
                }
                oldnumber=nums
                item.nums=nums
                sumTotal(item,1, nums,
                        holder.dataBinding?.etSingCarNums!!,holder.dataBinding?.etSingCarPrice!!,holder.dataBinding?.etSingCarTotal!!)

            }
        })
        holder.dataBinding?.etSingCarPrice!!.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) {
                var price = editable.toString().trim()
                if (TextUtils.isEmpty(price)) {
                    price = "0"
                }

                if(!TextUtils.isEmpty(oldprice)&& oldprice == price){
                    return
                }
                oldprice=price
                item.price=price
                sumTotal(item,2, price, holder.dataBinding?.etSingCarNums!!,holder.dataBinding?.etSingCarPrice!!,holder.dataBinding?.etSingCarTotal!!)


            }
        })
        holder.dataBinding?.etSingCarTotal!!.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) {
                var total = editable.toString().trim()
                if (TextUtils.isEmpty(total)) {
                    total = "0"
                }
                if(!TextUtils.isEmpty(oldtotal)&& oldtotal == total){
                    return
                }
                oldtotal=total
                item.total=total

                RxBus.get().post(ConfigRx.SUM_AGAIN, "")

            }
        })



        holder.dataBinding?.etName!!.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) {
                var name = editable.toString().trim()
                item.name=name

            }
        })

        holder.dataBinding?.etPhone!!.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) {
                var mobile = editable.toString().trim()
                item.mobile=mobile

            }
        })

        holder.dataBinding?.etDetailedAddress!!.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) {
                var address = editable.toString().trim()
                item.address=address

            }
        })
        holder.dataBinding?.etGoodsName!!.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) {
                var goodsname = editable.toString().trim()
                item.goods=goodsname

            }
        })




    }



    /**
     * type:1单车运量 2单价
     * value:输入的值
     * @return
     */
    private fun sumTotal(item: ReceiveBean,type: Int, value: String,etNums: EditText,etPrice: EditText,etTotal: EditText) {
        var df = DecimalFormat("0.00")
        var total = "0"
        if (type == 1) {//数量
            var price = etPrice.text.toString().trim()
            if (TextUtils.isEmpty(price)) {
                price = "0"
            }
            total = df.format(value.toDouble() * price.toDouble())

            item.nums=value
            item.price=price
            item.total=total
        } else {//价格
            var nums = etNums.text.toString().trim()
            if (TextUtils.isEmpty(nums)) {
                nums = "0"
            }
            var df = DecimalFormat("0.00");

            total = df.format(nums.toDouble() * value.toDouble())

            item.nums=nums
            item.price=value
            item.total=total
        }
        etTotal.setText(total)
        RxBus.get().post(ConfigRx.SUM_AGAIN, "")


    }


}
