package com.example.tfat.myapplication.navigationbar.scene

import android.content.Context
import android.graphics.Bitmap
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import com.example.tfat.myapplication.R
import kotlin.collections.ArrayList

class SceneAdapter(var context : Context, var data: ArrayList<Bitmap>, onClickListenerList: ArrayList<View.OnClickListener>, sceneIndex : Int) : RecyclerView.Adapter<SceneAdapter.ViewHolder>() {
    private var onClickListenerList = onClickListenerList
    private var sceneIndex = sceneIndex

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(data?.get(position), position, onClickListenerList, sceneIndex)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v1 = LayoutInflater.from(parent.context).inflate(R.layout.item_scene, parent, false)
        return ViewHolder(v1)
    }

    class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
        fun bind(bitmap: Bitmap?, position : Int, onClickListenerList: ArrayList<View.OnClickListener>, sceneIndex: Int) {
            itemView.findViewById<ImageView>(R.id.scene_image).setImageBitmap(bitmap)
            itemView.findViewById<ImageView>(R.id.scene_image).setOnClickListener(onClickListenerList[position])
            itemView.findViewById<TextView>(R.id.scene_number).text = position.toString()
            if (sceneIndex == position) {
                itemView.findViewById<ImageView>(R.id.edit_view).visibility = View.VISIBLE
            } else {
                itemView.findViewById<ImageView>(R.id.edit_view).visibility = View.INVISIBLE
            }
        }
    }

    fun updateCurrentScene(index : Int) {
        sceneIndex = index
        this.notifyDataSetChanged()
    }

    fun update(data : ArrayList<Bitmap>, onClickListenerList: ArrayList<View.OnClickListener>) {
        this.data = data
        this.onClickListenerList = onClickListenerList
        this.notifyDataSetChanged()
    }
}
