package cmput301f17t09.goalsandhabits.Follow_Activity;

import android.content.Context;
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 java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import cmput301f17t09.goalsandhabits.Main_Habits.Habit;
import cmput301f17t09.goalsandhabits.Main_Habits.HabitEvent;
import cmput301f17t09.goalsandhabits.R;

 * Created by chiasson on 2017-12-03.
 * This ArrayAdapter allows the each habit of each user followed to be displayed in a ListView
public class FollowedHabitsArrayAdapter extends ArrayAdapter<Habit> {
    private Context context;
    private float statThreshold = 50;

    public FollowedHabitsArrayAdapter(Context context, ArrayList<Habit> habits){
        super(context, 0, habits);
        this.context = context;
    public View getView(int position, View convertView, ViewGroup parent){
        Habit h = getItem(position);
        if (convertView==null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.followed_habits_list_item, parent, false);
        TextView owner = (TextView) convertView.findViewById(;
        TextView title = (TextView) convertView.findViewById(;
        TextView reason = (TextView) convertView.findViewById(;
        TextView startDate = (TextView) convertView.findViewById(;
        TextView textStats = (TextView) convertView.findViewById(;
        owner.setText(String.format("%s",h.getProfile())+"'s habit:");
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd", Locale.CANADA);
        ImageView imageView = (ImageView) convertView.findViewById(;
        float possibleEvents = h.getPossibleEvents();
        float stats = ((h.getEventsCompleted()/possibleEvents)*100);
        if (possibleEvents==0) stats=0;
        textStats.setText(String.format("%.0f",stats) + "%");
        if (stats<=statThreshold){
        Calendar c = Calendar.getInstance();
        Calendar latest = getLatestHabitEventDate(h);
        if ((h.getSchedule()!=null && h.getSchedule().contains(c.get(Calendar.DAY_OF_WEEK)) && latest!=null) && (h.getEvents()==null || !sameDay(c,latest))){
            //Set alarm icon if the habit is schedule for today and
            //if we either have no events, or the latest event wasn't today:
        }else {
            //If over half of the possible events have been completed:
            if (stats>statThreshold) {
            } else {

        return convertView;

     * Gets the most recent habit event date
     * @param h Habit
     * @return Most recent habit event date
    private Calendar getLatestHabitEventDate(Habit h){
        ArrayList<HabitEvent> events = h.getEvents();
        if (events==null) return null;
        if (events.isEmpty()) return null;
        Calendar latest = Calendar.getInstance();
        if (events.size()==1) return latest;
        Calendar test = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        for (HabitEvent event : events){
            if (event.getDate()==null) continue;
            if (test.after(latest)){
                if (!test.after(now)){
        return latest;

     * Checks if two dates are the same
     * @param c1 Calendar 1
     * @param c2 Calendar 2
     * @return Whether date is the same
    private boolean sameDay(Calendar c1, Calendar c2){
        return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR));