package com.example.punyaria;

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 RiwayatWellnessActivity extends AppCompatActivity {

    private DatabaseReference wellnessRef;
    private ListView listView;
    private CustomAdapterWellness adapter;
    private List<String> wellnessHistory;
    private String loggedInUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();

        setContentView(R.layout.activity_riwayat_wellness);

        // 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
        wellnessRef = 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);

        // Mendapatkan username pengguna yang sedang login
        loggedInUsername = getUsername();

        // Ambil dan tampilkan riwayat wellness sesuai dengan username pengguna yang sedang login dari Firebase
        wellnessRef.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(loggedInUsername)) {
                        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
            }
        });
    }

    private String getUsername() {
        // Mendapatkan username dari Shared Preferences
        return getSharedPreferences("loginPrefs", MODE_PRIVATE)
                .getString("username", null);
    }
}
