package com.example.punyaria;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
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;

public class DataUserActivity extends AppCompatActivity {
    private DatabaseReference databaseReference;
    private EditText editTextUsername, editTextNama, editTextEmail, editTextPhoneNumber, editTextTanggalLahir, editTextJenisKelamin, editTextAddress;
    private SharedPreferences sharedPreferences;
    private ImageView profileImageView; // Tambahkan ImageView untuk menampilkan gambar profil

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

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

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

        // Inisialisasi EditText
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextNama = findViewById(R.id.editTextNama);
        editTextEmail = findViewById(R.id.editTextEmail);
        editTextPhoneNumber = findViewById(R.id.editTextPhoneNumber);
        editTextTanggalLahir = findViewById(R.id.editTextTanggalLahir);
        editTextJenisKelamin = findViewById(R.id.editTextJenisKelamin);
        editTextAddress = findViewById(R.id.editTextAddress);
        profileImageView = findViewById(R.id.image_profile); // Inisialisasi ImageView

        // Menerima data username dari SharedPreferences
        String username = sharedPreferences.getString("selectedUsername", "");

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

        // Mendapatkan data dari database
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                // Mendapatkan data pengguna
                User user = dataSnapshot.getValue(User.class);
                if (user != null) {
                    // Menampilkan data pengguna di EditText
                    editTextUsername.setText(user.getUsername());
                    editTextNama.setText(user.getNama());
                    editTextEmail.setText(user.getEmail());
                    editTextPhoneNumber.setText(user.getTelepon());
                    editTextTanggalLahir.setText(user.getTanggalLahir());
                    editTextJenisKelamin.setText(user.getJenisKelamin());
                    editTextAddress.setText(user.getAlamat());

                    // Memuat gambar profil jika URL gambar profil tidak kosong
                    String imageUrl = user.getProfileImageUrl();
                    if (imageUrl != null && !imageUrl.isEmpty()) {
                        Glide.with(getApplicationContext())
                                .load(imageUrl)
                                .transform(new CircleCrop())
                                .placeholder(R.drawable.ic_profil)
                                .error(R.drawable.ic_profil)
                                .into(profileImageView);
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                // Menangani kesalahan
                Toast.makeText(getApplicationContext(), "Gagal memuat data: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

