package com.lukouguoji.gjj.adapt

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.model.GjjCangDanListModel
import com.lukouguoji.gjj.viewModel.GjjCangDanListViewModel
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.Common

class GjjCangDanListAdapter(
    val activity: Activity,
    private val viewModel: GjjCangDanListViewModel,
    private val collectList: MutableList<GjjCangDanListModel>
) :
    RecyclerView.Adapter<GjjCangDanListAdapter.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 goods: TextView = view.findViewById(R.id.goods)
        val awbType: TextView = view.findViewById(R.id.awbType)
        val startPort: TextView = view.findViewById(R.id.startPort)
        val destPort: TextView = view.findViewById(R.id.destPort)

        val agentCode: TextView = view.findViewById(R.id.agentCode)
        val spCode: TextView = view.findViewById(R.id.spCode)
        val totalPc: TextView = view.findViewById(R.id.totalPc)
        val pc: TextView = view.findViewById(R.id.pc)
        val acWeight: TextView = view.findViewById(R.id.acWeight)
        val chWight: TextView = view.findViewById(R.id.chWight)

        val delete: TextView = view.findViewById(R.id.delete)
    }

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

        viewHolder.itemView.setOnClickListener {

            var adapterPosition = viewHolder.adapterPosition
            if (adapterPosition < 0 || collectList.isEmpty()) {
                return@setOnClickListener
            }
            var item = collectList[adapterPosition]

            ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_GJJ_CANG_DAN_INFO).withString("id", item.id).withString("ref", item.ref)
                .withInt("fid", viewModel.fid ?: -9999).withString("prefix", viewModel.prefix)
                .navigation(activity, Constant.RequestCode.gnj_cang_dan_list)

        }
        return viewHolder
    }

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

        val ref = collect.ref
        if (ref == "1") {
            collect.isCheck = false
        }
        val leftIcon = holder.leftIcon
        //选中
        if (collect.isCheck) {
            leftIcon.setImageResource(R.mipmap.img_plane_s)
        } else {
            leftIcon.setImageResource(R.mipmap.gnc_ware_house_left_icon)
        }

        holder.wbNo.text = collect.wbNo
        holder.goods.text = collect.goods
        holder.awbType.text = collect.awbType
        holder.startPort.text = collect.startPort
        holder.destPort.text = collect.destPort
        holder.agentCode.text = collect.agentCode
        holder.spCode.text = collect.spCode
        holder.totalPc.text = "${collect.totalPc}"
        holder.pc.text = "${collect.pc}"
        holder.acWeight.text = "${collect.acWeight} KG"
        holder.chWight.text = "${collect.chWight} KG"

        //红色图标 不允许操作
        if (ref == "1") {
            leftIcon.setImageResource(R.mipmap.img_plane_disable)
        } else {
            leftIcon.setOnClickListener {
                collect.isCheck = !collect.isCheck
                if (collect.isCheck) {
                    viewModel.plusOne()
                } else {
                    viewModel.subOne()
                }
                notifyDataSetChanged() // 更新数据
            }
        }

        holder.delete.setOnClickListener {
            if (ref == "1") {
                Common.showToast(activity, "货物已发放，无法删除！")
            } else {
                Common.secondConfirmDialog(activity, "确认删除?") { dialog ->
                    dialog.dismiss()
                    val position = holder.adapterPosition
                    viewModel.deleteFestGjjCangDan(collect.id.toInt(), position)
                }
            }
        }
    }

    override fun getItemCount() = collectList.size

    fun append(gjjCangDanListModel: GjjCangDanListModel) {
        collectList.add(gjjCangDanListModel)
        notifyDataSetChanged()
    }

}