package de.sebse.fuplanner2.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import de.sebse.fuplanner2.R

sealed class CustomHolder(base: View) : RecyclerView.ViewHolder(base) {
    abstract fun clear()
}

class CaptionHolder(base: View) : CustomHolder(base) {
    companion object {
        private const val LAYOUT = R.layout.list_all_caption
        fun invoke(parent: ViewGroup) = CaptionHolder(
            LayoutInflater.from(parent.context)
                .inflate(LAYOUT, parent, false)
        )
    }
    val string: TextView = base.findViewById(R.id.string)

    override fun clear() {
        string.text = ""
    }
}

class NotificationHolder(base: View) : CustomHolder(base) {
    companion object {
        private const val LAYOUT = R.layout.list_notification_item
        fun invoke(parent: ViewGroup) = NotificationHolder(
            LayoutInflater.from(parent.context)
                .inflate(LAYOUT, parent, false)
        )
    }
    val string: TextView = base.findViewById(R.id.string)
    val timestamp: TextView = base.findViewById(R.id.timestamp)
    val read: ImageView = base.findViewById(R.id.read)

    override fun clear() {
        string.text = ""
        timestamp.text = ""
        read.visibility = View.INVISIBLE
    }
}

class QuickLinksHolder(base: View) : CustomHolder(base) {
    companion object {
        private const val LAYOUT = R.layout.list_courses_quicklinks
        fun invoke(parent: ViewGroup) = QuickLinksHolder(
            LayoutInflater.from(parent.context)
                .inflate(LAYOUT, parent, false)
        )
    }
    val btnDescription: Button = base.findViewById(R.id.btn_description)
    val btnResources: Button = base.findViewById(R.id.btn_resources)
    val btnGradebook: Button = base.findViewById(R.id.btn_gradebook)
    val btnAnnouncements: Button = base.findViewById(R.id.btn_announcements)
    val btnAssignments: Button = base.findViewById(R.id.btn_assignments)
    val btnEvents: Button = base.findViewById(R.id.btn_events)

    override fun clear() {
        btnDescription.setOnClickListener(null)
        btnResources.setOnClickListener(null)
        btnGradebook.setOnClickListener(null)
        btnAnnouncements.setOnClickListener(null)
        btnAssignments.setOnClickListener(null)
        btnEvents.setOnClickListener(null)
    }
}

class MailHolder(base: View) : CustomHolder(base) {
    companion object {
        private const val LAYOUT = R.layout.list_all_mails
        fun invoke(parent: ViewGroup) = MailHolder(
            LayoutInflater.from(parent.context)
                .inflate(LAYOUT, parent, false)
        )
    }
    val title: TextView = base.findViewById(R.id.title)
    val subLeft: TextView = base.findViewById(R.id.sub_left)
    val icon: ImageView = base.findViewById(R.id.icon)

    override fun clear() {
        title.text = ""
        subLeft.text = ""
        icon.setImageDrawable(null)
    }
}

class ListItemHolder(base: View) : CustomHolder(base) {
    companion object {
        private const val LAYOUT = R.layout.list_all_items
        fun invoke(parent: ViewGroup) = ListItemHolder(
            LayoutInflater.from(parent.context)
                .inflate(LAYOUT, parent, false)
        )
    }
    val title: TextView = base.findViewById(R.id.title)
    val subLeft: TextView = base.findViewById(R.id.sub_left)
    val subRight: TextView = base.findViewById(R.id.sub_right)
    val topRight: TextView = base.findViewById(R.id.top_right)

    override fun clear() {
        title.text = ""
        subLeft.text = ""
        subRight.text = ""
        topRight.text = ""
    }
}

object ViewHolderGenerator {
    enum class HolderType {
        HEADER, BUTTONS, MAIL, ITEM;
        companion object {
            val values: List<HolderType> = values().toList()
        }
    }

    fun getHolderByType(parent: ViewGroup, viewType: Int): CustomHolder {
        return when (HolderType.values[viewType]) {
            HolderType.HEADER -> CaptionHolder.invoke(parent)
            HolderType.ITEM -> ListItemHolder.invoke(parent)
            HolderType.BUTTONS -> QuickLinksHolder.invoke(parent)
            HolderType.MAIL -> MailHolder.invoke(parent)
        }
    }
}