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.*
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.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

class Book_authorAdapter(val authorworkList:List<AuthorWork>,val userId: String):RecyclerView.Adapter<Book_authorAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val workimg: ImageView = view.findViewById(R.id.authorwork_img)
        val addButton:Button=view.findViewById(R.id.addButton)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.book_author_item, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val authorwork = authorworkList[position]
        Glide.with(holder.itemView.context)
            .load(authorwork.bookurl) // 确保你的Book对象有正确的图片URL
            .placeholder(R.drawable.pre_load) // 可以设置一个占位图
            .error(R.drawable.fail_load) // 设置加载失败的图
            .into(holder.workimg)
        val query1=BmobQuery<BookShelf>()
        query1.addWhereEqualTo("userid", userId)
        query1.addWhereEqualTo("b_name",authorwork.book_name)
        query1.findObjects(object :FindListener<BookShelf>() {
            override fun done(p0: MutableList<BookShelf>?, p1: BmobException?) {
                if (p1 == null) {
                    if (p0 != null && p0.size > 0) {
                        holder.addButton.isEnabled = false
                        holder.addButton.setText("已添加")
                    }else{
                        holder.addButton.isEnabled = true
                        holder.addButton.setOnClickListener {
                            val bookshelf_record = BookShelf()
                            bookshelf_record.setb_name(authorwork.book_name)
                            bookshelf_record.setuserid(userId)
                            bookshelf_record.setpicurl(authorwork.bookurl)
                            bookshelf_record.save(object : SaveListener<String>() {
                                override fun done(objectId: String?, e: BmobException?) {
                                    if (e == null) {
                                        holder.addButton.isEnabled = false
                                        holder.addButton.text = "已添加"
                                    } else {
                                        // 添加失败的处理
                                    }
                                }
                            })
                        }
                    }
                }
            }
        })
        holder.workimg.setOnClickListener {
            val intent= Intent(holder.itemView.context,BookInformationActivity::class.java)
            intent.putExtra("Book_name",authorwork.book_name)
            ContextCompat.startActivity(holder.itemView.context, intent, null)
        }
    }
    override fun getItemCount() =authorworkList.size
}