package com.example.elva.ui.calendar

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.elva.R
import com.example.elva.databinding.ItemCalendarDayBinding
import java.time.LocalDate

class CalendarAdapter(
    private val onDateClick: (LocalDate) -> Unit
) : ListAdapter<CalendarDay, CalendarAdapter.DayViewHolder>(DayDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DayViewHolder {
        val binding = ItemCalendarDayBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return DayViewHolder(binding)
    }

    override fun onBindViewHolder(holder: DayViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class DayViewHolder(
        private val binding: ItemCalendarDayBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(day: CalendarDay) {
            when (day.type) {
                CalendarDayType.EMPTY -> {
                    binding.tvDay.text = ""
                    binding.dayContainer.background = null
                    binding.dayContainer.setOnClickListener(null)
                }
                else -> {
                    binding.tvDay.text = day.date?.dayOfMonth?.toString() ?: ""

                    // Устанавливаем фон в зависимости от типа дня
                    val backgroundRes = when (day.type) {
                        CalendarDayType.PERIOD -> R.drawable.bg_calendar_period
                        CalendarDayType.PREDICTED -> R.drawable.bg_calendar_predicted
                        CalendarDayType.FERTILE -> R.drawable.bg_calendar_fertile
                        CalendarDayType.SELECTED -> R.drawable.bg_calendar_selected
                        else -> null
                    }

                    if (backgroundRes != null) {
                        binding.dayContainer.background = ContextCompat.getDrawable(
                            binding.root.context,
                            backgroundRes
                        )
                        binding.tvDay.setTextColor(
                            ContextCompat.getColor(
                                binding.root.context,
                                if (day.type == CalendarDayType.PERIOD)
                                    android.R.color.white
                                else
                                    R.color.text_primary
                            )
                        )
                    } else {
                        binding.dayContainer.background = null
                        binding.tvDay.setTextColor(
                            ContextCompat.getColor(
                                binding.root.context,
                                R.color.text_primary
                            )
                        )
                    }

                    // Затемняем дни не из текущего месяца
                    if (day.date?.month != LocalDate.now().month) {
                        binding.tvDay.alpha = 0.3f
                    } else {
                        binding.tvDay.alpha = 1f
                    }

                    binding.dayContainer.setOnClickListener {
                        day.date?.let { onDateClick(it) }
                    }
                }
            }
        }
    }

    private class DayDiffCallback : DiffUtil.ItemCallback<CalendarDay>() {
        override fun areItemsTheSame(oldItem: CalendarDay, newItem: CalendarDay): Boolean {
            return oldItem.date == newItem.date
        }

        override fun areContentsTheSame(oldItem: CalendarDay, newItem: CalendarDay): Boolean {
            return oldItem == newItem
        }
    }
}

