aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/miscgui/customtodos/CustomTodo.kt
blob: 181f33356fb129273d39f1c0ce85c1f642d5c2b7 (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
97
98
/*
 * Copyright (C) 2023 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.miscgui.customtodos

import com.google.gson.annotations.Expose
import io.github.moulberry.notenoughupdates.util.SBInfo
import io.github.moulberry.notenoughupdates.util.TemplateUtil
import io.github.moulberry.notenoughupdates.util.kotlin.KSerializable

@KSerializable
data class CustomTodo(
    @Expose var label: String,
    @Expose var timer: Int,
    @Expose var trigger: String,
    @Expose var icon: String,
    @Expose var isResetOffset: Boolean,
    @Expose var showWhen: Int = 0,
    @Expose var showOnlyWhenReady: Boolean = false,
    @Expose var triggerTarget: TriggerTarget = TriggerTarget.CHAT,
    @Expose var triggerMatcher: TriggerMatcher = TriggerMatcher.CONTAINS,
    @Expose var readyAt: MutableMap<String, Long> = mutableMapOf(),
    @Expose var enabled: MutableMap<String, Boolean> = mutableMapOf(),
) {
    enum class TriggerMatcher {
        REGEX, STARTS_WITH, CONTAINS, EQUALS
    }

    enum class TriggerTarget {
        CHAT, ACTIONBAR, TAB_LIST, SIDEBAR
    }

    fun isValid(): Boolean {
        return timer >= 0 && !trigger.isBlank()
    }

    fun setDoneNow() {
        val t = System.currentTimeMillis()
        readyAt[SBInfo.getInstance().currentProfile ?: return] =
            if (isResetOffset) {
                t + DAY - t % DAY + timer * 1000L
            } else {
                t + timer * 1000L
            }
    }

    var readyAtOnCurrentProfile: Long?
        get() {
            return readyAt[SBInfo.getInstance().currentProfile ?: return null]
        }
        set(value) {
            readyAt[SBInfo.getInstance().currentProfile ?: return] = value ?: return
        }

    var isEnabledOnCurrentProfile: Boolean
        get() {
            return enabled[SBInfo.getInstance().currentProfile ?: return true] ?: true
        }
        set(value) {
            enabled[SBInfo.getInstance().currentProfile ?: return] = value
        }


    companion object {
        val templatePrefix = "NEU:CUSTOMTODO/"
        val DAY = (24 * 60 * 60 * 100)
        fun fromTemplate(data: String): CustomTodo? {
            return TemplateUtil.maybeDecodeTemplate(templatePrefix, data, CustomTodo::class.java)
                ?.also {
                    it.enabled.clear()
                    it.readyAt.clear()
                }
        }
    }

    fun toTemplate(): String {
        return TemplateUtil.encodeTemplate(
            templatePrefix,
            this.copy(enabled = mutableMapOf(), readyAt = mutableMapOf())
        )
    }
}