package com.kbs.kbsintranett;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

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

    public static final int TYPE_EVENT = 0;
    public static final int TYPE_YEAR_HEADER = 1;

    private List<Object> items;
    private final OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(CalendarEvent event);
    }

    public CalendarAdapter(List<Object> items, OnItemClickListener listener) {
        this.items = items;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return (items.get(position) instanceof String) ? TYPE_YEAR_HEADER : TYPE_EVENT;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_YEAR_HEADER) {
            return new YearViewHolder(inflater.inflate(R.layout.item_calendar_year_header, parent, false));
        } else {
            return new EventViewHolder(inflater.inflate(R.layout.item_calendar, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object item = items.get(position);

        if (holder instanceof YearViewHolder) {
            ((YearViewHolder) holder).yearText.setText((String) item);
        }
        else if (holder instanceof EventViewHolder) {
            CalendarEvent event = (CalendarEvent) item;
            EventViewHolder vh = (EventViewHolder) holder;

            vh.day.setText(event.getDay());
            vh.month.setText(event.getMonth());
            vh.time.setText(event.getTime());
            vh.title.setText(event.getTitle());

            // --- ÅRSTALL LOGIKK: BAKGRUNNSFARGE ---
            // Vi henter årstall fra datoen (yyyy-MM-dd)
            String year = "2025";
            if (event.getRawDate() != null && event.getRawDate().length() >= 4) {
                year = event.getRawDate().substring(0, 4);
            }

            // Alternerende bakgrunn basert på år (partall vs oddetall)
            int yearInt = Integer.parseInt(year);
            if (yearInt % 2 == 0) {
                vh.itemView.setBackgroundColor(Color.parseColor("#F5F7FA")); // KBS Very Light Blue
            } else {
                vh.itemView.setBackgroundColor(Color.WHITE);
            }

            // Privat-markering og farge på datoboks
            boolean isPrivate = event.getDescription() != null && event.getDescription().contains("#deltakere:");
            try {
                int color = Color.parseColor(isPrivate ? "#673AB7" : event.getCalendarColor());
                vh.dateBox.setBackgroundTintList(ColorStateList.valueOf(color));
            } catch (Exception e) {
                vh.dateBox.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#0069B3")));
            }

            vh.itemView.setOnClickListener(v -> listener.onItemClick(event));
        }
    }

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

    static class EventViewHolder extends RecyclerView.ViewHolder {
        TextView day, month, title, time;
        LinearLayout dateBox;
        EventViewHolder(View view) {
            super(view);
            day = view.findViewById(R.id.cal_day);
            month = view.findViewById(R.id.cal_month);
            title = view.findViewById(R.id.cal_title);
            time = view.findViewById(R.id.cal_time);
            dateBox = view.findViewById(R.id.date_box_background);
        }
    }

    static class YearViewHolder extends RecyclerView.ViewHolder {
        TextView yearText;
        YearViewHolder(View view) {
            super(view);
            yearText = view.findViewById(R.id.txt_calendar_year);
        }
    }
}