package com.kbs.kbsintranett;

import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

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

    private List<TaskItem> tasks;
    private OnTaskClickListener listener;
    private String currentUserEmail;

    public interface OnTaskClickListener {
        void onTaskClick(TaskItem task);
        void onStatusChanged(TaskItem task, boolean isDone);
    }

    public TaskAdapter(List<TaskItem> tasks, OnTaskClickListener listener) {
        this.tasks = tasks;
        this.listener = listener;
        this.currentUserEmail = UserManager.getInstance().getUserEmail();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TaskItem task = tasks.get(position);
        holder.title.setText(task.getTitle());

        SimpleDateFormat sdf = new SimpleDateFormat("dd. MMM", Locale.getDefault());
        holder.date.setText("Frist: " + sdf.format(new Date(task.getDueDate())));

        // Vis hvem som tildelte oppgaven hvis det ikke er meg selv (Nytt krav)
        if (task.getCreatedByEmail() != null && !task.getCreatedByEmail().equalsIgnoreCase(currentUserEmail)) {
            holder.creator.setText("Tildelt av: " + task.getCreatedByName());
            holder.creator.setVisibility(View.VISIBLE);
        } else {
            holder.creator.setVisibility(View.GONE);
        }

        boolean myStatus = task.getParticipantStatus(currentUserEmail);
        holder.checkBox.setChecked(myStatus);

        long now = System.currentTimeMillis();
        boolean isOverdue = task.getDueDate() < now && !task.isFullyCompleted() && !myStatus;

        if (isOverdue) {
            holder.cardView.setCardBackgroundColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.kbs_soft_light_pink_beige));
            holder.date.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.kbs_logo_accent_red));
            holder.date.setText("FORFALT: " + sdf.format(new Date(task.getDueDate())));
        } else {
            holder.cardView.setCardBackgroundColor(Color.WHITE);
            holder.date.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.kbs_muted_blue_gray));
        }

        if (myStatus || task.isFullyCompleted()) {
            holder.title.setPaintFlags(holder.title.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            holder.title.setTextColor(Color.GRAY);
            holder.cardView.setCardBackgroundColor(Color.parseColor("#F5F5F5"));
        } else if (!isOverdue) {
            holder.title.setPaintFlags(holder.title.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
            holder.title.setTextColor(Color.BLACK);
        }

        int total = task.getAssigneeStatus().size();
        int done = 0;
        for (Boolean b : task.getAssigneeStatus().values()) if (b) done++;
        holder.progress.setText("Fremdrift: " + done + "/" + total);

        holder.checkBox.setOnClickListener(v -> listener.onStatusChanged(task, holder.checkBox.isChecked()));
        holder.itemView.setOnClickListener(v -> listener.onTaskClick(task));
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title, date, creator, progress;
        CheckBox checkBox;
        CardView cardView;
        ViewHolder(View v) {
            super(v);
            title = v.findViewById(R.id.task_title);
            date = v.findViewById(R.id.task_date);
            creator = v.findViewById(R.id.task_creator);
            progress = v.findViewById(R.id.task_progress);
            checkBox = v.findViewById(R.id.task_checkbox);
            cardView = (CardView) v;
        }
    }
}