package com.nurulwafa.nufakuproject;

import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
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.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;

public class KegiatanSantriActivity extends AppCompatActivity {

    private LinearLayout layoutKonten;
    RequestQueue requestQueue;

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

        layoutKonten = findViewById(R.id.layoutKonten);
        requestQueue = Volley.newRequestQueue(this);
        // Panggil method untuk ambil dan tampilkan data
        fetchKegiatanSantri();
    }

    private void fetchKegiatanSantri() {
        String url = ApiHelper.getBaseUrl() + "getKegiatan";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                response -> {
                    try {
                        boolean status = response.getBoolean("status");
                        if (status) {
                            JSONArray dataArray = response.getJSONArray("data");

                            for (int i = 0; i < dataArray.length(); i++) {
                                JSONObject kegiatan = dataArray.getJSONObject(i);
                                String namaKegiatan = kegiatan.getString("nama_kegiatan");
                                String waktu = kegiatan.getString("waktu");

                                // Tambahkan ke layoutKonten
                                addKegiatanCard(namaKegiatan, waktu);
                            }
                        } else {
                            Toast.makeText(this, "Tidak ada data kegiatan ditemukan.", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(this, "Error parsing data", Toast.LENGTH_SHORT).show();
                    }
                },
                error -> {
                    error.printStackTrace();
                    Toast.makeText(this, "Gagal mengambil data dari server", Toast.LENGTH_SHORT).show();
                }
        );
        requestQueue.add(request);
    }

    private void addKegiatanCard(String nama, String waktu) {
        LinearLayout layoutKonten = findViewById(R.id.layoutKonten);

        LinearLayout card = new LinearLayout(this);
        card.setOrientation(LinearLayout.VERTICAL);
        card.setPadding(24, 24, 24, 24);
        card.setBackgroundResource(R.drawable.custom_editcard); // opsional: drawable card background
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 30, 0, 0);
        card.setLayoutParams(params);

        TextView tvNama = new TextView(this);
        tvNama.setText(nama);
        tvNama.setTextSize(16f);
        tvNama.setTypeface(null, Typeface.BOLD);

        TextView tvWaktu = new TextView(this);
        tvWaktu.setText(waktu);
        tvWaktu.setTextSize(14f);

        card.addView(tvNama);
        card.addView(tvWaktu);

        layoutKonten.addView(card);
    }

}

