aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/repopatterns/RepoPatternGui.kt
blob: 1981440fdc25c7fbebd76ab0b8c03ec0a4d589e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package at.hannibal2.skyhanni.utils.repopatterns

import at.hannibal2.skyhanni.SkyHanniMod
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
import io.github.notenoughupdates.moulconfig.gui.GuiComponentWrapper
import io.github.notenoughupdates.moulconfig.gui.GuiContext
import io.github.notenoughupdates.moulconfig.observer.ObservableList
import io.github.notenoughupdates.moulconfig.xml.Bind
import io.github.notenoughupdates.moulconfig.xml.XMLUniverse

/**
 * Gui for analyzing [RepoPattern]s
 */
class RepoPatternGui private constructor() {

    companion object {

        /**
         * Open the [RepoPatternGui]
         */
        fun open() {
            SkyHanniMod.screenToOpen = GuiComponentWrapper(
                GuiContext(
                    XMLUniverse.getDefaultUniverse()
                        .load(RepoPatternGui(), MyResourceLocation("skyhanni", "gui/regexes.xml"))
                )
            )
        }
    }

    @field:Bind
    var search: String = ""
    private var lastSearch = null as String?
    private val allKeys = RepoPatternManager.allPatterns
        .sortedBy { it.key }
        .map { RepoPatternInfo(it) }
    private var searchCache = ObservableList(mutableListOf<RepoPatternInfo>())

    class RepoPatternInfo(
        repoPatternImpl: CommonPatternInfo<*, *>,
    ) {

        @field:Bind
        val key: String = repoPatternImpl.key

        val remoteData = when (repoPatternImpl) {
            is RepoPatternList -> repoPatternImpl.value.map { it.pattern() }
            is RepoPattern -> listOf(repoPatternImpl.value.pattern())
        }

        @field:Bind
        val regex: String = remoteData.joinToString("\n")

        @field:Bind
        val hoverRegex: List<String> = run {
            val localPatterns = when (repoPatternImpl) {
                is RepoPatternList -> repoPatternImpl.defaultPattern
                is RepoPattern -> listOf(repoPatternImpl.defaultPattern)
            }
            if (repoPatternImpl.isLoadedRemotely) {
                listOf(
                    "§aLoaded remotely",
                    "§7Remote:",
                ) + remoteData.map { " §f- $it" } + listOf(
                    "§7Local:",
                ) + localPatterns.map { " §f- $it" }
            } else {
                listOf("§cLoaded locally", "§7Local:") + localPatterns.map { " §f- $it" }
            }
        }

        @field:Bind
        val keyW = listOf(key)

        @field:Bind
        val overriden: String =
            if (repoPatternImpl.wasOverridden) "§9Overriden"
            else if (repoPatternImpl.isLoadedRemotely) "§aRemote"
            else "§cLocal"
    }

    @Bind
    fun poll(): String {
        if (search != lastSearch) {
            searchCache.clear()
            searchCache.addAll(allKeys.filter { search in it.key })
            lastSearch = search
        }
        return ""
    }

    @Bind
    fun searchResults(): ObservableList<RepoPatternInfo> {
        return searchCache
    }
}