package com.zjgsu.jianshu

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.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 com.bumptech.glide.Glide
import com.zjgsu.jianshu.Bmob.Book_bmob
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL


class BookAdapter(var bookList: List<Book>) : RecyclerView.Adapter<BookAdapter.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 innerLinearLayout: LinearLayout = view.findViewById(R.id.bookItem_innerLinearLayout)
    val outerLinearLayout:LinearLayout=view.findViewById(R.id.bookItem_outerLinearLayout)
    val rankimg:ImageView=view.findViewById(R.id.bookItem_rankimg)
}

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): 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
        val backgrounds = arrayOf(R.drawable.color_lightpurple, R.drawable.color_lightblue,R.drawable.color_lightgreen, R.drawable.color_flesh)
        holder.innerLinearLayout.setBackgroundResource(backgrounds[position % backgrounds.size])

//        //添加金银铜奖牌
//        if (position == 0) { // 假设第一名使用金牌图片
//            holder.rankimg.setImageResource(R.drawable.gold)
//        } else if (position == 1) { // 第二名使用其他图片，以此类推
//            holder.rankimg.setImageResource(R.drawable.silver)
//        }
//        else if(position==2){
//            holder.rankimg.setImageResource(R.drawable.copper)
//        }
//        else holder.rankimg.setImageDrawable(null)

        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)
            intent.putExtra("Source_Activity", MainActivity::class.java)
            ContextCompat.startActivity(holder.itemView.context, intent, null)
        }
    }
    override fun getItemCount() = bookList.size
    fun updateBooks(newBooks: List<Book>) {
        bookList = newBooks
        notifyDataSetChanged()
    }
}
