package com.zjgsu.jianshu

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.bmob.v3.Bmob
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.Author_bmob
import com.zjgsu.jianshu.Bmob.Book_info_bmob
import kotlinx.android.synthetic.main.activity_anthor_introduction.*
import kotlinx.android.synthetic.main.activity_book_info.*

class Author_introductionActivity : AppCompatActivity() {
    private var authorworkList = ArrayList<AuthorWork>()
    private lateinit var userId:String
    private lateinit var adapter: Book_authorAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_anthor_introduction)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
        adapter = Book_authorAdapter(authorworkList,userId)
        val layoutManager =
            StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)//第一个参数是列数
        authorwork_recyclerView.layoutManager = layoutManager
        authorwork_recyclerView.adapter = adapter
        inits()
        author_back.setOnClickListener {
            val bookName=intent.getStringExtra("book_name")
            val intent = Intent(this, BookInformationActivity::class.java)
            intent.putExtra("Book_name", bookName)
            startActivity(intent)
            finish()
        }
    }
    private fun inits() {
        val author_name=intent.getStringExtra("author_name")
        book_authorname1.text = author_name
        val authorQuery = BmobQuery<Author_bmob>()
        authorQuery.addWhereEqualTo("author_name",author_name)
        authorQuery.findObjects(object : FindListener<Author_bmob>() {
            override fun done(list: List<Author_bmob>, e: BmobException?) {
                if (e == null) {
                    val author=list[0]
                    author_intro.text=author.author_introduce
                    loadAuthorPic(author.picture.url)
                    val authorworkQuery=BmobQuery<Book_info_bmob>()
                    authorworkQuery.addWhereEqualTo("authorid",author.objectId)
                    authorworkQuery.findObjects(object :FindListener<Book_info_bmob>(){
                        override fun done(books:List<Book_info_bmob>,e:BmobException?){
                            if(e==null){
                                authorworkList.clear()
                                books?.let {
                                    authorworkList.addAll(it.map { b ->
                                        // 使用安全调用和Elvis操作符提供默认值或进行错误处理
                                        val url = b.picture?.url ?: "default_url_or_handling_case"
                                        AuthorWork(b.name,author_name.toString(), url)
                                    })
                                }
                                adapter.notifyDataSetChanged()
                            }
                            else{
                            }
                        }
                    })
                } else {
                    Log.d("error", "error")
                }
//                expand_text_view.text=author_selfintro
            }
        })
    }
    private fun loadAuthorPic(coverUrl: String) {
        Glide.with(this) // 传入 Context
            .load(coverUrl) // 加载图片的 URL
            .placeholder(R.drawable.pre_load) // 设置占位图
            .error(R.drawable.fail_load) // 设置加载失败时显示的图片
            .into(author_img) // 将图片加载到指定的 ImageView 中
    }
}