package app.insti.fragment;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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 com.google.firebase.storage.StorageReference;

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

import app.insti.R;
import app.insti.Utils;
import app.insti.adapter.SubActivitiesAdapter;
import app.insti.api.model.SubActivity;

public class SubActivityFragment extends Fragment {

    private StorageReference storageReference2;
    private DatabaseReference databaseReference2;
    private List<SubActivity> subActivityList;

    public SubActivityFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sub_activity, container, false);


        Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
        toolbar.setTitle("Sub Activities");
        Utils.setSelectedMenuItem(getActivity(), R.id.nav_sub_activities);


        RecyclerView subActivitiesList = view.findViewById(R.id.subActivitiesList);
        subActivitiesList.setLayoutManager(new LinearLayoutManager(getContext()));

        subActivityList = new ArrayList<>();

        databaseReference2 = FirebaseDatabase.getInstance().getReference("sub_activities");

        databaseReference2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot subActivitySnapshot : dataSnapshot.getChildren()){
                    SubActivity subActivity = subActivitySnapshot.getValue(SubActivity.class);
                    subActivityList.add(subActivity);
                }
                SubActivitiesAdapter subActivityAdapter = new SubActivitiesAdapter(getContext(),subActivityList);
                subActivitiesList.setAdapter(subActivityAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getContext(),"Something wrong happened", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

}