package content.region.misthalin.varrock.quest.shieldofarrav

import core.game.node.item.GroundItemManager
import core.game.node.item.Item
import core.game.dialogue.DialogueFile
import core.tools.END_DIALOGUE
import core.tools.START_DIALOGUE
import content.data.Quests
import core.game.dialogue.FacialExpression
import core.api.Container
import core.api.inInventory
import core.api.removeItem
import core.game.dialogue.DialoguePlugin


private val CERTIFICATE = Item(769)


class KingRoaldArravDialogue() : DialogueFile() {
    companion object {
        const val BRIBE_PRICE = 6000
    }
    override fun handle(componentID: Int, buttonID: Int) {

        if (player!!.inventory.containsItem(ShieldofArrav.PHOENIX_SHIELD) || player!!.inventory.containsItem(ShieldofArrav.BLACKARM_SHIELD)) {
            when (stage) {
                START_DIALOGUE -> player("Your majesty, I have recovered the Shield of Arrav; I", "would like to claim the reward.").also { stage++ }
                1 -> npc("The Shield of Arrav, eh? Yes, I do recall my father,", "King Roald, put a reward out for that.").also { stage++ }
                2 -> npc("Very well.").also { stage++ }
                3 -> npc("If you get the authenticity of the shield verified by the", "curator at the museum and then return here with", "authentication, I will grant your reward.").also { stage = END_DIALOGUE }
                END_DIALOGUE -> end()
            }
        }

        else if(player!!.getInventory().containsItem(ShieldofArrav.BLACKARM_CERTIFICATE) || player!!.getInventory().containsItem(ShieldofArrav.PHOENIX_CERTIFICATE)){
            when(stage) {
                START_DIALOGUE -> player("Your majesty, I have come to claim the reward for the", "return of the Shield of Arrav.").also { stage++ }
                1 -> interpreter!!.sendItemMessage(if (player!!.inventory.containsItem(ShieldofArrav.BLACKARM_CERTIFICATE)) ShieldofArrav.BLACKARM_CERTIFICATE.id else ShieldofArrav.PHOENIX_CERTIFICATE.id, "You show the certificate to the king.").also { stage++ }
                2 -> npc("I'm  afraid that's only half the reward certificate. You'll", "have to get the other half and join them together if you", "want to cliam the reward.").also { stage = END_DIALOGUE }
                END_DIALOGUE -> end()
            }
        }

        else if(player!!.inventory.containsItem(CERTIFICATE)){
            when(stage){
                START_DIALOGUE -> player("Your majesty, I have come to claim the reward for the", "return of the Shield of Arrav.").also { stage++ }
                1 -> interpreter!!.sendItemMessage(CERTIFICATE.id, "You show the certificate to the king.").also { stage++ }
                2 -> npc("My goodness! This claim is for the reward offered by", "my father many years ago!").also { stage++ }
                3 -> npc("I never thought I would live to see the day when", "someone came forward to claim this reward!").also { stage++ }
                4 -> npc("I heard that you found half the shield, so I will give", "you half of the bounty. That comes to exactly 600 gp!").also { stage++ }
                5 -> {
                    interpreter!!.sendItemMessage(CERTIFICATE.id, "You hand over a certificate. The king gives you 600 gp.")
                    if (player!!.inventory.remove(CERTIFICATE)) {
                        if (!player!!.inventory.add(Item(995, 600))) {
                            GroundItemManager.create(Item(995, 600), player)
                        }
                        player!!.questRepository.getQuest(Quests.SHIELD_OF_ARRAV).finish(player)
                        stage = END_DIALOGUE
                    }
                }
            }
        }

        else {
            when (stage) {
            START_DIALOGUE -> player("I would like to contribute to one of", "your Political Action Committees").also{ stage++ }
            1 -> npcl("Which one?").also { stage++ }
            2 -> playerl("The Citizens Who Can't Be Bothered To Do Shield of Arrav").also { stage++ }
            3 -> npcl(FacialExpression.HALF_THINKING, "I see... that will be 6,000 coins").also { stage++ }
            4 -> options("Here you go!", "Nevermind.").also { stage++ }
            5 -> when (buttonID) {
                1 -> {
                    if (inInventory(player!!, 995, BRIBE_PRICE))
                        playerl(FacialExpression.FRIENDLY, "Here you go!").also { stage = 20 }
                    else
                        playerl(FacialExpression.HALF_GUILTY, "Actually, I don't have that much.").also { stage = END_DIALOGUE }
                }
                2 -> playerl(FacialExpression.FRIENDLY, "On second thought, never mind.").also { stage = END_DIALOGUE }
            }
            20 -> {
                npcl("Thank you so much, dear donor. The government of Varrock now has higher priorities than the Shield of Arrav.")
                if (removeItem(player!!, Item(995, BRIBE_PRICE), Container.INVENTORY)) {
                    player!!.questRepository.getQuest(Quests.SHIELD_OF_ARRAV).finish(player)
                }
                stage = END_DIALOGUE
            }
        }
        }
    }
}