package com.nurulwafa.nufakuproject;

import android.content.Intent;
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.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.ArrayList;
import java.util.List;

public class BeritaActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private BeritaAdapter adapter;
    private List<Berita> beritaList = new ArrayList<>();

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

        // Tampilkan toast jika dibuka dari notifikasi
//        if (getIntent().getBooleanExtra("from_notif", false)) {
//            String berita = getIntent().getStringExtra("berita");
//            Toast.makeText(this, "Berita baru: " + berita, Toast.LENGTH_LONG).show();
//        }

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

        adapter = new BeritaAdapter(this, beritaList, berita -> {
            // Intent ke Detail saat berita diklik
            Intent intent = new Intent(BeritaActivity.this, DetailBeritaActivity.class);
            intent.putExtra("judul", berita.getJudul());
            intent.putExtra("tanggal", berita.getTanggal());
            intent.putExtra("deskripsi", berita.getDeskripsi());
            intent.putExtra("gambar", berita.getGambar());

            // Tambahkan log dan toast DI SINI
//            Log.d("BERITA_GAMBAR_URL", berita.getGambar());
//            Toast.makeText(this, "Gambar: " + berita.getGambar(), Toast.LENGTH_LONG).show();

            startActivity(intent);
        });

        recyclerView.setAdapter(adapter);
        getBerita();
    }

    private void getBerita() {
        String baseUrl = ApiHelper.getBaseUrl();
        String requestUrl = baseUrl + "getBerita";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, requestUrl, null,
                response -> {
                    try {
                        if (response.has("data")) {
                            JSONArray jsonArray = response.getJSONArray("data");
                            if (jsonArray.length() == 0) {
                                Toast.makeText(this, "Data berita tidak ditemukan", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            beritaList.clear();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject obj = jsonArray.getJSONObject(i);
                                Berita berita = new Berita(
                                        obj.getString("judul"),
                                        obj.getString("gambar"),
                                        obj.getString("tanggal"),
                                        obj.getString("deskripsi")
                                );
                                beritaList.add(berita);
                            }
                            adapter.notifyDataSetChanged();
                        } else {
                            Toast.makeText(this, "Data berita 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()
        );

        Volley.newRequestQueue(this).add(request);
    }
}
