package com.nurulwafa.nufakuproject;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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;

import java.util.ArrayList;

public class PrestasiSantriActivity extends AppCompatActivity {

    private RecyclerView rvPrestasi;
    private PrestasiAdapter adapter;
    private ArrayList<Prestasi> prestasiList;

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

        rvPrestasi = findViewById(R.id.rvPrestasi);
        rvPrestasi.setLayoutManager(new LinearLayoutManager(this));

        prestasiList = new ArrayList<>();
        adapter = new PrestasiAdapter(prestasiList);
        rvPrestasi.setAdapter(adapter);

        String loginKey = getIntent().getStringExtra("login_key");
        String loginValue = getIntent().getStringExtra("login_value");

        if (loginKey != null && loginValue != null) {
            getDataSantri(loginKey, loginValue);
        } else {
            Toast.makeText(this, "Data login tidak ditemukan", Toast.LENGTH_SHORT).show();
        }
    }

    private void getDataSantri(String key, String value) {
        String baseUrl = ApiHelper.getBaseUrl();
        String requestUrl = baseUrl + "getDataSantri?" + key + "=" + value;

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, requestUrl, null,
                response -> {
                    try {
                        if (response.getBoolean("status")) {
                            prestasiList.clear();

                            // Ambil objek "data"
                            JSONObject dataObject = response.getJSONObject("data");

                            // Ambil array "prestasi_santri" dari dalam "data"
                            JSONArray prestasiArray = dataObject.getJSONArray("prestasi_santri");

                            for (int i = 0; i < prestasiArray.length(); i++) {
                                JSONObject prestasi = prestasiArray.getJSONObject(i);

                                String nama = prestasi.getString("nama_santri");
                                String namaPrestasi = prestasi.optString("prestasi", "-");
                                String agenda = prestasi.optString("agenda", "-");
                                String perlombaan = prestasi.optString("perlombaan", "-");
                                String tanggal = prestasi.optString("tanggal", "-");
                                String buktiPrestasi = prestasi.optString("bukti_prestasi", "-");

                                Prestasi p = new Prestasi(nama, namaPrestasi, agenda, perlombaan, tanggal, buktiPrestasi);
                                prestasiList.add(p);
                            }

                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(this, "Data 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);
    }

}
