package com.project.ilsaenterprise.fragment;

import android.content.Intent;
import android.os.Bundle;

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

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.bottomsheet.BottomSheetDialog;
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.project.ilsaenterprise.GroupChatActivity;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.adapter.AttendanceAdapter;
import com.project.ilsaenterprise.adapter.GroupListAdapter;
import com.project.ilsaenterprise.adapter.MonitoringAdapter;
import com.project.ilsaenterprise.models.ListGroup;
import com.project.ilsaenterprise.monitoring.DisplayMonitoring;
import com.project.ilsaenterprise.monitoring.ListData;

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

public class GroupFragment extends Fragment {
    private static final String TAG = "GroupFragment";
    private View groupFragmentView;
    private RecyclerView listGroupView;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<ListGroup> listGroup = new ArrayList<>();//List_of_group
    private DatabaseReference GroupRef = FirebaseDatabase.getInstance().getReference().child("Chats");
    private DatabaseReference chatRef = FirebaseDatabase.getInstance().getReference();
    private BottomSheetDialog bottomSheetDialog;
    private Toast toast;

    private GroupListAdapter groupListAdapter;
    private GroupListAdapter.onItemClickListener clickListener;


    public GroupFragment(){
        //Public Constructor
    }

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

        return groupFragmentView;
    }

    @Override
    public void onStart() {
        super.onStart();

        RetrieveDisplayGroup();

    }

    private void setClickListener(){
        clickListener = new GroupListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String idGroup = listGroup.get(position).getGroupName();
//                ref.child(idMonitoring).child("listLihat").child(cUser).setValue(true);//Add viewer report

                Intent groupChatIntent = new Intent(GroupFragment.this.getContext(), GroupChatActivity.class);
                groupChatIntent.putExtra("groupName", idGroup);
                startActivity(groupChatIntent);
            }
        };
    }

    private void RetrieveDisplayGroup() {
        GroupRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                listGroup.clear();
                for (DataSnapshot ds : snapshot.getChildren()) {
                    ListGroup model = new ListGroup();
                    model.setGroupName(ds.getKey());

                    GroupRef.child(Objects.requireNonNull(ds.getKey())).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            model.setCountMessages(String.valueOf(snapshot.getChildrenCount()));
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {

                        }
                    });
                    listGroup.add(model);
                }
                InitializeFields();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }


    private void InitializeFields() {
        setClickListener();
        listGroupView = groupFragmentView.findViewById(R.id.listViewGroup);
        groupListAdapter = new GroupListAdapter(GroupFragment.this.getContext(), listGroup, clickListener);
        listGroupView.setLayoutManager(new LinearLayoutManager(GroupFragment.this.getContext(), LinearLayoutManager.VERTICAL, false));
        listGroupView.setAdapter(groupListAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        RetrieveDisplayGroup();
    }


}