package net.heartshield.prevent;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.heartshield.data.MeasurementSummary;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author David Madl (git@abanbytes.eu)
 * @date 2017-04-20
 */
public class ResultLineAdapter extends ArrayAdapter<MeasurementSummary> {
    private List<MeasurementSummary> mEntries;
    private LayoutInflater mInflater;
    private java.text.DateFormat mDateFormat;
    private java.text.DateFormat mTimeFormat;

    public ResultLineAdapter(@NonNull Context context, MeasurementSummary[] entries) {
        super(context, R.layout.rowlayout_result, entries);

        mEntries = new ArrayList<>(Arrays.asList(Arrays.copyOf(entries, entries.length)));
        Collections.reverse(mEntries);

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mDateFormat = DateFormat.getDateFormat(context.getApplicationContext());
        mTimeFormat = DateFormat.getTimeFormat(context.getApplicationContext());
    }

    static class ViewHolder {
        public ImageView icon;
        public TextView heartScore;
        public TextView firstLine;
        public TextView secondLine;
    }

    // only call this from UI thread!
    public void add(MeasurementSummary measurement) {
        mEntries.add(0, measurement);
        //insert(measurement, 0);
        notifyDataSetChanged();
    }

    // only call this from UI thread!
    public void update(MeasurementSummary measurement) {
        int i = mEntries.indexOf(measurement);
        if(i == -1)
            throw new IllegalArgumentException("measurement " + measurement.getId().toString() + " not found");
        mEntries.set(i, measurement);
        //insert(measurement, 0);
        notifyDataSetChanged();
    }

    @Nullable
    @Override
    public MeasurementSummary getItem(int position) {
        return mEntries.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View rowView = convertView;

        //View rowView = mInflater.inflate(R.layout.rowlayout_result, parent, false);

        int[] heartIconRes = {
                R.drawable.help_small, // should be consistent with ResultListActivity -> ResultLineAdapter
                R.drawable.heart_ok,
                R.drawable.heart_only,
                R.drawable.heart_nok
        };

        if(rowView == null) {
            //rowView = mInflater.inflate(R.layout.rowlayout_result, null); // NO!

            //Log.i("ResultLineAdapter", "inflating position=" + position);
            rowView = mInflater.inflate(R.layout.rowlayout_result, parent, false);

            ViewHolder viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) rowView.findViewById(R.id.icon);
            viewHolder.heartScore = (TextView) rowView.findViewById(R.id.heartScore);
            viewHolder.firstLine = (TextView) rowView.findViewById(R.id.firstLine);
            viewHolder.secondLine = (TextView) rowView.findViewById(R.id.secondLine);
            rowView.setTag(viewHolder);
        }

        ViewHolder viewHolder = (ViewHolder) rowView.getTag();

        MeasurementSummary entry = mEntries.get(position);
        Date timeStamp = new java.util.Date(entry.startTime * 1000);

        // performance note: make sure the image is small enough for quick rendering
        // scroll performance will suffer otherwise.

        viewHolder.secondLine.setText("#" + entry.number + " on " + mDateFormat.format(timeStamp) + " at " + mTimeFormat.format(timeStamp));

        viewHolder.icon.setImageResource(heartIconRes[entry.riskLevel]);

        if(entry.status == MeasurementSummary.Status.OK) {
            String score = Integer.toString(entry.heartScore);
            viewHolder.heartScore.setText(score);
            viewHolder.firstLine.setText(entry.bpm + " bpm");
        } else if(entry.status == MeasurementSummary.Status.UNCLASSIFIED) {
            // should be consistent with ResultActivity UNCLASSIFIED
            viewHolder.heartScore.setText(" -");
            viewHolder.firstLine.setText(" - bpm");
        } else if(entry.status == MeasurementSummary.Status.OFFLINE) {
            viewHolder.icon.setImageResource(R.drawable.offline); // nice-to: improve icons
            viewHolder.heartScore.setText(" -");
            viewHolder.firstLine.setText(" - bpm");
        }

        return rowView;
    }
}
