package com.nurulwafa.nufakuproject;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
public class PrestasiAdapter extends RecyclerView.Adapter<PrestasiAdapter.PrestasiViewHolder> {
    private final List<Prestasi> prestasiList;

    public PrestasiAdapter(List<Prestasi> prestasiList) {
        this.prestasiList = prestasiList;
    }

    @NonNull
    @Override
    public PrestasiViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_prestasi, parent, false);
        return new PrestasiViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull PrestasiViewHolder holder, int position) {
        Prestasi prestasi = prestasiList.get(position);
        holder.tvNamaSantri.setText(prestasi.getNamaSantri());
        holder.tvPrestasi.setText("Prestasi: " + prestasi.getPrestasi());
        holder.tvAgenda.setText("Agenda: " + prestasi.getAgenda());
        holder.tvPerlombaan.setText("Perlombaan: " + prestasi.getPerlombaan());
        holder.tvTanggal.setText("Tanggal: " + prestasi.getTanggal());

        String buktiUrl = prestasi.getBuktiPrestasi();
        if (buktiUrl != null && !buktiUrl.isEmpty()) {
            holder.tvBuktiPrestasi.setText("Lihat Bukti Prestasi");
            holder.tvBuktiPrestasi.setTextColor(Color.BLUE);
            holder.tvBuktiPrestasi.setPaintFlags(holder.tvBuktiPrestasi.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

            holder.tvBuktiPrestasi.setOnClickListener(v -> {
                try {
                    String fileId = "";

                    if (buktiUrl.contains("/d/")) {
                        String[] parts = buktiUrl.split("/d/");
                        if (parts.length > 1) {
                            fileId = parts[1].split("/")[0];
                        }
                    } else if (buktiUrl.contains("id=")) {
                        Uri uri = Uri.parse(buktiUrl);
                        fileId = uri.getQueryParameter("id");
                    }

                    if (fileId != null && !fileId.isEmpty()) {
                        // Pakai format view langsung, bukan uc?export
                        String directLink = "https://drive.google.com/file/d/" + fileId + "/view";

                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(directLink));
                        intent.setPackage("com.android.chrome");

                        try {
                            v.getContext().startActivity(intent);
                        } catch (ActivityNotFoundException e) {
                            intent.setPackage(null); // fallback ke browser default
                            v.getContext().startActivity(intent);
                        }

                    } else {
                        Toast.makeText(v.getContext(), "Link file tidak valid", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(v.getContext(), "Gagal membuka link", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            });


        } else {
            holder.tvBuktiPrestasi.setText("Tidak Ada Bukti");
            holder.tvBuktiPrestasi.setTextColor(Color.GRAY);
            holder.tvBuktiPrestasi.setPaintFlags(0);
            holder.tvBuktiPrestasi.setOnClickListener(null); // tidak ada aksi
        }

    }


    @Override
    public int getItemCount() {
        return prestasiList.size();
    }

    static class PrestasiViewHolder extends RecyclerView.ViewHolder {
        TextView tvNamaSantri, tvPrestasi, tvAgenda, tvPerlombaan, tvTanggal, tvBuktiPrestasi;

        public PrestasiViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNamaSantri = itemView.findViewById(R.id.tvNamaSantri);
            tvPrestasi = itemView.findViewById(R.id.tvPrestasi);
            tvAgenda = itemView.findViewById(R.id.tvAgenda);
            tvPerlombaan = itemView.findViewById(R.id.tvPerlombaan);
            tvTanggal = itemView.findViewById(R.id.tvTanggal);
            tvBuktiPrestasi = itemView.findViewById(R.id.tvBuktiPrestasi);
        }
    }
}
