package exh.util

import android.content.Context
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.Locale

fun Manga.mangaType(context: Context): String {
    return context.getString(
        when (mangaType()) {
            MangaType.TYPE_WEBTOON -> R.string.webtoon
            MangaType.TYPE_MANHWA -> R.string.manhwa
            MangaType.TYPE_MANHUA -> R.string.manhua
            MangaType.TYPE_COMIC -> R.string.comic
            else -> R.string.manga
        }
    ).toLowerCase(Locale.getDefault())
}

/**
 * The type of comic the manga is (ie. manga, manhwa, manhua)
 */
fun Manga.mangaType(): MangaType {
    val sourceName = Injekt.get<SourceManager>().getOrStub(source).name
    val currentTags = getGenres() ?: emptyList()
    return if (currentTags.any { tag -> isMangaTag(tag) }) {
        MangaType.TYPE_MANGA
    } else if (currentTags.any { tag -> isWebtoonTag(tag) } || isWebtoonSource(sourceName)) {
        MangaType.TYPE_WEBTOON
    } else if (currentTags.any { tag -> isComicTag(tag) } || isComicSource(sourceName)) {
        MangaType.TYPE_COMIC
    } else if (currentTags.any { tag -> isManhuaTag(tag) } || isManhuaSource(sourceName)) {
        MangaType.TYPE_MANHUA
    } else if (currentTags.any { tag -> isManhwaTag(tag) } || isManhwaSource(sourceName)) {
        MangaType.TYPE_MANHWA
    } else {
        MangaType.TYPE_MANGA
    }
}

/**
 * The type the reader should use. Different from manga type as certain manga has different
 * read types
 */
fun Manga.defaultReaderType(): Int? {
    // val sourceName = Injekt.get<SourceManager>().getOrStub(source).name
    val type = mangaType()
    return if (type == MangaType.TYPE_MANHWA || type == MangaType.TYPE_WEBTOON) {
        ReaderActivity.WEBTOON
    } else null
}

private fun isMangaTag(tag: String): Boolean {
    return tag.contains("manga", true) ||
        tag.contains("манга", true)
}

private fun isManhuaTag(tag: String): Boolean {
    return tag.contains("manhua", true) ||
        tag.contains("маньхуа", true)
}

private fun isManhwaTag(tag: String): Boolean {
    return tag.contains("manhwa", true) ||
        tag.contains("манхва", true)
}

private fun isComicTag(tag: String): Boolean {
    return tag.contains("comic", true) ||
        tag.contains("комикс", true)
}

private fun isWebtoonTag(tag: String): Boolean {
    return tag.contains("long strip", true) ||
        tag.contains("webtoon", true)
}

/*private fun isMangaSource(sourceName: String): Boolean {
    return
}*/

private fun isManhwaSource(sourceName: String): Boolean {
    return sourceName.contains("getmanhwa", true) ||
        sourceName.contains("hiperdex", true) ||
        sourceName.contains("manhwa18", true) ||
        sourceName.contains("manhwascan", true) ||
        sourceName.contains("manwahentai.me", true) ||
        sourceName.contains("manwha club", true) ||
        sourceName.contains("manytoon", true) ||
        sourceName.contains("manwha", true) ||
        sourceName.contains("toonily", true) ||
        sourceName.contains("readmanhwa", true)
}

private fun isWebtoonSource(sourceName: String): Boolean {
    return sourceName.contains("mangatoon", true) ||
        sourceName.contains("manmanga", true) ||
        // sourceName.contains("tapas", true) ||
        sourceName.contains("toomics", true) ||
        sourceName.contains("webcomics", true) ||
        sourceName.contains("webtoons", true) ||
        sourceName.contains("webtoon", true)
}

private fun isComicSource(sourceName: String): Boolean {
    return sourceName.contains("ciayo comics", true) ||
        sourceName.contains("comicextra", true) ||
        sourceName.contains("comicpunch", true) ||
        sourceName.contains("cyanide", true) ||
        sourceName.contains("dilbert", true) ||
        sourceName.contains("existential comics", true) ||
        sourceName.contains("hiveworks comics", true) ||
        sourceName.contains("milftoon", true) ||
        sourceName.contains("myhentaicomics", true) ||
        sourceName.contains("myhentaigallery", true) ||
        sourceName.contains("gunnerkrigg", true) ||
        sourceName.contains("oglaf", true) ||
        sourceName.contains("patch friday", true) ||
        sourceName.contains("porncomix", true) ||
        sourceName.contains("questionable content", true) ||
        sourceName.contains("read comics online", true) ||
        sourceName.contains("readcomicsonline", true) ||
        sourceName.contains("swords comic", true) ||
        sourceName.contains("teabeer comics", true) ||
        sourceName.contains("xkcd", true)
}

private fun isManhuaSource(sourceName: String): Boolean {
    return sourceName.contains("1st kiss manhua", true) ||
        sourceName.contains("hero manhua", true) ||
        sourceName.contains("manhuabox", true) ||
        sourceName.contains("manhuaus", true) ||
        sourceName.contains("manhuas world", true) ||
        sourceName.contains("manhuas.net", true) ||
        sourceName.contains("readmanhua", true) ||
        sourceName.contains("wuxiaworld", true) ||
        sourceName.contains("manhua", true)
}

enum class MangaType {
    TYPE_MANGA,
    TYPE_MANHWA,
    TYPE_MANHUA,
    TYPE_COMIC,
    TYPE_WEBTOON
}
