package com.wdkl.app.ncs.callingbed.adapter

import android.content.Context
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.wdkl.app.ncs.callingbed.R
import com.wdkl.ncs.android.middleware.model.dto.NurseConfigDto
import java.lang.Exception

class NurseConfigAdpter : RecyclerView.Adapter<NurseConfigAdpter.ViewHolder> {
    private var context: Context
    private var data: ArrayList<NurseConfigDto>

    constructor(context: Context, data: ArrayList<NurseConfigDto>) {
        this.context = context
        this.data = data
    }

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

        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        try {
            holder.nurseColor.setBackgroundColor(Color.parseColor("#"+data.get(position).nurseColorRbg))
            holder.nurseName.setText(data.get(position).nurseConfigName)
            holder.nurseValue.setText(data.get(position).nurseOptionName)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

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

    fun updateData(data: ArrayList<NurseConfigDto>) {
        this.data = data
        notifyDataSetChanged()
    }

    class ViewHolder: RecyclerView.ViewHolder {
        var nurseColor : TextView
        var nurseName : TextView
        var nurseValue : TextView

        constructor(itemView: View): super(itemView) {
            nurseColor = itemView.findViewById(R.id.tv_nurse_color)
            nurseName = itemView.findViewById(R.id.tv_nurse_config_name)
            nurseValue = itemView.findViewById(R.id.tv_nurse_config_value)
        }
    }
}