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 com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import java.util.List;

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

    private List<WpPost> posts;
    private OnItemClickListener listener; // NYTT

    // Interface for klikk
    public interface OnItemClickListener {
        void onItemClick(WpPost post);
    }

    // Oppdatert konstruktør
    public NewsAdapter(List<WpPost> posts, OnItemClickListener listener) {
        this.posts = posts;
        this.listener = listener;
    }

    // Overload for bakoverkompatibilitet (hvis du ikke sender listener)
    public NewsAdapter(List<WpPost> posts) {
        this.posts = posts;
        this.listener = null;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WpPost post = posts.get(position);

        holder.title.setText(post.getTitleStr());
        holder.excerpt.setText(post.getExcerptStr());
        holder.date.setText(post.date);

        String cat = post.getCategoryName();
        if (!cat.isEmpty()) {
            holder.category.setText(cat);
            holder.category.setVisibility(View.VISIBLE);
        } else {
            holder.category.setVisibility(View.GONE);
        }

        String imageUrl = post.getFeaturedImageUrl();
        if (imageUrl != null && !imageUrl.isEmpty()) {
            holder.image.setVisibility(View.VISIBLE);
            Glide.with(holder.itemView.getContext())
                    .load(imageUrl)
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .centerCrop()
                    .into(holder.image);
        } else {
            holder.image.setVisibility(View.GONE);
        }

        // NYTT: Håndter klikk
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(post);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title, excerpt, date, category;
        ImageView image;

        public ViewHolder(View view) {
            super(view);
            title = view.findViewById(R.id.news_title);
            excerpt = view.findViewById(R.id.news_excerpt);
            date = view.findViewById(R.id.news_date);
            category = view.findViewById(R.id.news_category);
            image = view.findViewById(R.id.news_image);
        }
    }

    // NYTT: Metode for å oppdatere listen etter filtrering
    public void updateList(List<WpPost> newPosts) {
        this.posts = newPosts;
        notifyDataSetChanged();
    }
}