package com.zjgsu.jianshu

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
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.BookShelf
import kotlinx.android.synthetic.main.activity_bookshelf.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_perception.*
import kotlinx.android.synthetic.main.bookshelf_item.*
import kotlinx.android.synthetic.main.expandable.*
import kotlin.concurrent.thread
import com.google.android.material.snackbar.Snackbar

import android.view.LayoutInflater
import android.view.View.OnLongClickListener

import android.view.ViewGroup

import android.widget.TextView

import androidx.recyclerview.widget.RecyclerView
import android.view.MotionEvent
import android.view.View.OnTouchListener
import kotlinx.android.synthetic.main.layout_bottom_navigation.*

class BookShelfActivity : AppCompatActivity() {
    val Booklist = ArrayList<Book_Shelf>()
    val Booklist2 = ArrayList<Book_Shelf>()
    lateinit var adapter:BookshelfAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bookshelf)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        inits()
        val layoutManager=GridLayoutManager(this,3)
        recyclerView2.layoutManager=layoutManager
        adapter=BookshelfAdapter(this,Booklist)
        recyclerView2.adapter=adapter
        adapter.setRecyclerViewOnItemClickListener(object : BookshelfAdapter.RecyclerViewOnItemClickListener {
            override fun onItemClickListener(view: View?, position: Int) {
                                Log.d("zyzh","12311")
                adapter.setSelectItem(position)
            }

            override fun onItemLongClickListener(view: View?, position: Int): Boolean {
                Log.d("zy11","123121")
                adapter.setShowBox()
                adapter.setSelectItem(position)
                adapter.notifyDataSetChanged()
                return true
            }
        })

        swipeRefresh2.setOnRefreshListener {
            refreshBookShelf(adapter)
        }
        setupBottomNavigation()
    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.nav_bookshelf,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.getItemId()) {
            R.id.addBook-> {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            }
           R.id.editor->{
               adapter.initMap()
               adapter.setShowBox()
               adapter.notifyDataSetChanged()
//               bookshelf_checkBox.visibility=View.VISIBLE
           }
        }
        return super.onContextItemSelected(item);
    }
    private fun inits() {
        val bmobQuery = BmobQuery<BookShelf>()
        bmobQuery.addWhereEqualTo("userid", UserId)
        bmobQuery.findObjects(object : FindListener<BookShelf>() {
            override fun done(list: List<BookShelf>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        Booklist.add(Book_Shelf(i.b_name))
                        val adapter =BookshelfAdapter(this@BookShelfActivity,Booklist)
                        recyclerView2.adapter = adapter
                    }
                    Booklist.add(Book_Shelf("添加"))
                    Booklist2.add(Book_Shelf("添加"))
                    val adapter =BookshelfAdapter(this@BookShelfActivity,Booklist)
                    recyclerView2.adapter = adapter
                }
            }
        })
    }
    private fun refreshBookShelf(adapter: BookshelfAdapter) {
        thread {
            Thread.sleep(2000)
            runOnUiThread {
                initbookshelf()
                adapter.notifyDataSetChanged()
                swipeRefresh2.isRefreshing = false
                Toast.makeText(this, "刷新成功！", Toast.LENGTH_SHORT).show()
            }
        }
    }
    private fun initbookshelf(){
        Booklist.clear()
        val bmobQuery = BmobQuery<BookShelf>()
        bmobQuery.addWhereEqualTo("userid", UserId)
        bmobQuery.findObjects(object : FindListener<BookShelf>() {
            override fun done(list: List<BookShelf>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        Booklist.add(Book_Shelf(i.b_name))
                    }
                    Booklist.add(Book_Shelf("添加"))
                    val adapter =BookshelfAdapter(this@BookShelfActivity,Booklist)
                    recyclerView2.adapter = adapter
                }else {
                    Log.d("error", "error")
                }
            }
        })
    }
    private fun setupBottomNavigation() {
        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)
        }
    }
}