package bou.amine.apps.readerforselfossv2.android.adapters

import android.app.Activity
import android.graphics.Color
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import bou.amine.apps.readerforselfossv2.android.R
import bou.amine.apps.readerforselfossv2.model.SelfossModel
import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.service.AppSettingsService
import bou.amine.apps.readerforselfossv2.utils.ItemType
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.kodein.di.DIAware

abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapter<VH>(), DIAware {
    abstract var items: ArrayList<SelfossModel.Item>
    abstract val repository: Repository
    abstract val appSettingsService: AppSettingsService
    abstract val app: Activity
    abstract val updateItems: (ArrayList<SelfossModel.Item>) -> Unit

    fun updateAllItems(items: ArrayList<SelfossModel.Item>) {
        this.items = items
        notifyDataSetChanged()
        updateItems(this.items)
    }

    private fun unmarkSnackbar(item: SelfossModel.Item, position: Int) {
        val s = Snackbar
            .make(
                app.findViewById(R.id.coordLayout),
                R.string.marked_as_read,
                Snackbar.LENGTH_LONG
            )
            .setAction(R.string.undo_string) {
                CoroutineScope(Dispatchers.IO).launch {
                    unreadItemAtIndex(item, position, false)
                }
            }

        val view = s.view
        val tv: TextView = view.findViewById(com.google.android.material.R.id.snackbar_text)
        tv.setTextColor(Color.WHITE)
        s.show()
    }

    private fun markSnackbar(item: SelfossModel.Item, position: Int) {
        val s = Snackbar
            .make(
                app.findViewById(R.id.coordLayout),
                R.string.marked_as_unread,
                Snackbar.LENGTH_LONG
            )
            .setAction(R.string.undo_string) {
                readItemAtIndex(item, position, false)
            }

        val view = s.view
        val tv: TextView = view.findViewById(com.google.android.material.R.id.snackbar_text)
        tv.setTextColor(Color.WHITE)
        s.show()
    }

    fun handleItemAtIndex(position: Int) {
        if (items[position].unread) {
            readItemAtIndex(items[position], position)
        } else {
            unreadItemAtIndex(items[position], position)
        }
    }

    private fun readItemAtIndex(item: SelfossModel.Item, position: Int, showSnackbar: Boolean = true) {
        CoroutineScope(Dispatchers.IO).launch {
            repository.markAsRead(item)
        }
        if (repository.displayedItems == ItemType.UNREAD) {
            items.remove(item)
            notifyItemRemoved(position)
            updateItems(items)
        } else {
            notifyItemChanged(position)
        }
        if (showSnackbar) {
            unmarkSnackbar(item, position)
        }
    }

    private fun unreadItemAtIndex(item: SelfossModel.Item, position: Int, showSnackbar: Boolean = true) {
        CoroutineScope(Dispatchers.IO).launch {
            repository.unmarkAsRead(item)

        }
        notifyItemChanged(position)
        if (showSnackbar) {
            markSnackbar(item, position)
        }
    }

    fun addItemAtIndex(item: SelfossModel.Item, position: Int) {
        items.add(position, item)
        notifyItemInserted(position)
        updateItems(items)

    }

    fun addItemsAtEnd(newItems: List<SelfossModel.Item>) {
        val oldSize = items.size
        items.addAll(newItems)
        notifyItemRangeInserted(oldSize, newItems.size)
        updateItems(items)

    }
}