package content.global.handlers.iface

import core.api.*
import core.game.node.entity.player.Player
import core.tools.RandomFunction
import core.game.interaction.InterfaceListener

object RulesAndInfo {
    val RULES = arrayOf(
        "<col=ffffff>1. Basically just don't be an asshole</col>",
        "   -No spamming, harassment, or other",
        "   disruptive behaviors",
        "<col=ffffff>2. No exploitation of bugs.</col>",
        "   -Zero tolerance. Can result in account deletion.",
        "   -If you discover a bug, report it to Emo/admins.",
        "<col=ffffff>3. Discuss or Advertising.</col>",
        "   -Discussion of the live jagex games is fine.",
        "   -Discussion of open source projects is fine.",
        "   -No advertising of non-open source RSPSs.",
        "<col=ffffff>4. No unfair advantages.</col>",
        "   -No botting, autoclickers, AHKing, etc...",
        "   -Again, just don't be an asshole."
    )
    val SEPARATOR = "<str>                                             </str>"
    val INFO = arrayOf(
        "<col=6bcdfa>Visit emoscape.org for more info </col>"
    )

    @JvmStatic
    fun openFor(player: Player)
    {
        if(getAttribute(player, "rules:confirmed", false) || !getAttribute(player, "tutorial:complete", false))
            return
        val pin = getAttribute(player, "rules:pin", RandomFunction.random(1000,9999))
        setAttribute(player, "/save:rules:pin", pin)

        var ln = setBaseRulesAndInfo(player)

        setInterfaceText(player, "<col=ffffff>If you agree to the above, type ::confirmrules $pin", 384, ln++)
        setInterfaceText(player, "", 384, ln)
        player.packetDispatch.sendInterfaceConfig(384, 17, true)
        openInterface(player, 384)
        player.lock()
    }

    @JvmStatic
    fun setBaseRulesAndInfo(player: Player): Int {
        var ln = 1
        for(line in INFO) setInterfaceText(player, INFO[ln - 1], 384, ln++)
        setInterfaceText(player, SEPARATOR, 384, ln++)
        val newIndex = ln
        for(line in RULES) setInterfaceText(player, RULES[ln - newIndex], 384, ln++)
        setInterfaceText(player, "", 384, ln)
        return ln
    }
}

class RulesListener : InterfaceListener
{
    override fun defineInterfaceListeners() {
        onClose(384){player, _ ->
            if(!getAttribute(player, "rules:confirmed", false))
                runTask(player, 1) { RulesAndInfo.openFor(player); sendDialogue(player, "Please read the rules.") }
            return@onClose true
        }
    }
}