import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.randomtaskmobile.databinding.ItemOfListBinding
import model.TaskEntity

class ReciclerViewAdapter(var taskList : MutableList<TaskEntity>, private val listener: ListenerAdapter) : RecyclerView.Adapter<ReciclerViewViewHolder>() {

//    var taskList: List<Task> = emptyList()
//        set(newValue) {
//            field = newValue
//            notifyDataSetChanged()
//        }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReciclerViewViewHolder {
        var layoutInflater = LayoutInflater.from(parent.context)
        var binding = ItemOfListBinding.inflate(layoutInflater)
        return ReciclerViewViewHolder(binding)

    }

    override fun getItemCount(): Int = taskList.size

    override fun onBindViewHolder(holder: ReciclerViewViewHolder, position: Int) {
        val item = taskList[position]
        with(holder.binding)
        {
            titleTaskItem.text = item.name
            if(!item.description.isNullOrEmpty()){
                descTaskItem.visibility = View.VISIBLE
                descTaskItem.text = item.description
            }
            if(item.dateTimeTo != null){
                dateToItem.visibility = View.VISIBLE
                dateToItem.text = item.dateTimeTo.toString()
            }
            if (item.complete){
                updateTaskItem.visibility = View.GONE
                completeTaskItem.text = "Не выполнено"
            }
            completeTaskItem.setOnClickListener(){
                listener.setComplete(item)
            }
            updateTaskItem.setOnClickListener(){
                listener.update(item)
            }
            deleteTaskItem.setOnClickListener(){
                listener.delete(item)
            }
        }
    }
    interface ListenerAdapter{
        fun setComplete(taskEntity: TaskEntity)
        fun update(taskEntity: TaskEntity)
        fun delete(taskEntity: TaskEntity)
    }
}

class ReciclerViewViewHolder(var binding: ItemOfListBinding) : RecyclerView.ViewHolder(binding.root)