package com.pan_american.android.data.model.personnel_module.materials_operate.adapter

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.pan_american.android.OASystem
import com.pan_american.android.R
import com.pan_american.android.data.model.personnel_module.materials_operate.entity.MaterialListItem
import com.pan_american.android.util.ScrollEditText

class MaterialListAdapter(private val list: ArrayList<MaterialListItem>): RecyclerView.Adapter<MaterialListAdapter.ViewHolder>() {

    lateinit var onItemClickListener: OnItemClickListener

    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val materialName: TextView = view.findViewById(R.id.material_name)
        val deleteButton: ImageView = view.findViewById(R.id.delete)
        val materialType: TextView = view.findViewById(R.id.material_type)
        val materialCount: TextView = view.findViewById(R.id.material_count)
        val creator: TextView = view.findViewById(R.id.creator)
        val createTime: TextView = view.findViewById(R.id.create_time)
        val remark: ScrollEditText = view.findViewById(R.id.remark)
        val putInStorage: TextView = view.findViewById(R.id.put_in_storage)
        val usingApplication: TextView = view.findViewById(R.id.using_application)
    }

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

        val viewHolder = ViewHolder(view)

        if (OASystem.authorization(OASystem.MATERIAL_APPLICATION, OASystem.EDIT)) {
            viewHolder.itemView.setOnClickListener {
                onItemClickListener.onItemClick(viewHolder.absoluteAdapterPosition)
            }
        }

        viewHolder.deleteButton.setOnClickListener {
            onItemClickListener.onItemDelete(viewHolder.absoluteAdapterPosition)
        }

        viewHolder.putInStorage.setOnClickListener {
            onItemClickListener.onPutInClick(viewHolder.absoluteAdapterPosition)
        }

        viewHolder.usingApplication.setOnClickListener {
            onItemClickListener.onUseClick(viewHolder.absoluteAdapterPosition)
        }

        return viewHolder
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.apply {
            list[position].also {
                materialName.text = it.name
                materialType.text = it.typeName
                materialCount.text = it.stockQuantity.toString()
                creator.text = it.lastUpdateUserName
                createTime.text = it.lastUpdateTime
                remark.setText(it.remark)
            }

//            if (OASystem.authorization(OASystem.MATERIAL_OPERATION, OASystem.DELETE)) {
//                deleteButton.visibility = View.VISIBLE
//            } else {
//                deleteButton.visibility = View.GONE
//            }

//            if (OASystem.authorization(OASystem.MATERIAL_OPERATION, OASystem.ADD)) {
//                putInStorage.visibility = View.VISIBLE
//            } else {
//                deleteButton.visibility = View.GONE
//            }

            remark.isEnabled(false)
        }
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)

        fun onPutInClick(position: Int)

        fun onUseClick(position: Int)

        fun onItemDelete(position: Int)
    }
}