package com.zjgsu.jianshu

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

//TODO:选中书本的时候书本封面变灰
class BookshelfAdapter(val context: Context, val bookshelf: ArrayList<Book_Shelf>) : RecyclerView.Adapter<BookshelfAdapter.ViewHolder>() {
    private var isSelectMode: Boolean = false
    val selectedItems = mutableSetOf<Int>()     //当前选中的书本position列表
    var onSelectionChanged: ((Int) -> Unit)? = null

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val bookshelfBkimg: ImageView = view.findViewById(R.id.bookshelf_bkimg)
        val bookshelfSelectIcon: ImageView = view.findViewById(R.id.bookshelf_select_icon)
        val bookshelfBkname: TextView = view.findViewById(R.id.bookshelf_bkname)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val book = bookshelf[position]
        holder.bookshelfBkname.text = book.bkname

        if (book.bkname.equals("添加")) {
            holder.bookshelfBkimg.scaleType = ImageView.ScaleType.CENTER_INSIDE
            holder.bookshelfBkimg.setImageResource(R.drawable.add)
            holder.bookshelfSelectIcon.visibility = View.GONE
        } else {
            holder.bookshelfSelectIcon.visibility = if (isSelectMode) View.VISIBLE else View.GONE
            holder.bookshelfSelectIcon.setImageResource(if (selectedItems.contains(position)) R.drawable.selected else R.drawable.select)
            Glide.with(holder.itemView.context)
                .load(book.picurl)
                .placeholder(R.drawable.pre_load)
                .error(R.drawable.fail_load)
                .into(holder.bookshelfBkimg)
        }

        holder.bookshelfSelectIcon.setOnClickListener {  //选择or取消
            toggleSelection(position)
        }

        holder.bookshelfBkimg.setOnClickListener {
            if (book.bkname.equals("添加")) {
                val intent = Intent(context, MainActivity::class.java)
                context.startActivity(intent)
            } else {
                if (isSelectMode) {
                    toggleSelection(position)
                } else {
                    val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
                    intent.putExtra("Book_name", book.bkname)
                    intent.putExtra("Source_Activity", BookShelfActivity::class.java)
                    ContextCompat.startActivity(holder.itemView.context, intent, null)
                }
            }
        }
    }

    override fun getItemCount() = bookshelf.size

    fun toggleSelectMode() {
        isSelectMode = !isSelectMode
        if (!isSelectMode) {
            selectedItems.clear()
            onSelectionChanged?.invoke(selectedItems.size)
        }
        notifyDataSetChanged()
    }
    private fun toggleSelection(position: Int) {
        if (selectedItems.contains(position)) {
            selectedItems.remove(position)
        } else {
            selectedItems.add(position)
        }
        notifyItemChanged(position)
        onSelectionChanged?.invoke(selectedItems.size)
    }
}