package exh.ui.smartsearch

import android.os.Bundle
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.browse.source.SourceController
import exh.smartsearch.SmartSearchEngine
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch

class SmartSearchPresenter(private val source: CatalogueSource?, private val config: SourceController.SmartSearchConfig?) :
    BasePresenter<SmartSearchController>() {

    val scope = CoroutineScope(Job() + Dispatchers.Main)

    val smartSearchChannel = Channel<SearchResults>()

    private val smartSearchEngine = SmartSearchEngine()

    override fun onCreate(savedState: Bundle?) {
        super.onCreate(savedState)

        if (source != null && config != null) {
            scope.launch(Dispatchers.Default) {
                val result = try {
                    val resultManga = smartSearchEngine.smartSearch(source, config.origTitle)
                    if (resultManga != null) {
                        val localManga = smartSearchEngine.networkToLocalManga(resultManga, source.id)
                        SearchResults.Found(localManga)
                    } else {
                        SearchResults.NotFound
                    }
                } catch (e: Exception) {
                    if (e is CancellationException) {
                        throw e
                    } else {
                        SearchResults.Error
                    }
                }

                smartSearchChannel.send(result)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()

        scope.cancel()
    }

    sealed class SearchResults {
        data class Found(val manga: Manga) : SearchResults()
        object NotFound : SearchResults()
        object Error : SearchResults()
    }
}
