// FILSTI: app\src\main\java\com\kbs\kbsintranett\HandbookAdapter.java
package com.kbs.kbsintranett;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class HandbookAdapter extends RecyclerView.Adapter<HandbookAdapter.ViewHolder> {

    private List<HandbookItem> fullList;
    private List<HandbookItem> filteredList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(HandbookItem item);
    }

    public HandbookAdapter(List<HandbookItem> items, OnItemClickListener listener) {
        this.fullList = items;
        this.filteredList = new ArrayList<>(items);
        this.listener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HandbookItem item = filteredList.get(position);
        holder.title.setText(item.getTitle());

        // Håndter ingress: Kutt etter 80 tegn hvis den er veldig lang
        String desc = item.getDescription();
        if (desc == null) desc = "";
        if (desc.length() > 100) {
            desc = desc.substring(0, 100) + "...";
        }
        holder.desc.setText(desc);

        // Mapp ikon-type til ressurs (Utvidet liste fra PHP v3.0)
        int iconRes = R.drawable.ic_handbook_general; // Fallback
        String type = item.getIconType() != null ? item.getIconType() : "";

        switch (type) {
            case "car":         iconRes = R.drawable.ic_handbook_car; break;
            case "health":      iconRes = R.drawable.ic_handbook_health; break;
            case "people":      iconRes = R.drawable.ic_handbook_people; break;
            case "warning":     iconRes = R.drawable.ic_handbook_warning; break;
            case "doc":         iconRes = R.drawable.ic_handbook_doc; break;
            case "card":        iconRes = R.drawable.ic_handbook_doc; break; // Bruker doc inntil videre
            case "computer":    iconRes = R.drawable.ic_handbook_general; break;
            case "calendar":    iconRes = R.drawable.ic_handbook_general; break;
            case "money":       iconRes = R.drawable.ic_handbook_doc; break;
            case "helmet":      iconRes = R.drawable.ic_handbook_warning; break;
            case "trash":       iconRes = R.drawable.ic_handbook_general; break;
            case "book":        iconRes = R.drawable.ic_book; break; // Gjenbruk eksisterende ic_book
            case "chat":        iconRes = R.drawable.ic_handbook_people; break;
            default:            iconRes = R.drawable.ic_handbook_general; break;
        }
        holder.icon.setImageResource(iconRes);

        holder.itemView.setOnClickListener(v -> listener.onItemClick(item));
    }

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

    public void filter(String query) {
        filteredList.clear();
        if (query.isEmpty()) {
            filteredList.addAll(fullList);
        } else {
            String q = query.toLowerCase();
            for (HandbookItem item : fullList) {
                if (item.getTitle().toLowerCase().contains(q) ||
                        (item.getDescription() != null && item.getDescription().toLowerCase().contains(q))) {
                    filteredList.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title, desc;
        ImageView icon;

        public ViewHolder(View view) {
            super(view);
            title = view.findViewById(R.id.title);
            desc = view.findViewById(R.id.desc);
            icon = view.findViewById(R.id.icon);
        }
    }
}