package app.furmeet.events.system.schedule.adapters

import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.toColor
import androidx.recyclerview.widget.DiffUtil
import app.furmeet.events.bff.R
import app.furmeet.events.system.helper.*
import app.furmeet.events.system.helper.observables.Observable
import app.furmeet.events.system.helper.observables.Observer
import app.furmeet.events.system.schedule.EventActivity
import app.furmeet.events.system.schedule.abstractDataTypes.dataholder.PeekableEvent
import app.furmeet.events.system.schedule.managers.ScheduleManager
import app.furmeet.events.system.schedule.viewHolders.RecyclerViewHolder
import app.furmeet.events.bff.databinding.ActivityFavoritesItemBinding

class FavoritesAdapter(
        private val activity: Activity,
        private val scheduleManager: ScheduleManager
) :
        RecyclerViewAdapter(),
        Observer<Any?> {
    var data = scheduleManager.attendeeFavorites.toList()

    override fun update(observable: Observable<Any?>, args: Any?) = onDataChanged()

    private fun onDataChanged() {
        val oldData = data
        val newData = scheduleManager.attendeeFavorites.toList()
        activity.runOnUiThread {
            data = newData
            val diff = DiffUtil.calculateDiff(ListDiffCallback(oldData, newData))
            diff.dispatchUpdatesTo(this)
        }
    }

    override fun getItemCount(): Int = data.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder =
            RecyclerViewHolder(parent.context.layoutInflater.inflate(R.layout.activity_favorites_item))

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        val favorite = data[position]
        val view = holder.itemView
        var binding = ActivityFavoritesItemBinding.bind(view)
        scheduleManager.convention.colorScheme?.accentColor?.let { binding.starImage.imageTintList = ColorStateList.valueOf(it) }
        binding.startDay.text = favorite.timeRange.start.timeInMillis.formatAsDayOfWeek().toLowerCase().capitalize()
        binding.startText.text = favorite.timeRange.start.timeInMillis.formatAsHoursAndMinutes()
        binding.finishText.text = favorite.timeRange.finish.timeInMillis.formatAsHoursAndMinutes()
        binding.eventTitle.text = favorite.getString()
        binding.eventSubtitle.text = favorite.getSecondaryString()
        val event = favorite.findInConvention(scheduleManager.convention)
        if (event?.language == null) {
            binding.flag.visibility = View.GONE
        } else {
            binding.flag.visibility = View.VISIBLE
            binding.flag.setImageResource(event.language.drawableId)
        }
        view.setOnClickListener {
            activity.startActivity(
                    Intent(activity, EventActivity::class.java).apply {
                        putExtra("favorite", favorite)
                        putExtra("offerStar", false)
                    }
            )
        }
        binding.starButton.setOnClickListener {
            scheduleManager.attendeeFavorites.remove(favorite)
            onDataChanged()
        }
        bindPeekAndPop(
                activity,
                view as ViewGroup,
                view,
                true,
                { binding.starButton.callOnClick() },
                { view.callOnClick() },
                scheduleManager.convention.events.firstOrNull {
                    it.conbookId == favorite.uuid
                }?.let {
                    PeekableEvent(it)
                } ?: PeekableEvent(favorite)
        )
    }

}
