package com.zjgsu.jianshu

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory

import android.util.Log
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.UpdateListener
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.bmob.v3.listener.SaveListener
import com.zjgsu.jianshu.Activity.BookIntro
import com.zjgsu.jianshu.Adapter.GoodsuibiAdapter
import com.zjgsu.jianshu.Bmob.BookShelf
import com.zjgsu.jianshu.Bmob.Book_inf_bmob
import com.zjgsu.jianshu.Bmob.Perception_bmob
import kotlinx.android.synthetic.main.activity_book_info.*
import kotlinx.android.synthetic.main.title.*


class Book_informationActivity : AppCompatActivity() {
    private var author_name: String = ""
    private val perceptionList = ArrayList<Perception>()
    var Mycomment: Boolean = false
    private lateinit var Bookname: String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_book_info)
        supportActionBar?.hide()
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        Bookname = intent.getStringExtra("Book_name").toString()
        inits()
        val layoutManager =
            StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)//第一个参数是列数
        recyclerView3.layoutManager = layoutManager
        var adapter = GoodsuibiAdapter(perceptionList)
        recyclerView3.adapter = adapter
        val query = BmobQuery<Book_inf_bmob>()
        query.addWhereEqualTo("name", Bookname)
        query.findObjects(object : FindListener<Book_inf_bmob>() {
            override fun done(p0: MutableList<Book_inf_bmob>?, p1: BmobException?) {
                if (p1 == null) {
                    if (p0 != null && p0.size > 0) {
                        for (p in p0) {
                            object : Thread() {
                                override fun run() {
                                    try {
                                        val url = URL(p!!.picture.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`)
                                        book_imageview.setImageBitmap(bitmap)

                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                }
                            }.start()
                            if (p.buxing + p.tuijian + p.buxing != 0) {
                                var tuijian_res: Double = 0.0
                                tuijian_res = (p.tuijian) * 100.0 / (p.buxing + p.tuijian + p.yiban)
                                progressBar_henhao.progress =
                                    p.tuijian * 100 / (p.buxing + p.tuijian + p.yiban)
                                progressBar_yiban.progress =
                                    p.yiban * 100 / (p.buxing + p.tuijian + p.yiban)
                                progressBar_buxing.progress =
                                    p.buxing * 100 / (p.buxing + p.tuijian + p.yiban)
                                val str: String = String.format("%.2f", tuijian_res)
                                textView10.text = "简书推荐值 " + str + "%"
                            }
                        }
                    }
                }
            }
        })
//        val query1 = BmobQuery<Book_video>()
//        query1.addWhereEqualTo("objectId", Bookname)
//        query1.findObjects(object : FindListener<Book_video>() {
//            override fun done(p0: MutableList<Book_video>?, p1: BmobException?) {
//                if (p1 == null) {
//                    if (p0 != null && p0.size > 0) {
//                        for (p in p0) {
//                            object : Thread() {
//                                override fun run() {
//                                    try {
//                                        val url = Uri.parse(p!!.view1.url)
//                                        mVideoView1.setVideoURI(url)
//                                        mVideoView1.start()
//
//                                    } catch (e: Exception) {
//                                        e.printStackTrace()
//                                    }
//                                }
//                            }.start()
//                        }
//                    }
//                }
//            }
//        })
//        query1.findObjects(object : FindListener<Book_video>() {
//            override fun done(p0: MutableList<Book_video>?, p1: BmobException?) {
//                if (p1 == null) {
//                    if (p0 != null && p0.size > 0) {
//                        for (p in p0) {
//                            object : Thread() {
//                                override fun run() {
//                                    try {
//                                        val url = Uri.parse(p!!.view2.url)
//                                        mVideoView2.setVideoURI(url)
//                                        mVideoView2.start()
//                                    } catch (e: Exception) {
//                                        e.printStackTrace()
//                                    }
//                                }
//                            }.start()
//                        }
//                    }
//                }
//            }
//        })
        query.findObjects(object : FindListener<Book_inf_bmob>() {
            override fun done(list: List<Book_inf_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        Book_name_View.text = i.name
                        book_authorname.text = i.author_name
                        author_name=i.author_name
                    }
                } else {
                    Log.d("error", "error")
                }
            }
        })
        book_authorname.setOnClickListener {
            val intent = Intent(this, author_introductionActivity::class.java)
            intent.putExtra("author_name", author_name)
            startActivity(intent)
        }
        b_henhao.setOnClickListener {
            if (Mycomment == false) {
                Mycomment = true
                query.findObjects(object : FindListener<Book_inf_bmob>() {
                    override fun done(list: List<Book_inf_bmob>, e: BmobException?) {
                        if (e == null) {
                            for (i in list) {
                                i.settuijian(i.tuijian + 1)
                                i.update(i.objectId, object : UpdateListener() {
                                    override fun done(p0: BmobException?) {
                                        if (p0 == null) {
                                            Toast.makeText(
                                                this@Book_informationActivity,
                                                "评价成功！",
                                                Toast.LENGTH_LONG
                                            ).show()
                                        } else {
                                            Log.d(
                                                "保存数据！！！！！！！！！！！！！！！！！！",
                                                p0.message.toString()
                                            )
                                        }
                                    }
                                })
                            }
                        } else {
                            Log.d("error", "error")
                        }
                    }
                })
            } else
                Toast.makeText(this, "仅能评价一次！", Toast.LENGTH_SHORT).show()
        }
        b_yiban.setOnClickListener {
            if (Mycomment == false) {
                Mycomment = true
                query.findObjects(object : FindListener<Book_inf_bmob>() {
                    override fun done(list: List<Book_inf_bmob>, e: BmobException?) {
                        if (e == null) {
                            for (i in list) {
                                i.setyiban(i.yiban + 1)
                                i.update(i.objectId, object : UpdateListener() {
                                    override fun done(p0: BmobException?) {
                                        if (p0 == null) {
                                            Toast.makeText(
                                                this@Book_informationActivity,
                                                "评价成功！",
                                                Toast.LENGTH_LONG
                                            ).show()
                                        } else {
                                            Log.d(
                                                "保存数据！！！！！！！！！！！！！！！！！！",
                                                p0.message.toString()
                                            )
                                        }
                                    }
                                })
                            }
                        } else {
                            Log.d("error", "error")
                        }
                    }
                })
            } else
                Toast.makeText(this, "仅能评价一次！", Toast.LENGTH_SHORT).show()
        }
        b_buxing.setOnClickListener {
            if (Mycomment == false) {
                Mycomment = true
                query.findObjects(object : FindListener<Book_inf_bmob>() {
                    override fun done(list: List<Book_inf_bmob>, e: BmobException?) {
                        if (e == null) {
                            for (i in list) {
                                i.setbuxing(i.buxing + 1)
                                i.update(i.objectId, object : UpdateListener() {
                                    override fun done(p0: BmobException?) {
                                        if (p0 == null) {
                                            Toast.makeText(
                                                this@Book_informationActivity,
                                                "评价成功！",
                                                Toast.LENGTH_LONG
                                            ).show()
                                        } else {
                                            Log.d(
                                                "保存数据！！！！！！！！！！！！！！！！！！",
                                                p0.message.toString()
                                            )
                                        }
                                    }
                                })
                            }
                        } else {
                            Log.d("error", "error")
                        }
                    }
                })
            } else
                Toast.makeText(this, "仅能评价一次！", Toast.LENGTH_SHORT).show()
        }
        titleback.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
        titleAddBook.setOnClickListener {
            val query = BmobQuery<BookShelf>()
            query.addWhereEqualTo("userid", UserId)
            query.addWhereEqualTo("b_name", Bookname)
            query.findObjects(object : FindListener<BookShelf>() {
                override fun done(p0: MutableList<BookShelf>?, p1: BmobException?) {
                    if (p1 == null) {
                        if (p0 != null && p0.size > 0) {
                           Toast.makeText(this@Book_informationActivity, "书本已在您的书架中！", Toast.LENGTH_SHORT)
                        } else {
                            val bookshelf_record = BookShelf()
                            bookshelf_record.settb_name(Bookname)
                            bookshelf_record.setuserid(UserId)
                            bookshelf_record.save(object : SaveListener<String>() {
                                override fun done(objectId: String?, e: BmobException?) {
                                    if (e == null) {
                                        titleAddBook.setImageResource(R.drawable.bookshelf2)
                                        Toast.makeText(this@Book_informationActivity,"添加成功",Toast.LENGTH_SHORT).show()
                                    } else {

                                    }
                                }
                            })
                        }
                    }
                }
            })
        }
        book_imageview.setOnClickListener {
            val intent = Intent(this, BookIntro::class.java)
            intent.putExtra("Book_name", Bookname)
            startActivity(intent)
            finish()
        }
    }

    private fun inits() {
        val myquery = BmobQuery<Perception_bmob>()
        myquery.addWhereEqualTo("b_name", Bookname)
        myquery.findObjects(object : FindListener<Perception_bmob>() {
            override fun done(list: List<Perception_bmob>, e: BmobException?) {
                if (e == null) {
                    for (i in list) {
                        perceptionList.add(Perception(i.objectId))
                        val adapter = GoodsuibiAdapter(perceptionList)
                        recyclerView3.adapter = adapter
                    }
                } else {
                    Log.d("error", "error")
                }
            }
        })
        val myquery2 = BmobQuery<BookShelf>()
        myquery2.addWhereEqualTo("userid", UserId)
        myquery2.addWhereEqualTo("b_name", Bookname)
        myquery2.findObjects(object : FindListener<BookShelf>() {
            override fun done(p0: MutableList<BookShelf>?, p1: BmobException?) {
                if (p1 == null) {
                    if (p0 != null && p0.size > 0) {
                        titleAddBook.setImageResource(R.drawable.bookshelf2)
                    }
                }
            }
        })
    }
}