package app.insti.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

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

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

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.FirebaseStorage;
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.ActivitiesAdapter;
import app.insti.api.model.BroadActivity;

public class BroadActivityFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    private StorageReference storageReference;
    private DatabaseReference databaseReference;
    private List<BroadActivity> broadActivityList;

    public BroadActivityFragment() {
        // Required empty public constructor
    }

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

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

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

        broadActivityList = new ArrayList<>();

        databaseReference = FirebaseDatabase.getInstance().getReference("activities");

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot activitySnapshot : dataSnapshot.getChildren()){
                    BroadActivity broadActivity = activitySnapshot.getValue(BroadActivity.class);
                    broadActivityList.add(broadActivity);
                }
                ActivitiesAdapter broadActivityAdapter = new ActivitiesAdapter(getContext(),broadActivityList);
                broadActivitiesList.setAdapter(broadActivityAdapter);
            }

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