package com.zjgsu.jianshu

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
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.Activity.sendPerceptionActivity
import com.zjgsu.jianshu.Adapter.GoodperceptionAdapter
import com.zjgsu.jianshu.Bmob.Perception_bmob
import kotlinx.android.synthetic.main.activity_perception.*
import kotlinx.android.synthetic.main.layout_bottom_navigation.*
import kotlin.concurrent.thread

class PerceptionActivity : AppCompatActivity() {
    private val PerceptionList = ArrayList<Perception>()
    private lateinit var adapter: PerceptionAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_perception)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        val layoutManager=LinearLayoutManager(this)
        adapter =PerceptionAdapter(PerceptionList)
        perception_recyclerView.layoutManager = layoutManager
        perception_recyclerView.adapter = adapter
        inits()
        perception_swipeRefresh.setOnRefreshListener {
            inits()
        }
        setClickListeners()
        setupBottomNavigation()
    }
    private fun inits() {
        val bmobQuery = BmobQuery<Perception_bmob>()
        bmobQuery.findObjects(object : FindListener<Perception_bmob>() {
            override fun done(list: List<Perception_bmob>, e: BmobException?) {
                if (e == null) {
                    perception_swipeRefresh.isRefreshing = false
                    for (i in list){
                        PerceptionList.add(Perception(i.objectId))
                        val adapter =PerceptionAdapter(PerceptionList)
                        perception_recyclerView.adapter = adapter
                    }
                }
                else {
                    Log.d("error","error")}
            }
        })
    }
    private fun setClickListeners(){
        fab.setOnClickListener {
            val intent=Intent(this,sendPerceptionActivity::class.java)
            startActivity(intent)
        }
    }
    private fun setupBottomNavigation() {
        reading_comprehension.setImageResource(R.drawable.perception2)
        bookcity.setOnClickListener {
            NavigationHelper.navigateTo(this, MainActivity::class.java)
        }
        bookshelf.setOnClickListener {
            NavigationHelper.navigateTo(this, BookShelfActivity::class.java)
        }
        reading_comprehension.setOnClickListener {
            NavigationHelper.navigateTo(this, PerceptionActivity::class.java)
        }
        homepage.setOnClickListener {
            NavigationHelper.navigateTo(this, MyActivity::class.java)
        }
    }
}