<commit_before>package nl.mprog.friendsandfood.Activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.facebook.Profile;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nl.mprog.friendsandfood.Adapters.CustomAdapterRatingBar;
import nl.mprog.friendsandfood.R;
import nl.mprog.friendsandfood.Classes.Review;

/**
 * Created by <NAME> on 12-1-2017.
 * In this activity, the user is presented will all the infomation about a selected restaurant.
 * Here, the user can see the name, rating by friends, reviews by friends and friends who have
 * checked in. The user themselves can navigate to WriteReviewActivity by clicking the WriteReview
 * button and check in/out. Furthermore, the user can click on a review of a friend and navigate
 * to ReadReviewActivity and read the review a friend has written.
 */

public class SelectedRestaurantActivity extends BaseActivity implements View.OnClickListener{
    private ListView listViewCheckIn;
    private ListView listViewRatingBar;
    private List<Review> reviewList;

    private String restaurantID;
    private String restaurantName;

    private FirebaseDatabase database = FirebaseDatabase.getInstance();
    private ArrayList<String> mFriendsCompleteNames = new ArrayList<>();
    private ArrayList<String> mFriendsID = new ArrayList<>();
    private ArrayList<String> allReviewIDs = new ArrayList<>();
    private HashMap<String, HashMap<String, String>> allReviewsHash = new HashMap<>();

    private RatingBar ratingBar;
    final String profile = Profile.getCurrentProfile().getId();
    private ToggleButton toggle;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selected_restaurant);

        //Intent from previous activity.
        Intent intent = getIntent();
        restaurantName = intent.getStringExtra("restaurantName");
        restaurantID = intent.getStringExtra("restaurantID");

        //The Views.
        ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        listViewRatingBar = (ListView)findViewById(R.id.listViewReviewFriends);
        TextView name = (TextView) findViewById(R.id.selected_restaurant_name);
        findViewById(R.id.submit).setOnClickListener(this);
        name.setText(restaurantName);

        getUserFriendsValueEventListener();
        clickSelectReviewFriend();
        setToggleButton();

    }

    /** This method saves the check in status of the user to Firebase*/
    public void setToggleButton(){
        final DatabaseReference mRefCheckins =  database.getReference("checkin").child(restaurantID);
        toggle = (ToggleButton) findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    String time = String.valueOf(System.currentTimeMillis());
                    mRefCheckins.child(profile).setValue(createHashToggle(time));
                } else {
                    mRefCheckins.child(profile).removeValue();
                }
            }
        });
    }

    /** Saves all the data in a map so it can be saved in one go, this way the listener doesn't
     * retrieve incomplete data. */
    public Map<String, Object> createHashToggle(String time){
        Map<String, Object> activityInfo = new HashMap<>();
        activityInfo.put("Time", time);
        activityInfo.put("RestaurantName", restaurantName);
        activityInfo.put("RestaurantID", restaurantID);
        activityInfo.put("User", profile);
        return activityInfo;
    }

    /** First EventListener which gets all the friends of the user and calls addFriendInformation */
    public void getUserFriendsValueEventListener(){
        DatabaseReference mRefFriends = database.getReference("users").child(profile).child("friends");
        mRefFriends.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child: dataSnapshot.getChildren()) {
                    String friendName = child.getValue().toString();
                    String friendID = child.getKey();
                    addFriendInformation(friendName, friendID);
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

    /** Saves data of friends in ArrayLists. */
    public void addFriendInformation(String friendName, String friendID){
        mFriendsCompleteNames.add(friendName);
        mFriendsID.add(friendID);
        findReviews(mFriendsID);
        findCheckin(mFriendsID);
    }

    /** Finds all the reviews of friends from Firebase. */
    public void findReviews(final ArrayList<String> friends){
        DatabaseReference mRefReviews =  database.getReference("reviews");
        mRefReviews.addValueEventListener(new ValueEventListener() {
            ArrayList<String> friendReviewWriterNames = new ArrayList<String>();
            //The score is for the ratingbar.
            int totalscore = 0;<commit_msg>Refractoring and fixed bug in OwnReviewsActivity.<commit_after>package nl.mprog.friendsandfood.Activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.facebook.Profile;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nl.mprog.friendsandfood.Adapters.CustomAdapterRatingBar;
import nl.mprog.friendsandfood.R;
import nl.mprog.friendsandfood.Classes.Review;

/**
 * Created by <NAME> on 12-1-2017.
 * In this activity, the user is presented will all the infomation about a selected restaurant.
 * Here, the user can see the name, rating by friends, reviews by friends and friends who have
 * checked in. The user themselves can navigate to WriteReviewActivity by clicking the WriteReview
 * button and check in/out. Furthermore, the user can click on a review of a friend and navigate
 * to ReadReviewActivity and read the review a friend has written.
 */

public class SelectedRestaurantActivity extends BaseActivity implements View.OnClickListener{
    private ListView listViewCheckIn;
    private ListView listViewRatingBar;
    private List<Review> reviewList;

    private String restaurantID;
    private String restaurantName;

    private FirebaseDatabase database = FirebaseDatabase.getInstance();
    private DatabaseReference mRefFriends;

    private ArrayList<String> mFriendsCompleteNames = new ArrayList<>();
    private ArrayList<String> mFriendsID = new ArrayList<>();
    private ArrayList<String> allReviewIDs = new ArrayList<>();
    private HashMap<String, HashMap<String, String>> allReviewsHash = new HashMap<>();

    private RatingBar ratingBar;
    final String profile = Profile.getCurrentProfile().getId();
    private ToggleButton toggle;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selected_restaurant);

        //Intent from previous activity.
        Intent intent = getIntent();
        restaurantName = intent.getStringExtra("restaurantName");
        restaurantID = intent.getStringExtra("restaurantID");

        //The Views.
        ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        listViewRatingBar = (ListView)findViewById(R.id.listViewReviewFriends);
        TextView name = (TextView) findViewById(R.id.selected_restaurant_name);
        findViewById(R.id.submit).setOnClickListener(this);
        name.setText(restaurantName);

        mRefFriends = database.getReference("users").child(profile).child("friends");
        
        getUserFriendsValueEventListener();
        clickSelectReviewFriend();
        setToggleButton();

    }

    /** This method saves the check in status of the user to Firebase*/
    public void setToggleButton(){
        final DatabaseReference mRefCheckins =  database.getReference("checkin").child(restaurantID);
        toggle = (ToggleButton) findViewById(R.id.toggleButton);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    String time = String.valueOf(System.currentTimeMillis());
                    mRefCheckins.child(profile).setValue(createHashToggle(time));
                } else {
                    mRefCheckins.child(profile).removeValue();
                }
            }
        });
    }

    /** Saves all the data in a map so it can be saved in one go, this way the listener doesn't
     * retrieve incomplete data. */
    public Map<String, Object> createHashToggle(String time){
        Map<String, Object> activityInfo = new HashMap<>();
        activityInfo.put("Time", time);
        activityInfo.put("RestaurantName", restaurantName);
        activityInfo.put("RestaurantID", restaurantID);
        activityInfo.put("User", profile);
        return activityInfo;
    }

    /** First EventListener which gets all the friends of the user and calls addFriendInformation */
    public void getUserFriendsValueEventListener(){
        mRefFriends.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot child: dataSnapshot.getChildren()) {
                    String friendName = child.getValue().toString();
                    String friendID = child.getKey();
                    addFriendInformation(friendName, friendID);
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

    /** Saves data of friends in ArrayLists. */
    public void addFriendInformation(String friendName, String friendID){
        mFriendsCompleteNames.add(friendName);
        mFriendsID.add(friendID);
        findReviews(mFriendsID);
        findCheckin(mFriendsID);
    }

    /** Finds all the reviews of friends from Firebase. */
    public void findReviews(final ArrayList<String> friends){
        DatabaseReference mRefReviews =  database.getReference("reviews");
        mRefReviews.addValueEventListener(new ValueEventListener() {
            ArrayList<String> friendReviewWriterNames = new ArrayList<String>();
            //The score is for the ratingbar.
            int totalscore = 0;
{'fraction_non_alphanumeric': 0.08738595463296225, 'fraction_numerical': 0.0013392483468653218, 'mean_word_length': 3.3494721514379324, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3024628', 'n_tokens_mistral': 3039, 'n_tokens_neox': 2909, 'n_words': 790}