package com.nurulwafa.nufakuproject;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

    public class PonpesActivity extends AppCompatActivity {
        private TextView tvSejarah;

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

            tvSejarah = findViewById(R.id.tvSejarah);

            getSejarah(); // langsung ambil data sejarah
        }

        private void getSejarah() {
            String baseUrl = ApiHelper.getBaseUrl();
            String requestUrl = baseUrl + "getSejarah"; // Sesuaikan dengan controller kamu

            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, requestUrl, null,
                    response -> {
                        try {
                            if (response.getBoolean("status")) {
                                JSONArray sejarahArray = response.getJSONArray("data");
                                StringBuilder sejarahGabungan = new StringBuilder();

                                for (int i = 0; i < sejarahArray.length(); i++) {
                                    JSONObject sejarahItem = sejarahArray.getJSONObject(i);
                                    String isi = sejarahItem.getString("sejarah");
                                    sejarahGabungan.append(isi).append("\n\n");
                                }
                                tvSejarah.setText(sejarahGabungan.toString());
                            } else {
                                Toast.makeText(this, "Data sejarah tidak ditemukan", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(this, "Gagal parsing data", Toast.LENGTH_SHORT).show();
                        }
                    },
                    error -> Toast.makeText(this, "Gagal ambil data: " + error.getMessage(), Toast.LENGTH_SHORT).show()
            );

            RequestQueue queue = Volley.newRequestQueue(this);
            queue.add(request);
        }
    }

