package eu.kanade.tachiyomi.animeextension.en.hahomoe

import eu.kanade.tachiyomi.animesource.model.AnimeFilter
import eu.kanade.tachiyomi.animesource.model.AnimeFilter.TriState
import eu.kanade.tachiyomi.animesource.model.AnimeFilterList
import java.util.Locale

object HahoMoeFilters {
    open class TriStateFilterList(name: String, values: List<TriFilterVal>) : AnimeFilter.Group<TriState>(name, values)
    class TriFilterVal(name: String) : TriState(name)

    private inline fun <reified R> AnimeFilterList.getFirst(): R = first { it is R } as R

    private inline fun <reified R> AnimeFilterList.parseTriFilter(): List<List<String>> {
        return (getFirst<R>() as TriStateFilterList).state
            .filterNot { it.isIgnored() }
            .map { filter -> filter.state to "\"${filter.name.lowercase(Locale.US)}\"" }
            .groupBy { it.first } // group by state
            .let { dict ->
                val included = dict.get(TriState.STATE_INCLUDE)?.map { it.second }.orEmpty()
                val excluded = dict.get(TriState.STATE_EXCLUDE)?.map { it.second }.orEmpty()
                listOf(included, excluded)
            }
    }

    class TagFilter : TriStateFilterList("Tags", HahoMoeFiltersData.TAGS.map(::TriFilterVal))

    class SortFilter : AnimeFilter.Sort(
        "Sort",
        HahoMoeFiltersData.ORDERS.map { it.first }.toTypedArray(),
        Selection(0, true),
    )

    val FILTER_LIST get() = AnimeFilterList(
        TagFilter(),
        SortFilter(),
    )

    data class FilterSearchParams(
        val includedTags: List<String> = emptyList(),
        val excludedListedTags: List<String> = emptyList(),
        val orderBy: String = "az-",
        val ordering: String = "a", // ascending
    )

    internal fun getSearchParameters(filters: AnimeFilterList): FilterSearchParams {
        if (filters.isEmpty()) return FilterSearchParams()

        val (included, excluded) = filters.parseTriFilter<TagFilter>()
        val (orderBy, order) = filters.getFirst<SortFilter>().state?.let {
            val order = if (it.ascending) "a" else "d"
            val orderBy = HahoMoeFiltersData.ORDERS[it.index].second
            Pair(orderBy, order)
        } ?: Pair("az-", "d")

        return FilterSearchParams(included, excluded, orderBy, order)
    }

    private object HahoMoeFiltersData {
        val TAGS = listOf(
            "3D CG animation",
            "absurdist humour",
            "action",
            "action game",
            "adapted into Japanese movie",
            "adapted into JDrama",
            "adapted into other media",
            "adults are useless",
            "adventure",
            "Africa",
            "age difference romance",
            "ahegao",
            "air force",
            "Akihabara",
            "alcohol",
            "alien",
            "alien invasion",
            "all-boys school",
            "all-girls school",
            "alternating animation style",
            "alternative past",
            "alternative present",
            "Americas",
            "amnesia",
            "anal",
            "anal fingering",
            "anal pissing",
            "android",
            "angel",
            "angst",
            "animal abuse",
            "animal protagonist",
            "Animerama",
            "anthropomorphism",
            "aphrodisiac",
            "archery",
            "Asia",
            "ass-kicking girls",
            "assjob",
            "association football",
            "attempted rape",
            "aunt-nephew incest",
            "autofellatio",
            "autumn",
            "bad cooking",
            "Bakumatsu - Meiji Period",
            "baseball",
            "basketball",
            "BDSM",
            "be careful what you wish for",
            "bestiality",
            "betrayal",
            "bishoujo",
            "bishounen",
            "bitter-sweet",
            "black humour",
            "blackmail",
            "board games",
            "body and host",
            "body exchange",
            "body takeover",
            "bondage",
            "borderline porn",
            "boxing",
            "boy meets girl",
            "brainwashing",
            "branching story",
            "breast expansion",
            "breast fondling",
            "breasts",
            "brother-sister incest",
            "Buddhism",
            "bukkake",
            "bullying",
            "call my name",
            "calling your attacks",
            "car crash",
            "cast",
            "castaway",
            "catholic school",
            "censored uncensored version",
            "cervix penetration",
            "CG collection",
            "CGI",
            "cheating",
            "chikan",
            "child abuse",
            "China",
            "Christianity",
            "classical music",
            "cockring",
            "collateral damage",
            "colour coded",
            "combat",
            "comedy",
            "coming of age",
            "competition",
            "conspiracy",
            "contemporary fantasy",
            "content indicators",
            "contraband",
            "cooking",
            "cops",
            "corrupt church",
            "corrupt nobility",
            "cosplaying",
            "countryside",
            "cram school",
            "creampie",
            "crime",
            "cross-dressing",
            "cum play",
            "cum swapping",
            "cunnilingus",
            "curse",
            "cyberpunk",
            "cybersex",
            "cyborg",
            "daily life",
            "damsel in distress",
            "dancing",
            "dark",
            "dark atmosphere",
            "dark elf",
            "dark fantasy",
            "dark-skinned girl",
            "death",
            "defeat means friendship",
            "deflowering",
            "deity",
            "delinquent",
            "dementia",
            "demon",
            "demon hunt",
            "demonic power",
            "DESCRIPTION NEEDS IMPROVEMENT",
            "desert",
            "despair",
            "detective",
            "dildos - vibrators",
            "disaster",
            "discontinued",
            "disturbing",
            "divorce",
            "doggy style",
            "dominatrix",
            "double fellatio",
            "double penetration",
            "double-sided dildo",
            "doujin",
            "dragon",
            "drastic change of life",
            "dreams",
            "dreams and reality",
            "drugs",
            "dungeon",
            "dutch wife",
            "dynamic",
            "dysfunctional family",
            "dystopia",
            "Earth",
            "earthquake",
            "eating of humans",
            "ecchi",
            "Egypt",
            "elements",
            "elf",
            "emotions awaken superpowers",
            "ending",
            "enema",
            "Engrish",
            "enjo-kousai",
            "enjoyable rape",
            "entertainment industry",
            "episodic",
            "erotic asphyxiation",
            "erotic game",
            "erotic torture",
            "Europe",
            "European stylised",
            "everybody dies",
            "everybody has sex",
            "evil military",
            "excessive censoring",
            "exhibitionism",
            "exorcism",
            "experimental animation",
            "extrasensory perception",
            "eye penetration",
            "faceless background characters",
            "facesitting",
            "facial distortion",
            "fairy",
            "fake relationship",
            "family life",
            "family without mother",
            "fantasy",
            "father-daughter incest",
            "felching",
            "fellatio",
            "female protagonist",
            "female rapes female",
            "female student",
            "female teacher",
            "femdom",
            "feminism",
            "fetishes",
            "feudal warfare",
            "FFM threesome",
            "fictional location",
            "fighting",
            "fingering",
            "fire",
            "first love",
            "fishing",
            "fisting",
            "foot fetish",
            "footage reuse",
            "footjob",
            "forbidden love",
            "foreskin sex",
            "foursome",
            "France",
            "French kiss",
            "friendship",
            "full HD version available",
            "funny expressions",
            "futa x female",
            "futa x futa",
            "futa x male",
            "futanari",
            "future",
            "Gainax bounce",
            "game",
            "gang bang",
            "gang rape",
            "gangs",
            "gender bender",
            "genetic modification",
            "Germany",
            "ghost",
            "ghost hunting",
            "giant insects",
            "gigantic breasts",
            "girl rapes girl",
            "girly tears",
            "glory hole",
            "go",
            "god is a girl",
            "gokkun",
            "golden shower",
            "gore",
            "grandiose displays of wealth",
            "Greek mythology",
            "groping",
            "group love",
            "gunfights",
            "guro",
            "gymnastics",
            "half-length episodes",
            "handjob",
            "happy ending",
            "harem",
            "heaven",
            "hell",
            "henshin",
            "heroic sacrifice",
            "hidden agenda",
            "hidden vibrator",
            "high fantasy",
            "high school",
            "historical",
            "Hong Kong",
            "horny nosebleed",
            "horror",
            "hospital",
            "hostage situation",
            "housewives",
            "human cannibalism",
            "human enhancement",
            "human experimentation",
            "human sacrifice",
            "human-android love",
            "humanoid alien",
            "hyperspace mallet",
            "i got a crush on you",
            "ice skating",
            "idol",
            "immortality",
            "imperial stormtrooper marksmanship academy",
            "important haircut",
            "impregnation",
            "impregnation with larvae",
            "improbable physics",
            "in medias res",
            "incest",
            "India",
            "infidelity",
            "Injuu Hentai Series",
            "inter-dimensional schoolgirl",
            "intercrural sex",
            "internal shots",
            "isekai",
            "island",
            "Japan",
            "Japanese mythology",
            "jealousy",
            "Journey to the West",
            "just as planned",
            "juujin",
            "kamikaze",
            "kendo",
            "kidnapping",
            "killing criminals",
            "Korea",
            "lactation",
            "large breasts",
            "law and order",
            "library",
            "light-hearted",
            "lingerie",
            "live-action imagery",
            "loli",
            "long episodes",
            "love between enemies",
            "love polygon",
            "macabre",
            "mafia",
            "magic",
            "magic circles",
            "magic weapons",
            "magical girl",
            "maid",
            "main character dies",
            "maintenance tags",
            "male protagonist",
            "male rape victim",
            "mammary intercourse",
            "manga",
            "manipulation",
            "martial arts",
            "massacre",
            "master-servant relationship",
            "master-slave relation",
            "masturbation",
            "mecha",
            "mechanical tentacle",
            "medium awareness",
            "merchandising show",
            "mermaid",
            "meta tags",
            "Middle East",
            "middle school",
            "military",
            "military is useless",
            "mind fuck",
            "misunderstanding",
            "MMF threesome",
            "MMM threesome",
            "molestation",
            "money",
            "monster of the week",
            "mother-daughter incest",
            "mother-son incest",
            "movie",
            "multi-anime projects",
            "multi-segment episodes",
            "multiple couples",
            "murder",
            "murder of family members",
            "music",
            "musical band",
            "mutation",
            "mutilation",
            "mystery",
            "mythology",
            "Nagasaki",
            "narration",
            "navel fuck",
            "navy",
            "nearly almighty protagonist",
            "necrophilia",
            "nervous breakdown",
            "netorare",
            "netori",
            "new",
            "ninja",
            "nipple penetration",
            "non-linear",
            "Norse mythology",
            "nostril hook",
            "not for kids",
            "novel",
            "nudity",
            "nun",
            "nurse",
            "nurse office",
            "nyotaimori",
            "occult",
            "occupation and career",
            "ocean",
            "off-model animation",
            "office lady",
            "older female younger male",
            "omnibus format",
            "onahole",
            "One Thousand and One Nights",
            "onmyoudou",
            "open-ended",
            "oral",
            "orgasm denial",
            "orgy",
            "origin",
            "original work",
            "otaku culture",
            "other planet",
            "out-of-body experience",
            "outdoor sex",
            "oyakodon",
            "painting",
            "pantsu",
            "panty theft",
            "pantyjob",
            "paper clothes",
            "parallel world",
            "parasite",
            "parental abandonment",
            "parody",
            "parricide",
            "past",
            "pegging",
            "performance",
            "photographic backgrounds",
            "photography",
            "pillory",
            "piloted robot",
            "pirate",
            "place",
            "plot continuity",
            "plot twists",
            "plot with porn",
            "point of view",
            "police",
            "police are useless",
            "pornography",
            "post-apocalyptic",
            "poverty",
            "power corrupts",
            "power suit",
            "predominantly adult cast",
            "predominantly female cast",
            "predominantly male cast",
            "pregnant sex",
            "present",
            "prison",
            "promise",
            "prostate massage",
            "prostitution",
            "proxy battles",
            "psychoactive drugs",
            "psychological",
            "psychological manipulation",
            "psychological sexual abuse",
            "public sex",
            "pussy sandwich",
            "rape",
            "real-world location",
            "rebellion",
            "recycled animation",
            "red-light district",
            "reincarnation",
            "religion",
            "remastered version available",
            "restaurant",
            "revenge",
            "reverse harem",
            "reverse spitroast",
            "reverse trap",
            "rimming",
            "rivalry",
            "robot",
            "romance",
            "rotten world",
            "RPG",
            "rugby",
            "running gag",
            "safer sex",
            "sakura",
            "samurai",
            "scat",
            "school clubs",
            "school dormitory",
            "school for the rich elite",
            "school life",
            "science fiction",
            "scissoring",
            "season",
            "Secret Anima",
            "Secret Anima Series",
            "seiyuu",
            "self-parody",
            "setting",
            "sex",
            "sex change",
            "sex doll",
            "sex tape",
            "sex toys",
            "sex while on the phone",
            "sexual abuse",
            "sexual fantasies",
            "shibari",
            "Shinjuku",
            "shinsengumi",
            "shipboard",
            "short episodes",
            "short movie",
            "short story collection",
            "shota",
            "shoujo ai",
            "shounen ai",
            "sibling rivalry",
            "sibling yin yang",
            "sister-sister incest",
            "sixty-nine",
            "skimpy clothing",
            "slapstick",
            "slavery",
            "sleeping sex",
            "slide show animation",
            "slow when it comes to love",
            "slums",
            "small breasts",
            "soapland",
            "social class issues",
            "social commentary",
            "softball",
            "some weird shit goin` on",
            "South Korean production",
            "space",
            "space pirates",
            "space travel",
            "spacing out",
            "spanking",
            "special squads",
            "speculative fiction",
            "spellcasting",
            "spirit realm",
            "spirits",
            "spiritual powers",
            "spitroast",
            "sports",
            "spring",
            "squirting",
            "stand-alone movie",
            "stereotypes",
            "stomach bulge",
            "stomach stretch",
            "storytelling",
            "strapon",
            "strappado",
            "strappado bondage",
            "strong female lead",
            "strong male lead",
            "student government",
            "submission",
            "succubus",
            "sudden girlfriend appearance",
            "sudden naked girl appearance",
            "suicide",
            "sumata",
            "summer",
            "summoning",
            "super deformed",
            "super power",
            "superhero",
            "surreal",
            "survival",
            "suspension bondage",
            "swimming",
            "swordplay",
            "table tennis",
            "tales",
            "tank warfare",
            "teacher x student",
            "technical aspects",
            "tennis",
            "tentacle",
            "the arts",
            "the power of love",
            "themes",
            "thievery",
            "thigh sex",
            "Three Kingdoms",
            "threesome",
            "threesome with sisters",
            "thriller",
            "throat fucking",
            "time",
            "time loop",
            "time travel",
            "Tokugawa period",
            "Tokyo",
            "torture",
            "tournament",
            "track and field",
            "tragedy",
            "tragic beginning",
            "training",
            "transforming craft",
            "transforming weapons",
            "trap",
            "trapped",
            "triple penetration",
            "tropes",
            "tsunami",
            "TV censoring",
            "twincest",
            "ukiyo-e",
            "uncle-niece incest",
            "undead",
            "under one roof",
            "unexpected inheritance",
            "uniform fetish",
            "unintentional comedy",
            "United States",
            "university",
            "unrequited love",
            "unrequited shounen ai",
            "unsorted",
            "urethra penetration",
            "urination",
            "urophagia",
            "vampire",
            "Vanilla Series",
            "video game development",
            "violence",
            "violent retribution for accidental infringement",
            "virtual world",
            "visible aura",
            "visual novel",
            "volleyball",
            "voyeurism",
            "waitress",
            "wakamezake",
            "wardrobe malfunction",
            "water sex",
            "watercolour style",
            "wax play",
            "Weekly Shounen Jump",
            "whip",
            "whipping",
            "window fuck",
            "winter",
            "wooden horse",
            "working life",
            "world domination",
            "World War II",
            "wrestling",
            "yaoi",
            "Yokohama",
            "youji play",
            "yuri",
            "zero to hero",
            "zombie",
        )

        val ORDERS = arrayOf(
            Pair("Alphabetical", "az-"),
            Pair("Released", "rel-"),
            Pair("Added", "add-"),
            Pair("Bookmarked", "bkm-"),
            Pair("Rated", "rtg-"),
            Pair("Popular", "vtt-"),
            Pair("Popular Today", "vdy-"),
            Pair("Popular This Week", "vwk-"),
            Pair("Popular This Month", "vmt-"),
            Pair("Popular This Year", "vyr-"),
        )
    }
}
