package com.project.ilsaenterprise.monitoring;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;

import android.os.Handler;
import android.os.Looper;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.format.DateFormat;
import android.text.style.BulletSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.facebook.shimmer.ShimmerFrameLayout;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.adapter.MonitoringAdapter;
import com.project.ilsaenterprise.models.MonitoringListModel;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class ListData extends AppCompatActivity{

    private static final String TAG = "List Data";
    ArrayList<MonitoringListModel> postList;
    private RecyclerView listMonitoringView;
    private ArrayList<String> listMonitoring = new ArrayList<>();
    //private FirebaseAuth mAuth;
    private String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
    private ImageView filterBtn;
    private BottomSheetDialog bottomSheetDialog;
    private Spinner spinnerDis, spinnerKeb;
    private String[] distrik, kebunD1, kebunD2, kebunD3, kebunD4;
    private ArrayAdapter<String> arrayAdapter_farm, arrayAdapter_tLap;
    private RadioGroup listFilter;
    private RadioButton semuaRadio, hamaRadio;
    private ProgressBar progressBar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("users");
    private DatabaseReference ref = FirebaseDatabase.getInstance().getReference("monitoring");
    private MonitoringAdapter monitoringAdapter;
    private MonitoringAdapter.onItemClickListener clickListener;
    private GoogleSignInAccount googleLogin;
    private LinearLayout layPlaceholder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.Dark));
        setContentView(R.layout.activity_list_data);
        getSupportActionBar().hide();

        googleLogin = GoogleSignIn.getLastSignedInAccount(getApplicationContext());

        filterBtn = findViewById(R.id.filterBtn);
        listFilter = findViewById(R.id.listFilterGroup);
        semuaRadio = findViewById(R.id.radio0);
        hamaRadio = findViewById(R.id.radio1);
        progressBar = findViewById(R.id.progressBar);
        swipeRefreshLayout = findViewById(R.id.swipeRefresh1);
        layPlaceholder = findViewById(R.id.layPlaceholder);

        bottomSheetDialog = new BottomSheetDialog(
                ListData.this, R.style.BottomSheetDialogTheme
        );

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadPost();
                swipeRefreshLayout.setRefreshing(false);

            }
        });

        //mAuth = FirebaseAuth.getInstance();
        postList = new ArrayList<>();

        filterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View bottomSheetView = LayoutInflater.from(getApplicationContext()).inflate(
                                R.layout.layout_popup_filter,
                                (LinearLayout)findViewById(R.id.bottomSheetContainer)
                        );

                spinnerDis = bottomSheetView.findViewById(R.id.spinnerDistrikFilter);
                spinnerKeb = bottomSheetView.findViewById(R.id.spinnerKebunFilter);
                spinnerDis.setEnabled(true);
                spinnerKeb.setEnabled(false);
                final Resources res = getResources();
                kebunD1 = res.getStringArray(R.array.kebun_distrik_1);
                kebunD2 = res.getStringArray(R.array.kebun_distrik_2);
                kebunD3 = res.getStringArray(R.array.kebun_distrik_3);
                kebunD4 = res.getStringArray(R.array.kebun_distrik_4);
                distrik = res.getStringArray(R.array.distrik);
                ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(ListData.this, android.R.layout.simple_spinner_item, distrik);
                adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerDis.setAdapter(adapter1);

                spinnerDis.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        if(position == 0){
                            spinnerKeb.setEnabled(false);
                        }
                        else if(position==1)
                        {
                            spinnerKeb.setEnabled(true);
                            arrayAdapter_farm = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, kebunD1);
                        }
                        else if(position==2)
                        {
                            spinnerKeb.setEnabled(true);
                            arrayAdapter_farm = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, kebunD2);
                        }
                        else if(position==3)
                        {
                            spinnerKeb.setEnabled(true);
                            arrayAdapter_farm = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, kebunD3);
                        }
                        else if(position==4)
                        {
                            spinnerKeb.setEnabled(true);
                            arrayAdapter_farm = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, kebunD4);
                        }
                        spinnerKeb.setAdapter(arrayAdapter_farm);
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
                bottomSheetDialog.setContentView(bottomSheetView);
                bottomSheetDialog.show();
            }
        });

        loadPost();
        loadFilter();
    }

    private void loadFilter(){
        listFilter.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio0:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                    long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                    modelPost.setCountSee(jlhLihat);
                                    modelPost.setCountComment(jlhComment);
                                    long tmp = Long.parseLong(modelPost.getTimestamp());
                                    modelPost.setDateUpload(getDate(tmp));
                                    userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                                            String username = snapshot.child("namaLengkap").getValue(String.class);
                                            String role = snapshot.child("role").getValue(String.class);
                                            String userImg = snapshot.child("image").getValue(String.class);
                                            modelPost.setFullName(username);
                                            modelPost.setRole(role);
                                            modelPost.setImgLink(userImg);
                                        }

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

                                        }
                                    });
                                    postList.add(modelPost);
                                }

                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                    case R.id.radio1:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    if(modelPost.getJenisLaporan().equals("Pemeliharaan TM")){
                                        long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                        long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                        modelPost.setCountSee(jlhLihat);
                                        modelPost.setCountComment(jlhComment);
                                        long tmp = Long.parseLong(modelPost.getTimestamp());
                                        modelPost.setDateUpload(getDate(tmp));
                                        userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                String username = snapshot.child("namaLengkap").getValue(String.class);
                                                String role = snapshot.child("role").getValue(String.class);
                                                String userImg = snapshot.child("image").getValue(String.class);
                                                modelPost.setFullName(username);
                                                modelPost.setRole(role);
                                                modelPost.setImgLink(userImg);
                                            }

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

                                            }
                                        });
                                        postList.add(modelPost);
                                    }
                                }
                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                    case R.id.radio2:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    if(modelPost.getJenisLaporan().equals("Pemeliharaan TBM")){
                                        long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                        long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                        modelPost.setCountSee(jlhLihat);
                                        modelPost.setCountComment(jlhComment);
                                        long tmp = Long.parseLong(modelPost.getTimestamp());
                                        modelPost.setDateUpload(getDate(tmp));
                                        userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                String username = snapshot.child("namaLengkap").getValue(String.class);
                                                String role = snapshot.child("role").getValue(String.class);
                                                String userImg = snapshot.child("image").getValue(String.class);
                                                modelPost.setFullName(username);
                                                modelPost.setRole(role);
                                                modelPost.setImgLink(userImg);
                                            }

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

                                            }
                                        });
                                        postList.add(modelPost);
                                    }
                                }
                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                    case R.id.radio3:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    if(modelPost.getJenisLaporan().equals("Kondisi Jalan")){
                                        long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                        long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                        modelPost.setCountSee(jlhLihat);
                                        modelPost.setCountComment(jlhComment);
                                        long tmp = Long.parseLong(modelPost.getTimestamp());
                                        modelPost.setDateUpload(getDate(tmp));
                                        userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                String username = snapshot.child("namaLengkap").getValue(String.class);
                                                String role = snapshot.child("role").getValue(String.class);
                                                String userImg = snapshot.child("image").getValue(String.class);
                                                modelPost.setFullName(username);
                                                modelPost.setRole(role);
                                                modelPost.setImgLink(userImg);
                                            }

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

                                            }
                                        });
                                        postList.add(modelPost);
                                    }
                                }
                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                    case R.id.radio4:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    if(modelPost.getJenisLaporan().equals("Hama")){
                                        long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                        long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                        modelPost.setCountSee(jlhLihat);
                                        modelPost.setCountComment(jlhComment);
                                        long tmp = Long.parseLong(modelPost.getTimestamp());
                                        modelPost.setDateUpload(getDate(tmp));
                                        userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                String username = snapshot.child("namaLengkap").getValue(String.class);
                                                String role = snapshot.child("role").getValue(String.class);
                                                String userImg = snapshot.child("image").getValue(String.class);
                                                modelPost.setFullName(username);
                                                modelPost.setRole(role);
                                                modelPost.setImgLink(userImg);
                                            }

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

                                            }
                                        });
                                        postList.add(modelPost);
                                    }
                                }
                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                    case R.id.radio5:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    if(modelPost.getJenisLaporan().equals("Penyakit")){
                                        long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                        long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                        modelPost.setCountSee(jlhLihat);
                                        modelPost.setCountComment(jlhComment);
                                        long tmp = Long.parseLong(modelPost.getTimestamp());
                                        modelPost.setDateUpload(getDate(tmp));
                                        userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                String username = snapshot.child("namaLengkap").getValue(String.class);
                                                String role = snapshot.child("role").getValue(String.class);
                                                String userImg = snapshot.child("image").getValue(String.class);
                                                modelPost.setFullName(username);
                                                modelPost.setRole(role);
                                                modelPost.setImgLink(userImg);
                                            }

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

                                            }
                                        });
                                        postList.add(modelPost);
                                    }
                                }
                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                    case R.id.radio6:
                        postList.clear();
                        ref.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for(DataSnapshot ds : dataSnapshot.getChildren()){
                                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                                    if(modelPost.getJenisLaporan().equals("Pencurian")){
                                        long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                                        long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                                        modelPost.setCountSee(jlhLihat);
                                        modelPost.setCountComment(jlhComment);
                                        long tmp = Long.parseLong(modelPost.getTimestamp());
                                        modelPost.setDateUpload(getDate(tmp));
                                        userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                                String username = snapshot.child("namaLengkap").getValue(String.class);
                                                String role = snapshot.child("role").getValue(String.class);
                                                String userImg = snapshot.child("image").getValue(String.class);
                                                modelPost.setFullName(username);
                                                modelPost.setRole(role);
                                                modelPost.setImgLink(userImg);
                                            }

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

                                            }
                                        });
                                        postList.add(modelPost);
                                    }
                                }
                                Collections.reverse(postList);
                                final Handler handler = new Handler(Looper.getMainLooper());
                                handler.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        InitializeFields();
                                    }
                                }, 1000);

                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });

                        break;
                }
            }
        });
    }

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

    }

    @Override
    protected void onResume() {
        super.onResume();
//        postList.clear();
//        mShimmerViewContainer.startShimmerAnimation();
//        loadPost();
//        loadFilter();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void InitializeFields() {
        setClickListener();
        listMonitoringView = findViewById(R.id.listViewMonit);
        monitoringAdapter = new MonitoringAdapter(postList, clickListener, ListData.this);
        listMonitoringView.setLayoutManager(new LinearLayoutManager(ListData.this, LinearLayoutManager.VERTICAL, false));
        listMonitoringView.setAdapter(monitoringAdapter);
        layPlaceholder.setVisibility(View.GONE);
    }

    private void setClickListener(){
        clickListener = new MonitoringAdapter.onItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String idMonitoring = postList.get(position).getTimestamp();
                String cUser = googleLogin.getId();
                ref.child(idMonitoring).child("listLihat").child(cUser).setValue(true);//Add viewer report

                Intent groupChatIntent = new Intent(ListData.this, DisplayMonitoring.class);
                groupChatIntent.putExtra("idMonitoring", idMonitoring);
                startActivity(groupChatIntent);
            }
        };
    }

    private void loadPost() {
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                postList.clear();
                for(DataSnapshot ds : dataSnapshot.getChildren()){
                    MonitoringListModel modelPost = ds.getValue(MonitoringListModel.class);
                    long jlhLihat = dataSnapshot.child(modelPost.getTimestamp()).child("listLihat").getChildrenCount();
                    long jlhComment = dataSnapshot.child(modelPost.getTimestamp()).child("komentar").getChildrenCount();

                    modelPost.setCountSee(jlhLihat);
                    modelPost.setCountComment(jlhComment);
                    long tmp = Long.parseLong(modelPost.getTimestamp());
                    modelPost.setDateUpload(getDate(tmp));

                    userRef.child(modelPost.getUid()).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            String username = snapshot.child("namaLengkap").getValue(String.class);
                            String role = snapshot.child("role").getValue(String.class);
                            String userImg = snapshot.child("image").getValue(String.class);
                            String jabatan = snapshot.child("jabatan").getValue(String.class);
                            modelPost.setFullName(username);
                            modelPost.setRole(role);
                            modelPost.setImgLink(userImg);
                            modelPost.setJabatan(jabatan);
                        }

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

                        }
                    });
                    postList.add(modelPost);
                }

                Collections.reverse(postList);
                final Handler handler = new Handler(Looper.getMainLooper());
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InitializeFields();
                    }
                }, 1000);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ListData.this, ""+databaseError.getMessage(), Toast.LENGTH_LONG).show();
            }
        });



    }

    private String getDate(long time) {
        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
        cal.setTimeInMillis(time);
        String clock = cal.getTime().getHours() + ":" + cal.getTime().getMinutes();
        String date = "";

        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);

        if(month == 0)
            date += day + " Januari " + year;
        if(month == 1)
            date += day + " Februari " + year;
        if(month == 2)
            date += day + " Maret " + year;
        if(month == 3)
            date += day + " April " + year;
        if(month == 4)
            date += day + " Mei " + year;
        if(month == 5)
            date += day + " Juni " + year;
        if(month == 6)
            date += day + " Juli " + year;
        if(month == 7)
            date += day + " Agustus " + year;
        if(month == 8)
            date += day + " September " + year;
        if(month == 9)
            date += day + " Oktober " + year;
        if(month == 10)
            date += day + " November " + year;
        if(month == 11)
            date += day + " Desember " + year;

        return clock + " | " + date;
    }

    private String getName(String uid){
        final String[] username = {""};
        userRef.child(uid).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                username[0] = snapshot.child("namaLengkap").getValue(String.class);
            }

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

            }
        });

        return username[0];
    }

}
