package com.example.punyaria;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

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.util.ArrayList;
import java.util.List;

public class UserActivity extends AppCompatActivity {
    private ListView listViewUsernames;
    private ArrayAdapter<String> adapter;
    private DatabaseReference databaseReference;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        getSupportActionBar().hide();

        // Initialize SharedPreferences
        sharedPreferences = getSharedPreferences("userData", MODE_PRIVATE);

        ImageView headerImageView = findViewById(R.id.headerImageView);
        headerImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        listViewUsernames = findViewById(R.id.listViewUser);
        listViewUsernames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Mendapatkan username dari item yang diklik
                String selectedUsername = (String) parent.getItemAtPosition(position);

                // Menyimpan username yang dipilih dalam SharedPreferences
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("selectedUsername", selectedUsername);
                editor.apply();

                // Membuat Intent untuk membuka DataUserActivity
                Intent intent = new Intent(UserActivity.this, DataUserActivity.class);
                // Memulai activity baru
                startActivity(intent);
            }
        });

        // Inisialisasi ArrayList untuk menyimpan daftar username
        final List<String> usernames = new ArrayList<>();

        // Inisialisasi adapter untuk ListView
        adapter = new ArrayAdapter<>(this, R.layout.list_item_user, R.id.textViewUsername, usernames);
        listViewUsernames.setAdapter(adapter);

        // Referensi database
        databaseReference = FirebaseDatabase.getInstance().getReference().child("users");

        // Mendapatkan data dari database
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                // Membersihkan list sebelum menambahkan data baru
                usernames.clear();

                // Menambahkan semua username ke list kecuali "uptd_ppajember21"
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    String username = snapshot.child("username").getValue(String.class);
                    if (!"uptd_ppajember21".equals(username)) {
                        usernames.add(username);
                    }
                }

                // Memberitahu adapter bahwa data telah berubah
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                // Menangani kesalahan
            }
        });
    }
}
