package com.lukouguoji.mit.adapt

import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.lukouguoji.mit.R
import com.lukouguoji.mit.model.OutLoadingPullModel
import com.lukouguoji.mit.viewModel.OutLoadingPullViewModel
import com.lukouguoji.module_base.util.Arith

class OutLoadingPullAdapter(
    val activity: Activity,
    private val viewModel: OutLoadingPullViewModel,
    private val collectList: MutableList<OutLoadingPullModel>
) :
    RecyclerView.Adapter<OutLoadingPullAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val leftIcon: ImageView = view.findViewById(R.id.leftIcon)
        val wbNo: TextView = view.findViewById(R.id.wbNo)
        val pc: TextView = view.findViewById(R.id.pc)
        val weight: TextView = view.findViewById(R.id.weight)
        val volume: TextView = view.findViewById(R.id.volume)
        val location: TextView = view.findViewById(R.id.location)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.out_loading_pull_list_item, parent, false)
        val viewHolder = ViewHolder(view)

        viewHolder.itemView.setOnClickListener {

            var adapterPosition = viewHolder.absoluteAdapterPosition
            if (adapterPosition < 0 || collectList.isEmpty()) {
                return@setOnClickListener
            }
            val builder: AlertDialog.Builder = AlertDialog.Builder(activity)
            builder.setTitle("请填写拉货件数、重量")
            // 通过LayoutInflater来加载一个xml的布局文件作为一个View对象
            val viewDialog: View = LayoutInflater.from(activity).inflate(
                R.layout.out_loading_pull_sub_dialog, null
            )
            // 设置我们自己定义的布局文件作为弹出框的Content
            builder.setView(viewDialog)

            val wbNo = viewDialog.findViewById<TextView>(R.id.wbNo)
            val pc = viewDialog.findViewById<EditText>(R.id.pc)
            val weight = viewDialog.findViewById<EditText>(R.id.weight)

            wbNo.text = collectList[adapterPosition].wbNo
            /*pc.setText(collectList[adapterPosition].pc.toString())
            weight.setText(collectList[adapterPosition].weight.toString())*/

            pc?.requestFocus()

            builder.setPositiveButton("确定",
                DialogInterface.OnClickListener { dialog, which ->
                    //确定操作的内容
                    collectList[adapterPosition].pc = pc.text.toString().toInt()
                    collectList[adapterPosition].weight = weight.text.toString().toDouble()
                    collectList[adapterPosition].volume = Arith.div(weight.text.toString().toDouble(), 220.0, 2)
                    notifyDataSetChanged()
                    dialog.dismiss()
                })

            builder.setNegativeButton("取消",
                DialogInterface.OnClickListener { dialog, which ->
                    dialog.dismiss()
                })
            builder.show()

        }

        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val collect = collectList[position]

        val leftIcon = holder.leftIcon
        //选中
        if (collect.isCheck) {
            leftIcon.setImageResource(R.mipmap.left_icon_check)
        } else {
            leftIcon.setImageResource(R.mipmap.gnc_ware_house_left_icon)
        }

        holder.wbNo.text = collect.wbNo
        holder.pc.text = "${collect.pc}"
        holder.weight.text = "${collect.weight} KG"
        holder.volume.text = "${collect.volume}"
        holder.location.text = "${collect.location}"

        leftIcon.setOnClickListener {
            collect.isCheck = !collect.isCheck
            if (collect.isCheck) {
                viewModel.plusOne()
            } else {
                viewModel.subOne()
            }
            notifyDataSetChanged() // 更新数据
        }

    }

    override fun getItemCount() = collectList.size

    fun append(outLoadingPullModel: OutLoadingPullModel) {
        collectList.add(outLoadingPullModel)
        notifyDataSetChanged()
    }

}