package com.zjgsu.jianshu.Adapter

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.zjgsu.jianshu.Perception
import com.zjgsu.jianshu.R
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
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.Perception_bmob
import com.zjgsu.jianshu.Bmob.User_bmob
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL


class GoodsuibiAdapter(val perceptionList: List<Perception>) : RecyclerView.Adapter<GoodsuibiAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val user_Image: ImageView = view.findViewById(R.id.user_Image)
        val user_name: TextView = view.findViewById(R.id.user_Name)
        val perception: TextView = view.findViewById(R.id.goodContent)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.goodsuibi_item, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val perception = perceptionList[position]
        val query = BmobQuery<Perception_bmob>()
        query.addWhereEqualTo("objectId",perception.perceptionid)
        query.findObjects(object : FindListener<Perception_bmob>() {
            override fun done(p0: MutableList<Perception_bmob>?, p1: BmobException?) {
                if (p1 == null) {
                    if (p0 != null && p0.size > 0) {
                        for (p in p0) {
                            holder.perception.text = p.perception
//                            holder.user_Image.setImageResource(R.drawable.touxiang)
                            val bmob=BmobQuery<User_bmob>()
                            bmob.addWhereEqualTo("objectId",p.userid)
                            bmob.findObjects(object :FindListener<User_bmob>(){
                                override fun done(p2: MutableList<User_bmob>?, p3: BmobException?) {
                                    if(p3==null){
                                        if(p2!=null&&p2.size>0){
                                            for(i in p2){
                                                holder.user_name.text=i.nickName
                                                object : Thread() {
                                                    override fun run() {
                                                        try {
                                                            val url = URL(i!!.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_Image.setImageBitmap(bitmap)
                                                        } catch (e: Exception) {
                                                            e.printStackTrace()
                                                        }
                                                    }
                                                }.start()
                                            }
                                        }
                                    }
                                }
                            })
                        }
                    }
                }
            }
        })
    }
    override fun getItemCount() =perceptionList.size

}
