package edu.whut.jianshu.Activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
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 edu.whut.jianshu.Adapter.PerceptionAdapter
import edu.whut.jianshu.Data.entity.Perception_bmob
import edu.whut.jianshu.Data.vo.Perception
import edu.whut.jianshu.NavigationHelper
import edu.whut.jianshu.R
import kotlinx.android.synthetic.main.activity_perception.*
import kotlinx.android.synthetic.main.layout_bottom_navigation.*
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)
        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)
        }
        homepage.setOnClickListener {
            NavigationHelper.navigateTo(this, MyActivity::class.java)
        }
    }
}