package com.zjgsu.jianshu.Adapter

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.drawable.Drawable
import android.transition.Transition
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.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.zjgsu.jianshu.*

class BillboardAdapter(var listOfbookRankList: List<List<Book_rank>>) : RecyclerView.Adapter<BillboardAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val bookRank_title:TextView=view.findViewById(R.id.bookRank_title)
        val bookRank_name1: TextView = view.findViewById(R.id.bookRank_name1)
        val bookRank_pic1: ImageView = view.findViewById(R.id.bookRank_pic1)
        val bookRank_decr1: TextView = view.findViewById(R.id.bookRank_desc1)
        val bookRank_name2: TextView= view.findViewById(R.id.bookRank_name2)
        val bookRank_pic2: ImageView = view.findViewById(R.id.bookRank_pic2)
        val bookRank_decr2: TextView = view.findViewById(R.id.bookRank_desc2)
        val bookRank_name3: TextView = view.findViewById(R.id.bookRank_name3)
        val bookRank_pic3: ImageView = view.findViewById(R.id.bookRank_pic3)
        val bookRank_decr3: TextView = view.findViewById(R.id.bookRank_desc3)
        val bookRank_backimg:LinearLayout=view.findViewById(R.id.bookRank_backimg)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bookRankList = listOfbookRankList[position]
        Log.d("BillboardAdapter", "Size of bookRankList at position $position")
        if(position==0) {
            holder.bookRank_title.text="热门图书榜"
            holder.bookRank_decr1.text = "在读人数:"+bookRankList[0].courtOfReaders.toString()
            holder.bookRank_decr2.text = "在读人数:"+bookRankList[1].courtOfReaders.toString()
            holder.bookRank_decr3.text = "在读人数:"+bookRankList[2].courtOfReaders.toString()
            holder.bookRank_backimg.setOnClickListener {
                val intent = Intent(holder.itemView.context, HotBookActivity::class.java)
                // 保存当前页面的滚动位置
                ContextCompat.startActivity(holder.itemView.context, intent, null)
            }
        }
        else{
            holder.bookRank_title.text="高分图书榜"
            holder.bookRank_decr1.text = "简书推荐值:"+bookRankList[0].score
            holder.bookRank_decr2.text = "简书推荐值:"+bookRankList[1].score
            holder.bookRank_decr3.text = "简书推荐值:"+bookRankList[2].score
            holder.bookRank_backimg.setOnClickListener {
                val intent = Intent(holder.itemView.context, GoodBookActivity::class.java)
                ContextCompat.startActivity(holder.itemView.context, intent, null)
            }
        }
        holder.bookRank_name1.text = bookRankList[0].name
        Glide.with(holder.itemView.context)
            .load(bookRankList[0].picUrl) // 确保你的Book对象有正确的图片URL
            .placeholder(R.drawable.pre_load) // 可以设置一个占位图
            .error(R.drawable.fail_load) // 设置加载失败的图
            .into(holder.bookRank_pic1)
        holder.bookRank_name2.text = bookRankList[1].name
        Glide.with(holder.itemView.context)
            .load(bookRankList[1].picUrl) // 确保你的Book对象有正确的图片URL
            .placeholder(R.drawable.pre_load) // 可以设置一个占位图
            .error(R.drawable.fail_load) // 设置加载失败的图
            .into(holder.bookRank_pic2)
        holder.bookRank_name3.text = bookRankList[2].name
        Glide.with(holder.itemView.context)
            .load(bookRankList[2].picUrl) // 确保你的Book对象有正确的图片URL
            .placeholder(R.drawable.pre_load) // 可以设置一个占位图
            .error(R.drawable.fail_load) // 设置加载失败的图
            .into(holder.bookRank_pic3)
    }
    override fun getItemCount() = listOfbookRankList.size
}