package app.furmeet.events.brasilfurfest.notification

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import com.adlerosn.brasilfurfest.R
import app.furmeet.events.brasilfurfest.helper.*
import app.furmeet.events.brasilfurfest.schedule.abstractDataTypes.convention.Announcement
import app.furmeet.events.brasilfurfest.schedule.adapters.RecyclerViewAdapter
import app.furmeet.events.brasilfurfest.schedule.viewHolders.RecyclerViewHolder
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 ctx = view.context
        @SuppressLint("DefaultLocale")
        view.dt.text = announcement.created.timeInMillis.formatAsMonthName().capitalize() + " " + announcement.created.timeInMillis.formatAsDayOfMonth()
        @SuppressLint("DefaultLocale")
        view.dow.text = announcement.created.timeInMillis.formatAsDayOfWeek().capitalize()
        view.hour.text = announcement.created.timeInMillis.formatAsHoursAndMinutes()
        view.title.text = announcement.title[ctx.runtimeLanguage] ?: ""
        view.description.text = announcement.body[ctx.runtimeLanguage] ?: ""
        val link = announcement.link?.get(ctx.runtimeLanguage) ?: ""
        view.link.visibility = if (link.isNotBlank()) View.VISIBLE else View.INVISIBLE
        view.announcement.setOnClickListener {
            if (link.isNotBlank()) {
                it.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))
            }
        }
    }

}
