package kr.co.vividnext.sodalive.mypage.alarm

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemAlarmBinding
import kr.co.vividnext.sodalive.mypage.alarm.db.Alarm
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

class AlarmAdapter(
    private val updateAlarm: (Alarm) -> Unit,
    private val deleteAlarm: (Alarm) -> Unit,
    private val onClick: (Int) -> Unit
) : ListAdapter<Alarm, AlarmAdapter.AlarmViewHolder>(AlarmDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AlarmViewHolder {
        return AlarmViewHolder(
            ItemAlarmBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: AlarmViewHolder, position: Int) {
        val alarm = getItem(position)
        holder.bind(alarm)
    }

    inner class AlarmViewHolder(
        private val binding: ItemAlarmBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(alarm: Alarm) {
            binding.tvTitle.text = alarm.title
            binding.tvAmpm.text = SimpleDateFormat("a", Locale.getDefault())
                .format(alarm.time)
            binding.tvTime.text = SimpleDateFormat("hh:mm", Locale.getDefault())
                .format(alarm.time)
            binding.tvDays.text = if (alarm.days.isNotEmpty()) {
                alarm.getDaysText()
            } else {
                val currentCalendar = Calendar.getInstance()
                val alarmCalendar = Calendar.getInstance()
                alarmCalendar.timeInMillis = alarm.time

                val pattern = if (
                    currentCalendar.get(Calendar.YEAR) == alarmCalendar.get(Calendar.YEAR)
                ) {
                    "M월 d일 (E)"
                } else {
                    "yyyy년 M월 d일 (E)"
                }

                SimpleDateFormat(pattern, Locale.getDefault())
                    .format(alarm.time)
            }

            binding.tvContentTitle.text = alarm.contentTitle
            binding.tvCreatorNickname.text = alarm.contentCreatorNickname

            binding.ivEnable.setImageResource(
                if (alarm.isEnabled) {
                    R.drawable.btn_toggle_on_big
                } else {
                    R.drawable.btn_toggle_off_big
                }
            )

            binding.ivEnable.setOnClickListener {
                alarm.isEnabled = !alarm.isEnabled
                updateAlarm(alarm)
            }

            binding.root.setOnClickListener { onClick(alarm.id) }
            binding.root.setOnLongClickListener {
                deleteAlarm(alarm)
                true
            }
        }
    }

    class AlarmDiffCallback : DiffUtil.ItemCallback<Alarm>() {
        override fun areItemsTheSame(oldItem: Alarm, newItem: Alarm): Boolean {
            return oldItem.id == newItem.id
        }

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