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.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 bookList:List<BookAuthor>):RecyclerView.Adapter<Book_authorAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val bookList_Image: ImageView = view.findViewById(R.id.book_Image1)
        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 book = bookList[position]
        val query = BmobQuery<Book_bmob>()
        query.addWhereEqualTo("name",book.name)
        query.findObjects(object : FindListener<Book_bmob>() {
            override fun done(p0: MutableList<Book_bmob>?, p1: BmobException?) {
                if (p1 == null) {
                    if (p0 != null && p0.size > 0) {
                        for (p in p0) {
                            object : Thread() {
                                override fun run() {
                                    try {
                                        val url = URL(p!!.picture.url)
                                        val connection: HttpURLConnection =
                                            url.openConnection() as HttpURLConnection
                                        connection.setRequestMethod("GET")
                                        connection.setConnectTimeout(5000)
                                        val `in`: InputStream = connection.getInputStream()
                                        val bitmap: Bitmap =
                                            BitmapFactory.decodeStream(`in`)
                                        holder.bookList_Image.setImageBitmap(bitmap)
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                }
                            }.start()
                        }
                    }
                }
            }
        })
        val query1=BmobQuery<BookShelf>()
        query1.addWhereEqualTo("userid", UserId)
        query1.addWhereEqualTo("b_name",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.setText("已添加")
                    }
                }
            }
        })
        holder.bookList_Image.setOnClickListener {
            val intent= Intent(holder.itemView.context,Book_informationActivity::class.java)
            intent.putExtra("Book_name",book.name)
            ContextCompat.startActivity(holder.itemView.context, intent, null)
        }
        holder.addButton.setOnClickListener{
//            val intent = Intent(holder.itemView.context,BookShelfActivity::class.java)
//            intent.putExtra("Book_name",book.name)
//            ContextCompat.startActivity(holder.itemView.context,intent,null)
            val query= BmobQuery<BookShelf>()
            query.addWhereEqualTo("userid", UserId)
            query.addWhereEqualTo("b_name",book.name)
            query.findObjects(object :FindListener<BookShelf>(){
                override fun done(p0: MutableList<BookShelf>?, p1: BmobException?) {
                    if(p1==null){
                        if(p0!=null&&p0.size>0){
//                           Toast.makeText(author_introductionActivity, "11", Toast.LENGTH_SHORT)
                        }
                        else{
                            val bookshelf_record=BookShelf()
                            bookshelf_record.settb_name(book.name)
                            bookshelf_record.setuserid(UserId)
                            bookshelf_record.save(object : SaveListener<String>() {
                                override fun done(objectId: String?, e: BmobException?) {
                                    if (e == null) {
                                     holder.addButton.text="已添加"
                                    } else {
                                    }
                                }
                            })
                        }
                    }
                }
            })
        }
    }
    override fun getItemCount() =bookList.size
}