package app.furmeet.events.system.notification

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import app.furmeet.events.bff.R
import app.furmeet.events.system.helper.*
import app.furmeet.events.system.schedule.abstractDataTypes.convention.Announcement
import app.furmeet.events.system.schedule.adapters.RecyclerViewAdapter
import app.furmeet.events.system.schedule.viewHolders.RecyclerViewHolder
import app.furmeet.events.bff.databinding.ActivityPastNotificationsItemBinding
import java.util.*

//import kotlinx.android.synthetic.main.activity_past_notifications_item.view.*

class PastNotificationsAdapter(private val announcements: List<Announcement>) : RecyclerViewAdapter() {

    override fun getItemCount(): Int = announcements.size
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder =
            RecyclerViewHolder(parent.context.layoutInflater.inflate(R.layout.activity_past_notifications_item))

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        val announcement = announcements[position]
        val view = holder.itemView
        val binding = ActivityPastNotificationsItemBinding.bind(view)
        val ctx = view.context
        @SuppressLint("DefaultLocale")
        binding.dt.text = announcement.created.timeInMillis.formatAsMonthName()
            .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } + " " + announcement.created.timeInMillis.formatAsDayOfMonth()
        @SuppressLint("DefaultLocale")
        binding.dow.text = announcement.created.timeInMillis.formatAsDayOfWeek()
            .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
        binding.hour.text = announcement.created.timeInMillis.formatAsHoursAndMinutes()
        binding.title.text = announcement.title[ctx.runtimeLanguage] ?: ""
        binding.description.text = announcement.body[ctx.runtimeLanguage] ?: ""
        val link = announcement.link?.get(ctx.runtimeLanguage) ?: ""
        binding.link.visibility = if (link.isNotBlank()) View.VISIBLE else View.INVISIBLE
        binding.announcement.setOnClickListener {
            if (link.isNotBlank()) {
                it.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))
            }
        }
    }

}
