package openfoodfacts.github.scrachx.openfood.views.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.HistoryItem;
import openfoodfacts.github.scrachx.openfood.views.holders.HistoryScanHolder;

public class HistoryListAdapter extends RecyclerView.Adapter<HistoryScanHolder> {

    private final List<HistoryItem> list;
    private final String productUrl;
    private Activity mActivity;

    public HistoryListAdapter(List<HistoryItem> list, String productUrl, Activity activity) {
        this.list = list == null ? Collections.<HistoryItem>emptyList() : list;
        this.productUrl = productUrl;
        this.mActivity = activity;
    }

    @Override
    public HistoryScanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Inflate the layout, initialize the View Holder
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
        return new HistoryScanHolder(v, productUrl, mActivity);
    }

    @Override
    public void onBindViewHolder(HistoryScanHolder holder, int position) {

        //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
        holder.txtTitle.setText(list.get(position).getTitle());
        holder.txtBarcode.setText(list.get(position).getBarcode());
        holder.txtBrands.setText(list.get(position).getBrands());
        holder.imgProduct.setImageBitmap(list.get(position).getUrl());

        Date date = list.get(position).getTime();

        long time = date.getTime();
        String timeStamp = (String)DateUtils.getRelativeTimeSpanString(time);
        holder.txtDate.setText(timeStamp);

        //animate(holder);
    }

    @Override
    public int getItemCount() {
        //returns the number of elements the RecyclerView will display
        return list.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    // Insert a new item to the RecyclerView on a predefined position
    public void insert(int position, HistoryItem data) {
        list.add(position, data);
        notifyItemInserted(position);
    }

    // Remove a RecyclerView item containing a specified Data object
    public void remove(HistoryItem data) {
        int position = list.indexOf(data);
        list.remove(position);
        notifyItemRemoved(position);
    }

}

@@ -399,16 +399,54 @@
 .Locale;
+%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aimport
@@ -1944,25 +1944,24 @@
 Time();%0A
-%0A
         
 long tim
@@ -1956,154 +1956,29 @@
     
-long time = date.getTime();%0A        String timeStamp = (String)DateUtils.getRelativeTimeSpanString(time);%0A        holder.txtDate.setText(timeStamp
+calcTime(date, holder
 );%0A%0A
@@ -2737,17 +2737,1031 @@
 ition);%0A
-
     %7D%0A%0A
+%0A    public void calcTime(Date date, HistoryScanHolder holder) %7B%0A%0A%0A        Date now = new Date();%0A        long seconds = TimeUnit.MILLISECONDS.toSeconds(now.getTime() - date.getTime());%0A        long minutes = TimeUnit.MILLISECONDS.toMinutes(now.getTime() - date.getTime());%0A        long hours = TimeUnit.MILLISECONDS.toHours(now.getTime() - date.getTime());%0A        long days = TimeUnit.MILLISECONDS.toDays(now.getTime() - date.getTime());%0A%0A%0A        String secText = String.valueOf(seconds) + %22 seconds ago%22;%0A        String minText = String.valueOf(minutes) + %22 minutes ago%22;%0A        String hourText = String.valueOf(hours) + %22 hours ago%22;%0A        String dayText = String.valueOf(days) + %22 days ago%22;%0A%0A        if (seconds %3C 60) %7B%0A            holder.txtDate.setText(secText);%0A        %7D else if (minutes %3C 60) %7B%0A            holder.txtDate.setText(minText);%0A        %7D else if (hours %3C 24) %7B%0A            holder.txtDate.setText(hourText);%0A        %7D else %7B%0A            holder.txtDate.setText(dayText);%0A        %7D%0A    %7D%0A%0A%0A
 %7D%0A

Fix TimeStamp in history list (#941)