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.bumptech.glide.Glide
import com.zjgsu.jianshu.Bmob.Perception_bmob
import com.zjgsu.jianshu.Bmob.User_bmob
import kotlinx.android.synthetic.main.activity_book_info.*
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL


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

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

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.goodperception_item, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val perception = perceptionList[position]
        // 从 Perception_bmob 表中查找与 perception 对应的数据
        BmobQuery<Perception_bmob>()
            .addWhereEqualTo("objectId", perception.perceptionid)
            .findObjects(object : FindListener<Perception_bmob>() {
                override fun done(perceptionList: MutableList<Perception_bmob>?, exception: BmobException?) {
                    if (exception == null && perceptionList != null && perceptionList.isNotEmpty()) {
                        val perceptionData = perceptionList.first()
                        holder.perception.text = perceptionData.perception
                        if (perceptionData.evaluation == 1)
                            holder.evaluation_state.setImageResource(R.drawable.recommend1)
                        else if (perceptionData.evaluation == 2)
                            holder.evaluation_state.setImageResource(R.drawable.normal1)
                        else if (perceptionData.evaluation == 3)
                            holder.evaluation_state.setImageResource(R.drawable.bad1)
                        // 从 User_bmob 表中查找与 perceptionData.userid 对应的数据
                        BmobQuery<User_bmob>()
                            .addWhereEqualTo("objectId", perceptionData.userid)
                            .findObjects(object : FindListener<User_bmob>() {
                                override fun done(userList: MutableList<User_bmob>?, userException: BmobException?) {
                                    if (userException == null && userList != null && userList.isNotEmpty()) {
                                        val userData = userList.first()
                                        holder.user_name.text = userData.nickName
                                        Glide.with(holder.itemView.context)
                                            .load(userData.face.url)
                                            .placeholder(R.drawable.pre_load)
                                            .error(R.drawable.fail_load)
                                            .into(holder.user_Image)
                                    }
                                }
                            })
                    }
                }
            })
    }
    override fun getItemCount() =perceptionList.size

}
