package com.zjgsu.jianshu

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
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.Activity.sendPerceptionActivity
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
lateinit var selectedBook:String
class send_perceptionAdapter(val bookList:List<BookAuthor>):RecyclerView.Adapter<send_perceptionAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val book_Image: ImageView = view.findViewById(R.id.book_Image2)
        val book_Name: TextView = view.findViewById(R.id.book_name)
        val background: LinearLayout = view.findViewById(R.id.bookpic_background)
    }
    private val map: MutableMap<Int, Boolean?> = HashMap()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.bookpic_item, parent, false)
//       val viewHolder=ViewHolder(view)
//        viewHolder.book_Image.setOnClickListener {
//            val position=viewHolder.adapterPosition
//            val book=bookList[position]
//            var cnt=0
//            for (i in map){
//                if(i.value==true)
//                    cnt++
//            }
//            if(cnt==0) {
//                if (map[position] == false) {
//                    map[position] = true
//                    viewHolder.background.setBackgroundColor(Color.CYAN)
//                    selectedBook = book.name
//                } else {
//                    map[position] = false
//                    viewHolder.background.setBackgroundColor(Color.WHITE)
//                    selectedBook = ""
//                }
//            }
//            else{
//                Toast.makeText(parent.context,"只能选择一本书！",Toast.LENGTH_SHORT).show()
//            }
//        }
        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) {
                            holder.book_Name.text=p.name
                            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.book_Image.setImageBitmap(bitmap)
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                }
                            }.start()
                        }
                    }
                }
            }
        })
        holder.book_Image.setOnClickListener {
            var cnt=0
            for (i in map){
                if(i.value==true)
                    cnt++
            }
            if(cnt==0) {
                map[position]=true
                holder.background.setBackgroundColor(Color.CYAN)
                selectedBook = book.name
            }
            else if(cnt==1){
                if (map[position] == true){
                    map[position] = false
                    holder.background.setBackgroundColor(Color.WHITE)
                    selectedBook = ""
                } else {

                }
            }
            else{

            }
        }
    }
    override fun getItemCount() =bookList.size
}