package zero.zd.eventlogger.adapter;

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

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import zero.zd.eventlogger.Event;
import zero.zd.eventlogger.R;

public class EventArrayAdapter extends ArrayAdapter<Event> {

    private final Context mContext;
    private final int mResource;
    private final List<Event> mEventList;

    public EventArrayAdapter(Context context, List<Event> eventList) {
        super(context, R.layout.item_event, eventList);
        mContext = context;
        mResource = R.layout.item_event;
        mEventList = eventList;
    }

    @Override
    public int getCount() {
        return mEventList.size();
    }

    @Nullable
    @Override
    public Event getItem(int position) {
        return mEventList.get(position);
    }

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

        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)
                    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(mResource, parent, false);

            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else viewHolder = (ViewHolder) convertView.getTag();

        Event event = mEventList.get(position);

        viewHolder.eventTextView.setText(event.getEvent());
        viewHolder.dateTextView.setText(getStringDate(event));

        return convertView;
    }

    private String getStringDate(Event event) {
        long timeDifference = (new Date().getTime()) - event.getDate().getTime();
        long second = timeDifference / 1000;
        long minute = second / 60;
        long hour = minute / 60;

        if (hour >= 24)
            return event.getStringTime() + " " + event.getStringDate();
        else if (hour > 0)
            return hour + " " + getProperTimeForm("hr", hour);
        else if (minute > 0)
            return minute + " " + getProperTimeForm("min", minute);

        return "a few seconds ago.";
    }

    private String getProperTimeForm(String noun, long value) {
        if (value > 1) noun += "s";
        return noun + " ago.";
    }

    static class ViewHolder {
        @BindView(R.id.text_event)
        TextView eventTextView;
        @BindView(R.id.text_date)
        TextView dateTextView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

@@ -2103,25 +2103,24 @@
 e / 60;%0A
-%0A
         
 if (hour
@@ -2115,23 +2115,29 @@
     
-if (
+long day = 
 hour 
-%3E= 24)
+/ 24;
 %0A   
@@ -2145,159 +2145,385 @@
     
-    return event.getStringTime() + %22 %22 + event.getStringDate();%0A        else if (hour %3E 0)%0A            return hour + %22 %22
+long week = day / 7;%0A%0A        String dateString = %22%22;%0A        if (week %3E 0 && week %3C 4) dateString += getTimeDisplay(%22week%22, week);%0A        else if (day %3E 1 && day %3C 21) dateString += getTimeDisplay(%22day%22, day);%0A        else if (hour %3E 0 && day == 0) dateString += getTimeDisplay(%22hr%22, hour);%0A        else if (minute %3E 0 && hour == 0) dateString
  +
+=
  get
-ProperTimeForm(%22hr%22, hour
+TimeDisplay(%22min%22, minute
 );%0A 
@@ -2549,111 +2549,289 @@
 ute 
-%3E 0)%0A            return minute + %22 %22 + getProperTimeForm(%22min%22, minute);%0A%0A        return %22a few seconds
+== 0) dateString += %22a few seconds ago.%22;%0A        else dateString += event.getStringTime() + %22 %22 + event.getStringDate();%0A%0A        return dateString;%0A    %7D%0A%0A    private String getTimeDisplay(String noun, long value) %7B%0A        return value + %22 %22 + getProperTimeForm(noun, value) + %22
  ago
@@ -2960,26 +2960,16 @@
 urn noun
- + %22 ago.%22
 ;%0A    %7D%0A

Update Display of Time to Include Weeks