package com.aajtech.mobile.goproshowcase

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.aajtech.mobile.goproshowcase.dto.GoProStatusDTO

/**
 * [RecyclerView.Adapter] that can display a [GoProStatusDTO] and makes a call to the
 * specified [OnListFragmentInteractionListener].
 */
class MyGoProStatusDTORecyclerViewAdapter(private val mValues: List<GoProStatusDTO>, private val mListener: GoProStatusDTOFragment.OnListFragmentInteractionListener?) : RecyclerView.Adapter<MyGoProStatusDTORecyclerViewAdapter.ViewHolder>() {

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mItem = mValues[position]
        holder.mIdView.text = mValues[position].propName
        holder.mContentView.text = mValues[position].propValue.toString()

        holder.mView.setOnClickListener {
            mListener?.onListFragmentInteraction(holder.mItem!!)
        }
    }

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

    inner class ViewHolder(val mView: View) : RecyclerView.ViewHolder(mView) {
        val mIdView: TextView
        val mContentView: TextView
        var mItem: GoProStatusDTO? = null

        init {
            mIdView = mView.findViewById(R.id.id) as TextView
            mContentView = mView.findViewById(R.id.content) as TextView
        }

        override fun toString(): String {
            return super.toString() + " '" + mContentView.text + "'"
        }
    }
}
