package com.zjgsu.jianshu

import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GravityCompat
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.Adapter.BookAdapter2
import com.zjgsu.jianshu.Bmob.Book_bmob
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_kexue.*
import kotlinx.android.synthetic.main.activity_xinli.*
import kotlinx.android.synthetic.main.layout_bottom_navigation.*
import kotlin.concurrent.thread

class kexueActivity:AppCompatActivity() {
    private var bookList = ArrayList<Book>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kexue)
        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setHomeAsUpIndicator(R.drawable.menu)
        }
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        inits()
        val layoutManager =
            StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL)//第一个参数是列数
        kexue_recyclerView.layoutManager = layoutManager
        var adapter = BookAdapter2(bookList)
        kexue_recyclerView.adapter = adapter
        kexue_swipeRefresh.setOnRefreshListener {
            refreshBooks(adapter)
        }
        kexue_navView.setCheckedItem(R.id.kexue)
        kexue_navView.setNavigationItemSelectedListener {
            val intentwenxue = Intent(this, WenXueActivity::class.java)
            val intentguanli = Intent(this, guanliActivity::class.java)
            val intentxinli = Intent(this, xinliActivity::class.java)
            val intentkexue=Intent(this,kexueActivity::class.java)
            val intentzhexue=Intent(this,zhexueActivity::class.java)
            val intentgood=Intent(this,MainActivity::class.java)
            when (it.itemId) {
                R.id.wenxue -> startActivity(intentwenxue)
                R.id.guanli -> startActivity(intentguanli)
                R.id.xinli -> startActivity(intentxinli)
                R.id.kexue->startActivity(intentkexue)
                R.id.zhexue->startActivity(intentzhexue)
                R.id.goodbook->startActivity(intentgood)
            }
            kexue_drawerLayout.closeDrawers()
            true
        }

        kexue_back.setOnClickListener{
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        bookcity.setOnClickListener{
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        bookshelf.setOnClickListener{
            val intent = Intent(this, BookShelfActivity::class.java)
            startActivity(intent)
            finish()
        }
        reading_comprehension.setOnClickListener {
            val intent = Intent(this, PerceptionActivity::class.java)
            startActivity(intent)
            finish()
        }
        homepage.setOnClickListener {
            val intent = Intent(this, MyActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> kexue_drawerLayout.openDrawer(GravityCompat.START)
        }
        return true
    }
    private fun inits() {
        val bmobQuery = BmobQuery<Book_bmob>()
        bmobQuery.findObjects(object : FindListener<Book_bmob>() {
            override fun done(list: List<Book_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        if(i.category_id==4){
                            bookList.add(Book(i.name, i.introduce,i.picture.url))
//                            bookList2.add(Book(i.name, i.introduce))
                            val adapter = BookAdapter2(bookList)
                            kexue_recyclerView.adapter = adapter
                        }
                    }
                } else {
                    Log.d("error", "error")
                }
            }
        })
    }
    private fun refreshBooks(adapter: BookAdapter2) {
        thread {
            Thread.sleep(2000)
            runOnUiThread {
                initBooks()
                adapter.notifyDataSetChanged()
                kexue_swipeRefresh.isRefreshing = false
                Toast.makeText(this, "刷新成功！", Toast.LENGTH_SHORT).show()
            }
        }
    }
    private fun initBooks() {
        bookList.clear()
        val bmobQuery = BmobQuery<Book_bmob>()
        bmobQuery.findObjects(object : FindListener<Book_bmob>() {
            override fun done(list: List<Book_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        if(i.category_id==4)
                            bookList.add(Book(i.name, i.introduce,i.picture.url))
                    }
                } else {
                    Log.d("error", "error")
                }
            }
        })
    }
}