package com.example.punyaria;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
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 DataWellnessActivity extends AppCompatActivity {
    private DatabaseReference wellnessDatabaseRef;
    private ListView listView;
    private CustomAdapterWellness adapter;
    private List<String> wellnessHistory;
    private SharedPreferences sharedPreferences;
    private String selectedUsername;

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

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

        // Mendapatkan username yang dipilih dari SharedPreferences
        selectedUsername = sharedPreferences.getString("selectedUsername", "");

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

        // Inisialisasi referensi database wellness
        wellnessDatabaseRef = FirebaseDatabase.getInstance().getReference().child("wellness");

        // Inisialisasi ListView
        listView = findViewById(R.id.listViewWellness);

        // Inisialisasi daftar riwayat wellness
        wellnessHistory = new ArrayList<>();

        // Inisialisasi adapter kustom untuk ListView
        adapter = new CustomAdapterWellness(this, R.layout.list_item_wellness, wellnessHistory);

        // Set adapter ke ListView
        listView.setAdapter(adapter);

        // Ambil dan tampilkan riwayat wellness sesuai dengan username yang dipilih dari Firebase
        wellnessDatabaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                wellnessHistory.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    WellnessData wellnessData = snapshot.getValue(WellnessData.class);
                    if (wellnessData != null && wellnessData.getUsername().equals(selectedUsername)) {
                        String history = "Perasaan " + wellnessData.getPerasaan() + "\n" + wellnessData.getKeluhan() + "\n" + wellnessData.getTanggal();
                        wellnessHistory.add(0, history); // Tambahkan ke indeks 0
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                // Handle database error
            }
        });
    }
}
