package com.project.ilsaenterprise.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.models.Beacon;

import java.util.List;

public class BeaconAdapter extends ArrayAdapter<Beacon> {
    private List<Beacon> monitoringList;
    private Context context;

    public BeaconAdapter (List<Beacon> monitoringList, Context context){
        super(context, R.layout.layout_item_beacon, monitoringList);
        this.monitoringList = monitoringList;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        viewHolder holder;

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.layout_item_beacon, null, true);
        holder = new viewHolder();

        holder.tvEntryId = convertView.findViewById(R.id.tvEntryID);
        holder.tvCreatedAt = convertView.findViewById(R.id.tvCreatedAt);
        holder.tvTemperature = convertView.findViewById(R.id.tvTemperature);
        holder.tvHumidity = convertView.findViewById(R.id.tvHumidity);
        holder.tvPressure = convertView.findViewById(R.id.tvPressure);
        holder.tvCO = convertView.findViewById(R.id.tvCO);
        holder.tvWindSpeed = convertView.findViewById(R.id.tvWindSpeed);
        holder.tvWindDirection = convertView.findViewById(R.id.tvWindDirection);
        holder.tvPrecipitation = convertView.findViewById(R.id.tvPrecipitation);
        holder.tvHeatIndex = convertView.findViewById(R.id.tvHeatIndex);

        convertView.setTag(holder);

        Beacon beacon = monitoringList.get(position);
//        String[] time = TUnits.getTimeAgo(monitoringAlat.getCreated_at());
//
//        String date = time[2] + " " + time[1] + " " + time[0];
//        String clock = String.format("%02d", Integer.parseInt(time[3])) + " : " + String.format("%02d", Integer.parseInt(time[4])) + " : " + String.format("%02d", Integer.parseInt(time[5])) + " WIB";

        holder.tvEntryId.setText(beacon.getEntry_id());
        holder.tvCreatedAt.setText(beacon.getCreated_at());
        holder.tvTemperature.setText(beacon.getTemperature());
        holder.tvHumidity.setText(beacon.getHumidity());
        holder.tvPressure.setText(beacon.getPressure());
        holder.tvCO.setText(beacon.getCO());
        holder.tvWindSpeed.setText(beacon.getWind_speed());
        holder.tvWindDirection.setText(beacon.getWind_direction());
        holder.tvPrecipitation.setText(beacon.getPrecipitation());
        holder.tvHeatIndex.setText(beacon.getHeat_index());

        return convertView;
    }

    static class viewHolder{
        TextView tvEntryId;
        TextView tvCreatedAt;
        TextView tvTemperature;
        TextView tvHumidity;
        TextView tvPressure;
        TextView tvCO;
        TextView tvWindSpeed;
        TextView tvWindDirection;
        TextView tvPrecipitation;
        TextView tvHeatIndex;
    }

}
