package com.zjgsu.jianshu

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.opengl.Visibility
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import android.widget.AdapterView.OnItemLongClickListener
import android.widget.AdapterView.VISIBLE
import android.widget.Toast
import android.view.View.OnLongClickListener

import android.widget.CompoundButton

import android.view.animation.Animation
import com.zjgsu.jianshu.Bmob.Book_bmob


class BookshelfAdapter(val context: Context, val bookshelf: ArrayList<Book_Shelf>) : RecyclerView.Adapter<BookshelfAdapter.ViewHolder>(),
    View.OnLongClickListener, View.OnClickListener {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val book_img: ImageView = view.findViewById(R.id.bookshelf_bkimg)
        val book_name: TextView = view.findViewById(R.id.bookshelf_bkname)
//        val mCheckBox: CheckBox = view.findViewById(R.id.bookshelf_checkBox)
    }
    private var onItemClickListener: RecyclerViewOnItemClickListener? = null
    private var isshowBox: Boolean = false
    private val map: MutableMap<Int, Boolean?> = HashMap()
    private var flag: Boolean = false
    fun initMap() {
        for (i in bookshelf.indices) {
            map[i] = false
        }
        Log.d("initmap", "no")
    }

    fun setSelectItem(position: Int) {
        //对当前状态取反
        if (map[position]!!) {
            map[position] = false
        } else {
            map[position] = true
        }
        notifyItemChanged(position)
    }

    fun setShowBox() {
        //取反
        isshowBox = !isshowBox
    }

    fun getMap(): Map<Int, Boolean?> {
        return map
    }

    //    fun setOnItemLongClickListener(onItemLongClickListener: RecyclerViewOnItemLongClickListener) {
//        this.onItemLongClickListener = onItemLongClickListener;
//    }
//    interface RecyclerViewOnItemClickListener {
//        fun onItemClickListener(view: View?, position: Int)
//    }
//    interface RecyclerViewOnItemLongClickListener {
//        fun onItemLongClickListener(view: View, position: Int): Boolean;
//    }
    interface RecyclerViewOnItemClickListener {
        //点击事件
        fun onItemClickListener(view: View?, position: Int)

        //长按事件
        fun onItemLongClickListener(view: View?, position: Int): Boolean
    }

    fun setRecyclerViewOnItemClickListener(onItemClickListener: RecyclerViewOnItemClickListener?) {
        this.onItemClickListener = onItemClickListener
    }

    override fun onClick(v: View) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            onItemClickListener!!.onItemClickListener(v, v.tag as Int)
        }
    }

    override fun onLongClick(v: View): Boolean {
        initMap()
        return onItemClickListener != null && onItemClickListener!!.onItemLongClickListener(
            v,
            (v.tag as Int)
        )
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.bookshelf_item, parent, false)
        view.setOnLongClickListener(this)
        view.setOnClickListener(this)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//        Log.d("check", isshowBox.toString())
//        if (isshowBox) {
//            holder.mCheckBox.visibility = VISIBLE
//        } else {
//            holder.mCheckBox.visibility = View.INVISIBLE
//        }
//        holder.mCheckBox.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked -> //用map集合保存
//            map[position] = isChecked
//        })
        // 设置CheckBox的状态
        if (map[position] == null) {
            map[position] = false
        }
//        holder.mCheckBox.isChecked = map[position]!!
//        holder.itemView.setOnClickListener{
//            if(!flag){
//            holder.mCheckBox.visibility=View.VISIBLE
//                flag=true
//            }
//            else{
//                holder.mCheckBox.visibility=View.INVISIBLE
//                flag=false
//            }
//        }
        val book = bookshelf[position]
//        if (holder is ViewHolder) {
//            val viewHolder: ViewHolder = holder
//            if (selected == position) {
//                viewHolder.mCheckBox.setChecked(true)
//                viewHolder.itemView.setSelected(true)
//            } else {
//                viewHolder.mCheckBox.setChecked(false)
//                viewHolder.itemView.setSelected(false)
//            }
//            if (onItemClickListener != null) {
//                viewHolder.itemView.setOnClickListener(View.OnClickListener {
//                    onItemClickListener!!.onItemClickListener(
//                        viewHolder.itemView,
//                        viewHolder.adapterPosition
//                    )
//                })
//            }
//        }
        var flag: Boolean = false
        holder.book_img.setTag("1")
        if (book.bkname.equals("添加")) {
            holder.book_img.scaleType = ImageView.ScaleType.CENTER_INSIDE
            holder.book_img.setImageResource(R.drawable.add)
            flag = true
        }
        holder.book_name.text = book.bkname
        val query = BmobQuery<Book_bmob>()
        query.addWhereEqualTo("name", book.bkname)
        query.findObjects(object : FindListener<Book_bmob>() {
            override fun done(p0: MutableList<Book_bmob>?, p1: BmobException?) {
                if (p1 == null) {
                    if (p0 != null && p0.size > 0) {
                        for (p in p0) {
                            object : Thread() {
                                override fun run() {
                                    try {
                                        if (flag) {

                                        } else {
                                            holder.book_img.setTag("0")
                                            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`)
                                            holder.book_img.setImageBitmap(bitmap)
                                        }
                                    } catch (e: Exception) {
                                        e.printStackTrace()
                                    }
                                }
                            }.start()
                        }
                    }
                }
            }
        })
        holder.book_img.setOnClickListener {
            val res = holder.book_img.getTag()
            if (res == "0") {
                val intent = Intent(holder.itemView.context, Book_informationActivity::class.java)
                intent.putExtra("Book_name", book.bkname)
                ContextCompat.startActivity(holder.itemView.context, intent, null)
            } else {
                val intent = Intent(context, MainActivity::class.java)
                context.startActivity(intent)
            }
        }
    }

    override fun getItemCount() = bookshelf.size
}

