package de.sebse.fuplanner2.ui.details

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import de.sebse.fuplanner2.R
import de.sebse.fuplanner2.database.Lecturer
import de.sebse.fuplanner2.ui.*
import de.sebse.fuplanner2.utils.cast
import java.util.*

class DetailsAdapter(private val onQuickLink: (ButtonTypes) -> Unit, private val onMailTo: (Lecturer) -> Unit) : RecyclerView.Adapter<CustomHolder>() {

    enum class HeaderTypes {
        BUTTONS, LECTURER, ANNOUNCEMENTS, ASSIGNMENTS, EVENTS;
    }

    enum class ButtonTypes {
        DESCRIPTION, RESOURCES, GRADEBOOK, ANNOUNCEMENTS, ASSIGNMENTS, EVENTS;
    }

    private val positionalData: ArrayList<Any> = arrayListOf()

    var lecturers: List<Lecturer> = listOf()
    set(value) {
        field = value
        updatePositionalData()
    }

    private fun updatePositionalData() {
        positionalData.clear()
        positionalData.add(HeaderTypes.BUTTONS)
        positionalData.add(ViewHolderGenerator.HolderType.BUTTONS)
        positionalData.add(HeaderTypes.LECTURER)
        positionalData.addAll(lecturers.sortedBy { it.lastName }.sortedBy { !it.isResponsible })
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomHolder {
        return ViewHolderGenerator.getHolderByType(parent, viewType)
    }

    override fun onBindViewHolder(holder: CustomHolder, position: Int) {
        // val viewType = getItemViewType(position)
        val res = holder.itemView.resources
        when (holder) {
            is CaptionHolder -> cast<HeaderTypes>(positionalData[position])?.let { type ->
                holder.string.text = when (type) {
                    HeaderTypes.BUTTONS -> res.getString(R.string.quick_links)
                    HeaderTypes.LECTURER -> res.getString(R.string.lecturers)
                    HeaderTypes.ANNOUNCEMENTS -> res.getString(R.string.announcements)
                    HeaderTypes.ASSIGNMENTS -> res.getString(R.string.assignments)
                    HeaderTypes.EVENTS -> res.getString(R.string.events)
                }
            }
            is QuickLinksHolder -> {
                holder.btnAnnouncements.setOnClickListener { this.onQuickLink(ButtonTypes.ANNOUNCEMENTS) }
                holder.btnAssignments.setOnClickListener { this.onQuickLink(ButtonTypes.ASSIGNMENTS) }
                holder.btnDescription.setOnClickListener { this.onQuickLink(ButtonTypes.DESCRIPTION) }
                holder.btnEvents.setOnClickListener { this.onQuickLink(ButtonTypes.EVENTS) }
                holder.btnGradebook.setOnClickListener { this.onQuickLink(ButtonTypes.GRADEBOOK) }
                holder.btnResources.setOnClickListener { this.onQuickLink(ButtonTypes.RESOURCES) }
            }
            is MailHolder -> cast<Lecturer>(positionalData[position])?.let { type ->
                holder.title.text = res.getString(R.string.full_name, type.firstName, type.lastName)
                holder.subLeft.text = type.email
                holder.itemView.setOnClickListener { this.onMailTo(type) }
            }
            is ListItemHolder -> when (positionalData[position]) {
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return when (positionalData[position]) {
            is HeaderTypes -> ViewHolderGenerator.HolderType.HEADER.ordinal
            ViewHolderGenerator.HolderType.BUTTONS -> ViewHolderGenerator.HolderType.BUTTONS.ordinal
            is Lecturer -> ViewHolderGenerator.HolderType.MAIL.ordinal
            else -> ViewHolderGenerator.HolderType.ITEM.ordinal
        }
    }

    // Return the size of your dataset (invoked by the layout manager)
    override fun getItemCount() = positionalData.size
}
