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.ImageView
import android.widget.TextView
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 com.bumptech.glide.Glide
import com.zjgsu.jianshu.Activity.Perception_informationActivity
import com.zjgsu.jianshu.Bmob.Book_info_bmob
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 PerceptionAdapter(val perceptionList: List<Perception>) :
    RecyclerView.Adapter<PerceptionAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val perception_perception: TextView = view.findViewById(R.id.perception_content)

        //用户头像
        val perception_userIcon: ImageView = view.findViewById(R.id.perception_avatar)
        val perception_posetPerson: TextView = view.findViewById(R.id.perception_username)

        //书籍封面
        val perception_bookPic: ImageView = view.findViewById(R.id.perception_bookpic)
        val perception_bookName: TextView = view.findViewById(R.id.perception_bookName)
        val perception_bookAuthor: TextView = view.findViewById(R.id.perception_bookAuthor)
        val perception_bookInf: View = view.findViewById(R.id.perception_bookInf)
        lateinit var perceptionid: String
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var bookName: String = ""
        val perception = perceptionList[position]
        //设置text
        holder.perceptionid = perception.perceptionid
        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) {
                            bookName = p.b_name
                            //找发布用户
                            holder.perception_perception.text = p.perception
                            val queryUser = BmobQuery<User_bmob>()
                            queryUser.findObjects(object : FindListener<User_bmob>() {
                                override fun done(list: List<User_bmob>, e: BmobException?) {
                                    if (e == null) {
                                        for (i in list) {
                                            if (p.userid.equals(i.objectId)) {
                                                holder.perception_posetPerson.text = i.nickName
//                                                flag = true //找到
                                                Log.d("perception", "find sucessflly")
                                                //加载图片
                                                Glide.with(holder.itemView.context)
                                                    .load(i.face.url) // 确保你的Book对象有正确的图片URL
                                                    .placeholder(R.drawable.pre_load) // 可以设置一个占位图
                                                    .error(R.drawable.fail_load) // 设置加载失败的图
                                                    .into(holder.perception_userIcon)
                                            }
                                        }
                                    } else {
                                        Log.d("perception", "error")
                                    }
                                }
                            })
                            val queryBook = BmobQuery<Book_info_bmob>()
                            queryBook.addWhereEqualTo("name", bookName)
                            queryBook.findObjects(object : FindListener<Book_info_bmob>() {
                                override fun done(list: List<Book_info_bmob>, e: BmobException?) {
                                    if (e == null) {
                                        for (i in list) {
                                            holder.perception_bookName.text=bookName
                                            holder.perception_bookAuthor.text=i.author_name
                                            Glide.with(holder.itemView.context)
                                                .load(i.picture.url) // 确保你的Book对象有正确的图片URL
                                                .placeholder(R.drawable.pre_load) // 可以设置一个占位图
                                                .error(R.drawable.fail_load) // 设置加载失败的图
                                                .into(holder.perception_bookPic)
                                        }
                                    } else {
                                        Log.d("myLog", "error")
                                    }
                                }
                            })
                        }
                    }
                }
            }
        })
        //加载书籍封面
//        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) {
//                            Log.d("myLog", p.b_name)
//                            bookName=p.b_name
//                            holder.perceptionList_bookName.text=bookName
//                            val queryBook = BmobQuery<Book_info_bmob>()
//                            queryBook.findObjects(object : FindListener<Book_info_bmob>() {
//                                override fun done(list: List<Book_info_bmob>, e: BmobException?) {
//                                    if (e == null) {
//                                        for (i in list) {
//                                            if (p.b_name.equals(i.name)) {
//                                                holder.perceptionList_bookAuthor.text=i.author_name
//                                                //加载图片
//                                                object : Thread() {
//                                                    override fun run() {
//                                                        try {
//                                                            val url = URL(i!!.picture?.url)
//                                                            val connection: HttpURLConnection =
//                                                                url.openConnection() as HttpURLConnection
//                                                            connection.setRequestMethod("GET")
//                                                            connection.setConnectTimeout(3000)
//                                                            val `in`: InputStream = connection.getInputStream()
//                                                            val bitmap: Bitmap =
//                                                                BitmapFactory.decodeStream(`in`)
//                                                            holder.perceptionList_bookPic.setImageBitmap(bitmap)
//                                                        } catch (e: Exception) {
//                                                            e.printStackTrace()
//                                                        }
//                                                    }
//                                                }.start()
//
//
//                                            }
//                                        }
//                                    } else {
//                                        Log.d("myLog", "error")
//                                    }
//                                }
//                            })
//                        }
//                    }
//                }
//            }
//        })

        holder.perception_bookInf.setOnClickListener() {
            val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
            intent.putExtra("Book_name", bookName)
            ContextCompat.startActivity(holder.itemView.context, intent, null)
        }
        holder.perception_perception.setOnClickListener {
            val intent = Intent(holder.itemView.context, Perception_informationActivity::class.java)
            intent.putExtra("PerceptionId", holder.perceptionid)
            ContextCompat.startActivity(holder.itemView.context, intent, null)
        }
        //给评论图标设置点击事件
        //给评论图标设置点击事件
//        holder.comment.setOnClickListener(View.OnClickListener { showPopupcomment() })
    }

    override fun getItemCount() = perceptionList.size

}
