package it.polito.mad.countonme;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.ValueEventListener;

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

import it.polito.mad.countonme.database.DataManager;
import it.polito.mad.countonme.lists.SharingActivitiesAdapter;
import it.polito.mad.countonme.models.SharingActivity;

public class SharingsListFragment extends Fragment implements ValueEventListener {
    private RecyclerView mSharActsRv;
    private SharingActivitiesAdapter mSharActsAdapter;
    private List<SharingActivity> mSharActsList;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sharing_list_fragment, container, false);
        mSharActsRv = ( RecyclerView ) view.findViewById( R.id.sharing_activity_list );
        mSharActsList = new ArrayList<SharingActivity>();
        mSharActsList.add( new SharingActivity( "Activity name", "A short description",  null, "euro"));
        mSharActsAdapter = new SharingActivitiesAdapter( getActivity(), mSharActsList );
        mSharActsRv.setAdapter( mSharActsAdapter );
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        DataManager.getsInstance().getSharingActivitiesReference().addValueEventListener( this );
    }

    @Override
    public void onStop() {
        super.onStop();
        DataManager.getsInstance().getSharingActivitiesReference().removeEventListener( this );
    }

    @Override
    public void onDataChange( DataSnapshot dataSnapshot ) {
        //mSharActsList.clear();
        mSharActsList.add( new SharingActivity( "Activity name", "A short description",  null, "euro"));
        mSharActsAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
}

@@ -114,16 +114,70 @@
 Bundle;%0A
+import android.support.v7.widget.LinearLayoutManager;%0A
 import a
@@ -1431,33 +1431,33 @@
    mSharActs
-List.add(
+Adapter =
  new Sharing
@@ -1467,153 +1467,261 @@
 ivit
-y( %22Activity name%22, %22A short description%22,  null, %22euro%22));%0A        mSharActsAdapter = new SharingActivitiesAdapter( getActivity(), mSharActsList
+iesAdapter( getActivity(), mSharActsList );%0A        final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());%0A        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);%0A        mSharActsRv.setLayoutManager( layoutManager
  );%0A
@@ -2228,10 +2228,37 @@
     
-//
+SharingActivity tmp;%0A        
 mSha
@@ -2288,104 +2288,238 @@
     
-mSharActsList.add( new SharingActivity( %22Activity name%22, %22A short description%22,  null, %22euro%22));
+for ( DataSnapshot data : dataSnapshot.getChildren() ) %7B%0A            tmp = (SharingActivity) data.getValue( SharingActivity.class );%0A            tmp.setKey( (String) data.getKey() );%0A            mSharActsList.add( tmp );%0A        %7D
 %0A   

add data retrieval form server