package com.zjgsu.jianshu.Activity

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.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 cn.bmob.v3.listener.SaveListener
import com.zjgsu.jianshu.*
import com.zjgsu.jianshu.Bmob.Book_inf_bmob
import com.zjgsu.jianshu.Bmob.Perception_bmob
import com.zjgsu.jianshu.Bmob.User_bmob
import kotlinx.android.synthetic.main.activity_sendperception.*
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

class sendPerceptionActivity:AppCompatActivity() {
    private var bookList = ArrayList<BookAuthor>()
    lateinit var myuserid:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sendperception)
        supportActionBar?.hide()
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        myuserid=intent.getStringExtra("userid").toString()
        inits()
        val layoutManager =
            StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)//第一个参数是列数
        send_recyclerview.layoutManager = layoutManager
        var adapter = send_perceptionAdapter(bookList)
        send_recyclerview.adapter = adapter
        delete.setOnClickListener {
            val intent= Intent(this,PerceptionActivity::class.java)
            startActivity(intent)
            finish()
        }
        submit.setOnClickListener {
            if(selectedBook.equals("")){
                Toast.makeText(this,"请选择一本书！",Toast.LENGTH_SHORT).show()
            }
            else {
                val perception = Perception_bmob()
                perception.perception = mycontent.text.toString()
                perception.userid = myuserid
                perception.b_name = selectedBook
                perception.save(object : SaveListener<String>() {
                    override fun done(objectId: String?, e: BmobException?) {
                        if (e == null) {
                            Toast.makeText(
                                this@sendPerceptionActivity, "发布成功！",
                                Toast.LENGTH_SHORT
                            ).show()
                            val intent =
                                Intent(this@sendPerceptionActivity, PerceptionActivity::class.java)
                            startActivity(intent)
                            finish()
                        } else {
                            Toast.makeText(
                                this@sendPerceptionActivity,
                                "发布失败" + e.message,
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                })
            }
        }
    }
    private fun inits(){
        val bmobQuery = BmobQuery<Book_inf_bmob>()
        bmobQuery.findObjects(object : FindListener<Book_inf_bmob>() {
            override fun done(list: List<Book_inf_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        bookList.add(BookAuthor(i.name, i.author_name))
                        val adapter = send_perceptionAdapter(bookList)
                        send_recyclerview.adapter = adapter
                    }
                } else {
                    Log.d("error", "error")
                }
            }
        })
        val query=BmobQuery<User_bmob>()
        query.findObjects(object:FindListener<User_bmob>(){
            override fun done(list:List<User_bmob>,e:BmobException?){
                if(e==null){
                    for(i in list){
                        if(i.objectId.equals(UserId)) {
                            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`)
                                       touxiang.setImageBitmap(bitmap)
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                }
                            }.start()
                        }
                    }
                }
            }
        })
    }
}