package com.zjgsu.jianshu

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.Toast
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.zjgsu.jianshu.Bmob.Book_inf_bmob
import kotlinx.android.synthetic.main.activity_anthor_introduction.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.book_author_item.*
import kotlinx.android.synthetic.main.expandable.*
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

class author_introductionActivity : AppCompatActivity() {
    private var bookList = ArrayList<BookAuthor>()
    private var author_selfintro:String=""
    private val url_list = mutableListOf<String>()
    private var flag: Int =0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_anthor_introduction)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        book_authorname1.text = intent.getStringExtra("author_name")
        inits()
        val layoutManager =
            StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)//第一个参数是列数
        recyclerView1.layoutManager = layoutManager
        var adapter = Book_authorAdapter(bookList)
        recyclerView1.adapter = adapter
        expand_collapse.setOnClickListener{
            val myDialog:MyDialog= MyDialog(this)
            myDialog.setTitle("作者介绍")
            myDialog.setMessage(author_selfintro)
            myDialog.setCancel("cancel",MyDialog.IOnCancelListener {
                fun onCancel(dialog:MyDialog) {
                    Toast.makeText(this,"取消",Toast.LENGTH_SHORT).show()
                }
            })
            myDialog.setConfirm("confirm",MyDialog.IOnConfirmListener {
                fun OnConfirm(dialog:MyDialog){
                    Toast.makeText(this,"确认",Toast.LENGTH_SHORT).show()
                }
            })
            myDialog.show()
        }
    }
    private fun inits() {
        val bmobQuery = BmobQuery<Book_inf_bmob>()
        bmobQuery.addWhereEqualTo("author_name",book_authorname1.text.toString())
        bmobQuery.findObjects(object : FindListener<Book_inf_bmob>() {
            override fun done(list: List<Book_inf_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        if(flag==0){
                            author_selfintro=i.author_introduce
                            flag=1
                            url_list.add(i!!.picture.url)
                            //加载authorPic
                            object : Thread() {
                                override fun run() {
                                    try {
                                        //作者
                                        val url = URL(i!!.authorpic.url)
                                        Log.d("myLog", url.toString())
                                        val connection: HttpURLConnection =
                                            url.openConnection() as HttpURLConnection
                                        connection.setRequestMethod("GET")
                                        connection.setConnectTimeout(5000)
                                        val `in`: InputStream = connection.getInputStream()
                                        val bitmap: Bitmap =
                                            BitmapFactory.decodeStream(`in`)
//                                Log.d("myLog","fine")
                                        //yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
                                        author_img.setImageBitmap(bitmap)
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                }
                            }.start()
                        }
                        bookList.add(BookAuthor(i.name, i.author_name))
                        val adapter = Book_authorAdapter(bookList)
                        recyclerView1.adapter = adapter
                    }
                } else {
                    Log.d("error", "error")
                }
                expand_text_view.text=author_selfintro
            }
        })
    }
}