package com.zjgsu.jianshu

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import com.bumptech.glide.Glide
import com.zjgsu.jianshu.Activity.sendPerceptionActivity
import com.zjgsu.jianshu.Bmob.BookShelf
import com.zjgsu.jianshu.Bmob.Book_bmob
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.book_author_item.*
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
lateinit var selectedBook:String
class send_perceptionAdapter(val bookList:List<Book_Shelf>):RecyclerView.Adapter<send_perceptionAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val book_Image: ImageView = view.findViewById(R.id.send_bookimg)
        val book_Name: TextView = view.findViewById(R.id.send_bookname)
        val background: LinearLayout = view.findViewById(R.id.bookpic_background)
    }
    private val map: MutableMap<Int, Boolean?> = HashMap()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.bookpic_item, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val book = bookList[position]
        Glide.with(holder.itemView.context)
            .load(book.picurl) // 确保你的Book对象有正确的图片URL
            .placeholder(R.drawable.pre_load) // 可以设置一个占位图
            .error(R.drawable.fail_load) // 设置加载失败的图
            .into(holder.book_Image)
        holder.book_Name.text=book.bkname
        holder.book_Image.setOnClickListener {
            var cnt=0
            for (i in map){
                if(i.value==true)
                    cnt++
            }
            if(cnt==0) {
                map[position]=true
                holder.background.setBackgroundColor(Color.CYAN)
                selectedBook = book.bkname
            }
            else if(cnt==1){
                if (map[position] == true){
                    map[position] = false
                    holder.background.setBackgroundColor(Color.WHITE)
                    selectedBook = ""
                } else {

                }
            }
            else{
            }
        }
    }
    override fun getItemCount() =bookList.size
}