package com.example.pap_findu.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.pap_findu.R;
import com.google.firebase.firestore.DocumentSnapshot;

import com.google.firebase.firestore.FirebaseFirestore;
import android.app.AlertDialog;
import android.widget.ImageView;
import android.widget.Toast;

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

public class ZonesAdapter extends RecyclerView.Adapter<ZonesAdapter.ZoneViewHolder> {

    private List<DocumentSnapshot> zonesList = new ArrayList<>();
    private boolean isChild = false;

    public void setChildMode(boolean isChild) {
        this.isChild = isChild;
        notifyDataSetChanged();
    }

    public void setZonesList(List<DocumentSnapshot> zonesList) {
        this.zonesList = zonesList;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ZoneViewHolder holder, int position) {
        DocumentSnapshot document = zonesList.get(position);
        String name = document.getString("name");
        String address = document.getString("address");
        Double radius = document.getDouble("radius");

        if (name != null) holder.tvZoneName.setText(name);
        if (address != null) holder.tvZoneAddress.setText(address);
        if (radius != null) {
            holder.tvZoneRadius.setText(radius + "m");
        } else {
            holder.tvZoneRadius.setText("200m");
        }
        
        // Let's set an icon dynamically or statically for now
        holder.tvZoneIcon.setText("\uD83D\uDCCD"); // Pin icon

        if (isChild) {
            holder.ivDeleteZone.setVisibility(View.GONE);
        } else {
            holder.ivDeleteZone.setVisibility(View.VISIBLE);
            holder.ivDeleteZone.setOnClickListener(v -> {
                new AlertDialog.Builder(v.getContext())
                        .setTitle("Remover Zona")
                        .setMessage("Tem a certeza que deseja eliminar esta zona segura?")
                        .setPositiveButton("Eliminar", (dialog, which) -> {
                            String zoneId = document.getId();
                            FirebaseFirestore.getInstance().collection("SafeZones").document(zoneId)
                                    .delete()
                                    .addOnSuccessListener(aVoid -> {
                                        Toast.makeText(v.getContext(), "Zona removida com sucesso.", Toast.LENGTH_SHORT).show();
                                    })
                                    .addOnFailureListener(e -> {
                                        Toast.makeText(v.getContext(), "Erro ao remover zona.", Toast.LENGTH_SHORT).show();
                                    });
                        })
                        .setNegativeButton("Cancelar", null)
                        .show();
            });
        }
    }

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

    static class ZoneViewHolder extends RecyclerView.ViewHolder {
        TextView tvZoneName, tvZoneAddress, tvZoneRadius, tvZoneIcon;
        ImageView ivDeleteZone;

        public ZoneViewHolder(@NonNull View itemView) {
            super(itemView);
            tvZoneName = itemView.findViewById(R.id.tvZoneName);
            tvZoneAddress = itemView.findViewById(R.id.tvZoneAddress);
            tvZoneRadius = itemView.findViewById(R.id.tvZoneRadius);
            tvZoneIcon = itemView.findViewById(R.id.tvZoneIcon);
            ivDeleteZone = itemView.findViewById(R.id.ivDeleteZone);
        }
    }
}
