package com.zjgsu.jianshu.Adapter

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.ActivityChooserView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.datatype.BmobFile
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.bumptech.glide.Glide
import com.zjgsu.jianshu.*
import com.zjgsu.jianshu.Bmob.Book_bmob
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

class BookAdapter2(val bookList: List<Book>,val categoryId:Int) : RecyclerView.Adapter<BookAdapter2.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val bookList_Image: ImageView = view.findViewById(R.id.book_Image)
        val bookList_Name: TextView = view.findViewById(R.id.book_Name)
        val bookList_Introduce: TextView = view.findViewById(R.id.book_Introduce)
        val outerLinearLayout: LinearLayout =view.findViewById(R.id.bookItem_outerLinearLayout)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookAdapter2.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.book_item, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val book = bookList[position]
        holder.bookList_Name.text = book.name
        holder.bookList_Introduce.text = book.introduce
        Glide.with(holder.itemView.context)
            .load(book.picUrl) // 确保你的Book对象有正确的图片URL
            .placeholder(R.drawable.pre_load) // 可以设置一个占位图
            .error(R.drawable.fail_load) // 设置加载失败的图
            .into(holder.bookList_Image)
        holder.outerLinearLayout.setOnClickListener {
            val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
            intent.putExtra("Book_name", book.name)
            val sourceActivity = when (categoryId) {
                1 -> LiteratureActivity::class.java
                2 -> ManagementActivity::class.java
                3 -> PsychologyActivity::class.java
                4 -> ScienceActivity::class.java
                5 -> PhilosophyActivity::class.java
                else -> null
            }
            intent.putExtra("Source_Activity", sourceActivity)
            ContextCompat.startActivity(holder.itemView.context, intent, null)
        }
    }

    override fun getItemCount() = bookList.size
}
