package com.remax.visualnovel.widget.imageviewer.core

import com.remax.visualnovel.widget.imageviewer.core.DataProvider
import com.remax.visualnovel.widget.imageviewer.core.ImageLoader
import com.remax.visualnovel.widget.imageviewer.core.OverlayCustomizer
import com.remax.visualnovel.widget.imageviewer.core.Transformer
import com.remax.visualnovel.widget.imageviewer.core.VHCustomizer
import com.remax.visualnovel.widget.imageviewer.core.ViewerCallback

object Components {
    private var initialize = false
    val working get() = initialize
    private var imageLoader: ImageLoader? = null
    private var dataProvider: DataProvider? = null
    private var transformer: Transformer? = null
    private var vhCustomizer: VHCustomizer? = null
    private var overlayCustomizer: OverlayCustomizer? = null
    private var viewerCallback: ViewerCallback? = null

    fun initialize(imageLoader: ImageLoader, dataProvider: DataProvider, transformer: Transformer) {
        if (initialize) throw IllegalStateException()
        Components.imageLoader = imageLoader
        Components.dataProvider = dataProvider
        Components.transformer = transformer
        initialize = true
    }

    fun setVHCustomizer(vhCustomizer: VHCustomizer?) {
        Components.vhCustomizer = vhCustomizer
    }

    fun setViewerCallback(viewerCallback: ViewerCallback?) {
        Components.viewerCallback = viewerCallback
    }

    fun setOverlayCustomizer(overlayCustomizer: OverlayCustomizer?) {
        Components.overlayCustomizer = overlayCustomizer
    }

    fun requireImageLoader() = imageLoader ?: object : ImageLoader {}
    fun requireDataProvider() = dataProvider ?: object : DataProvider {}
    fun requireTransformer() = transformer ?: object : Transformer {}
    fun requireVHCustomizer() = vhCustomizer ?: object : VHCustomizer {}
    fun requireViewerCallback() = viewerCallback ?: object : ViewerCallback {}
    fun requireOverlayCustomizer() = overlayCustomizer ?: object : OverlayCustomizer {}

    fun release() {
        initialize = false
        imageLoader = null
        dataProvider = null
        transformer = null
        vhCustomizer = null
        viewerCallback = null
        overlayCustomizer = null
    }
}