package com.example.todojava;

import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.example.todojava.data.Todo;
import com.example.todojava.databinding.ItemTodoBinding;

public class TodoAdapter extends ListAdapter<Todo, TodoAdapter.TodoViewHolder> {

    public interface OnTodoClickListener {
        void onTodoClick(Todo todo);
    }

    public interface OnTodoCheckedListener {
        void onTodoChecked(Todo todo);
    }

    private OnTodoClickListener clickListener;
    private OnTodoCheckedListener checkedListener;

    public TodoAdapter() {
        super(DIFF_CALLBACK);
    }

    public void setOnTodoClickListener(OnTodoClickListener listener) {
        this.clickListener = listener;
    }

    public void setOnTodoCheckedListener(OnTodoCheckedListener listener) {
        this.checkedListener = listener;
    }

    private static final DiffUtil.ItemCallback<Todo> DIFF_CALLBACK =
            new DiffUtil.ItemCallback<Todo>() {
                @Override
                public boolean areItemsTheSame(@NonNull Todo oldItem, @NonNull Todo newItem) {
                    return oldItem.getId() == newItem.getId();
                }

                @Override
                public boolean areContentsTheSame(@NonNull Todo oldItem, @NonNull Todo newItem) {
                    return oldItem.getTitle().equals(newItem.getTitle())
                            && oldItem.isCompleted() == newItem.isCompleted()
                            && oldItem.getCategory().equals(newItem.getCategory())
                            && oldItem.getPriority().equals(newItem.getPriority());
                }
            };

    @NonNull
    @Override
    public TodoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemTodoBinding binding = ItemTodoBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        return new TodoViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull TodoViewHolder holder, int position) {
        Todo todo = getItem(position);
        holder.bind(todo);
    }

    class TodoViewHolder extends RecyclerView.ViewHolder {
        private final ItemTodoBinding binding;

        TodoViewHolder(ItemTodoBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(Todo todo) {
            binding.textTitle.setText(todo.getTitle());
            binding.chipCategory.setText(todo.getCategory());

            // Handle completed state
            binding.checkBox.setOnCheckedChangeListener(null);
            binding.checkBox.setChecked(todo.isCompleted());

            if (todo.isCompleted()) {
                binding.textTitle.setPaintFlags(
                        binding.textTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                binding.textTitle.setTextColor(
                        ContextCompat.getColor(itemView.getContext(), R.color.completed_text));
            } else {
                binding.textTitle.setPaintFlags(
                        binding.textTitle.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                binding.textTitle.setTextColor(
                        ContextCompat.getColor(itemView.getContext(), R.color.on_surface));
            }

            // Priority dot color
            int priorityColor;
            switch (todo.getPriority()) {
                case "High":
                    priorityColor = ContextCompat.getColor(itemView.getContext(), R.color.priority_high);
                    break;
                case "Medium":
                    priorityColor = ContextCompat.getColor(itemView.getContext(), R.color.priority_medium);
                    break;
                default:
                    priorityColor = ContextCompat.getColor(itemView.getContext(), R.color.priority_low);
                    break;
            }
            GradientDrawable dot = (GradientDrawable) binding.priorityDot.getBackground().mutate();
            dot.setColor(priorityColor);

            // Click listener for editing
            itemView.setOnClickListener(v -> {
                if (clickListener != null) {
                    clickListener.onTodoClick(todo);
                }
            });

            // Checkbox listener
            binding.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (checkedListener != null) {
                    checkedListener.onTodoChecked(todo);
                }
            });
        }
    }
}
