package net.micode.notes.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.architecture.extension.click
import com.android.architecture.helper.DateHelper
import com.android.architecture.ui.adapter.BaseAdapter
import net.micode.notes.R

import net.micode.notes.data.local.db.entity.Note
import net.micode.notes.databinding.AdapterNoteListBinding

class NoteAdapter : BaseAdapter<Note, AdapterNoteListBinding>() {

    override fun getViewBinding(parent: ViewGroup): AdapterNoteListBinding {
        return AdapterNoteListBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    }

    override fun bindViewHolder(
        holder: ViewHolder<AdapterNoteListBinding>,
        item: Note,
        position: Int
    ) {
        val binding = holder.binding
        binding.tvTitle.text = item.title
        binding.layoutItem.clipToOutline = true
        binding.tvTime.text = DateHelper.getDateFormatString(millis = item.modifyTime)
        binding.tvTopped.visibility = if (item.isTopping) View.VISIBLE else View.GONE
        binding.btnLock.setImageResource(if (item.isLocked) R.drawable.icon_unlock else R.drawable.icon_lock)
        binding.layoutItem.click { mOnItemClickListener?.onItemClick(it.id, position, item) }//点击
        binding.btnDelete.click {
            notifyItemRemoved(position)
            data.removeAt(position)
            notifyItemRangeRemoved(position, data.size - position)
            mOnItemClickListener?.onItemClick(it.id, position, item)
        }
        binding.btnLock.click {
            item.toggleType(Note.TYPE_LOCKED)
            notifyItemRemoved(position)
            data.removeAt(position)
            notifyItemRangeRemoved(position, data.size - position)
            mOnItemClickListener?.onItemClick(it.id, position, item)
        }
    }
}