package com.drabarz.karola.raillearn.trip.list;

import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.drabarz.karola.raillearn.R;
import com.drabarz.karola.raillearn.model.Trip;

import java.util.ArrayList;
import java.util.List;

public class TripsGroupAdapter extends BaseAdapter {

    private final List<TripItem> currentTrips = new ArrayList<TripItem>();
    private final List<TripItem> allTrips = new ArrayList<TripItem>();

    private TripsActivity tripsActivity;

    public TripsGroupAdapter(TripsActivity tripsActivity) {
        this.tripsActivity = tripsActivity;
    }

    public void replaceTripItems(List<Trip> trips) {
        currentTrips.clear();
        allTrips.clear();
        for (Trip trip : trips) {
            allTrips.add(new TripItem(trip));
        }
        currentTrips.addAll(allTrips);
        notifyDataSetChanged();
    }

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

    @Override
    public TripItem getItem(int position) {
        return currentTrips.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, final View convertView, ViewGroup viewGroup) {

        final TripItem tripItem = currentTrips.get(position);
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.trip_field, viewGroup, false);
        tripItem.bindLayout(view);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isMyTrip(tripItem.getTrip())) {
                    tripsActivity.startSelectedMyFullTripActivity(tripItem.getTrip());
                } else {
                    tripsActivity.startSelectedOtherFullTripActivity(tripItem.getTrip());
                }
            }
        });

        return view;
    }

    private boolean isMyTrip(Trip trip) {
        String myId = PreferenceManager.getDefaultSharedPreferences(tripsActivity).getString("user_id", null);
        String tripUserId = trip.getUser().getId();

        return myId.equals(tripUserId);
    }

    public void showAll() {
        currentTrips.clear();
        currentTrips.addAll(allTrips);
        notifyDataSetChanged();
    }

    public void showMyOffers() {
        currentTrips.clear();
        for(TripItem tripItem: allTrips) {
            if(isMyTrip(tripItem.getTrip())) {
                currentTrips.add(tripItem);
            }
        }
        notifyDataSetChanged();
    }

    public void showMyRequests() {
        currentTrips.clear();
        for(TripItem tripItem: allTrips) {
            if(isMyJoinedTrip(tripItem.getTrip())) {
                currentTrips.add(tripItem);
            }
        }
        notifyDataSetChanged();
    }

    private boolean isMyJoinedTrip(Trip trip) {
        String myId = PreferenceManager.getDefaultSharedPreferences(tripsActivity).getString("user_id", null);
        String joinedUserId = trip.getJoinedUser();

        return myId.equals(joinedUserId);
    }

    public void showOtherOffers() {
        currentTrips.clear();
        for (TripItem tripItem : allTrips) {
            if(!isMyTrip(tripItem.getTrip()) && !isMyJoinedTrip(tripItem.getTrip())) {
                currentTrips.add(tripItem);
            }
        }
        notifyDataSetChanged();
    }
}

@@ -1716,32 +1716,33 @@
               if
+ 
 (isMyTrip(tripIt
@@ -1750,32 +1750,32 @@
 m.getTrip())) %7B%0A
-
                 
@@ -1837,32 +1837,193 @@
 tem.getTrip());%0A
+                %7D else if (isMyJoinedTrip(tripItem.getTrip())) %7B%0A                    tripsActivity.startSelectedMyRequestedFullTripActivity(tripItem.getTrip());%0A
                 
@@ -2439,32 +2439,293 @@
 UserId);%0A    %7D%0A%0A
+    private boolean isMyJoinedTrip(Trip trip) %7B%0A        String myId = PreferenceManager.getDefaultSharedPreferences(tripsActivity).getString(%22user_id%22, null);%0A        String joinedUserId = trip.getJoinedUser();%0A%0A        return myId.equals(joinedUserId);%0A    %7D%0A%0A
     public void 
@@ -3325,32 +3325,32 @@
     %7D%0A        %7D%0A
+
         notifyDa
@@ -3376,269 +3376,8 @@
  %7D%0A%0A
-    private boolean isMyJoinedTrip(Trip trip) %7B%0A        String myId = PreferenceManager.getDefaultSharedPreferences(tripsActivity).getString(%22user_id%22, null);%0A        String joinedUserId = trip.getJoinedUser();%0A%0A        return myId.equals(joinedUserId);%0A    %7D%0A%0A
     

Fix add TripsGroupAdapter changes