package com.zjgsu.jianshu.Adapter

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.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.zjgsu.jianshu.Bmob.Comment_bmob
import com.zjgsu.jianshu.Bmob.User_bmob
import com.zjgsu.jianshu.Book_authorAdapter
import com.zjgsu.jianshu.Comment
import com.zjgsu.jianshu.R
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

class CommentAdapter(val commentlist: List<Comment>) :
    RecyclerView.Adapter<CommentAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val user_icon: ImageView = view.findViewById(R.id.inf_comment_user_icon)
        val user_name: TextView = view.findViewById(R.id.inf_comment_postPersonName)
        val content: TextView = view.findViewById(R.id.inf_comment_textView_perception)
    }

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

    override fun onBindViewHolder(holder: CommentAdapter.ViewHolder, position: Int) {
        val comment = commentlist[position]
        val query = BmobQuery<Comment_bmob>()
        query.addWhereEqualTo("objectId", comment.commentid)
        query.findObjects(object : FindListener<Comment_bmob>() {
            override fun done(list: List<Comment_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        holder.content.text = i.text
                        Log.d("zytt",i.text)
                        val query2 = BmobQuery<User_bmob>()
                        query2.addWhereEqualTo("objectId",i.comment_personid)
                        query2.findObjects(object:FindListener<User_bmob>(){
                            override fun done(list:List<User_bmob>,e:BmobException?){
                                if(e==null){
                                    for(j in list){
                                        holder.user_name.text=j.nickName
                                        object : Thread() {
                                            override fun run() {
                                                try {
                                                    val url = URL(j!!.face.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.user_icon.setImageBitmap(bitmap)
                                                } catch (e: Exception) {
                                                    e.printStackTrace()
                                                }
                                            }
                                        }.start()
                                    }
                                }
                            }
                        })
                    }
                }
            }
        })
    }
    override fun getItemCount() = commentlist.size
}