package com.kuckovsky.richard.home;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by richard on 5.6.17.
 */

public class roomSensorAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private JSONArray mDataSource;

    public roomSensorAdapter(Context context, JSONArray items) {
        mContext = context;
        mDataSource = items;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mDataSource.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mDataSource.getJSONObject(position);
        } catch (JSONException e) {
            return 0;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = mInflater.inflate(R.layout.list_view_room_sensor, parent, false);

        TextView title = (TextView) rowView.findViewById(R.id.sensor_name);
        TextView value = (TextView) rowView.findViewById(R.id.sensor_value);
        TextView unit = (TextView) rowView.findViewById(R.id.sensor_unit);
        try {
            JSONObject sensor = mDataSource.getJSONObject(position);

            title.setText(sensor.getString("sensor_name"));
            value.setText(sensor.getString("value"));
            unit.setText(sensor.getString("unit"));

        } catch (JSONException e) {
            e.printStackTrace();
        }

        //title.setTextColor(Color.BLACK);
        return rowView;
    }
}
