#Guard tower fight at the base of the stair case, gonna make em work for these from eventAPI import getTag, setTag, giveItems, optionInput, combat
from APIUtils import *
from eventAPI import *

GtowerFlagFight1 = 0
GtowerFlagFight2 = 0

def fireEvent(e):
    global GtowerFlagFight1
    global GtowerFlagFight2
    GtowerFlagFight1 = getTag(e, "GtowerFlagFight1")
    GtowerFlagFight2 = getTag(e, "GtowerFlagFight2")
    if (GtowerFlagFight1 == 1):
        if(GtowerFlagFight2 == 1):    #Won Fight already
            return 1
        elif(GtowerFlagFight2 == 2):   #Ran Away last time
            showMessage(e, ["\"It's a brave person who attempts to do the impossible, a fool who tries it twice. Have at you!\""])
            fightThem2(e)
        else:                            #Died
            fightThem2(e)
    elif (GtowerFlagFight1 == 2):
        showMessage(e,
            ["\"The guards over this macabre prison were still waiting for you!\""])
        fightThem1(e)
    else:
        showMessage(e,
            ["As you approach the center of the level, your Banestaff comes alight!  In the surge of light, you see many spirits in this place.",
             "They appear to be somehow imprisoned all around you.  While the light from the banestaff shows the spirits to you, it does not appear to help free them.",
             "While you are caught in awe at the sight of these helpless spirits, the foul denizens of this place assault you."],
            optionInput)
        fightThem1(e)
    setTag(e, "GtowerFlagFight1", GtowerFlagFight1)
    setTag(e, "GtowerFlagFight2", GtowerFlagFight2)
    return 0


def fightThem1(e):
    combatRet = combat(e, [("Demon Lord", 5, 2, 40), ("Yevil", 8, 2, 20), ("Dactyl", 10, 0, 20) ])
    if (combatRet == 1):
        wonGuard1Fight(e)
    elif (combatRet == 3):
        lostGuard1Fight(e)
    else:
        defeatedGuard1Fight(e)

def wonGuard1Fight(e):
    global GtowerFlagFight1
    showMessage(e, 
        ["The last enemy falls before your might, and as you wipe the sweat from your brow, you hear a voice call to you",
         "\"You fight well and strongly, but these spirits will not be freed.\"",
         "As you turn to see where the voice came from, you feel the presence of a great magic force"])
    showPicture(e, "Necrolord")
    showMessage(e,
        ["\"You have become more than an annoyance, you have become a major threat.  We will have to deal with you accordingly.",
         "Too long have we toiled, and too many troubles we have faced to see it fall apart because of you.\"",
         "As the last words leave his lips, the shadwos come alive, and you are once again assaulted."])
    GtowerFlagFight1 = 1
    setTag(e, "GtowerFlagFight1", GtowerFlagFight1)
    #initiate second combat
    fightThem2(e)

def lostGuard1Fight(e):
    showMesasge(e,
        ["You fall to the enemy, and Rennibister falls with you."])

def defeatedGuard1Fight(e):
    global GtowerFlagFight1
    showMessage(e,
        ["You manage to evade your opponents, but you know that these spirits, and Rennibister, MUST be freed!"])
    GtowerFlagFight1 = 2
    setTag(e, "GtowerFlagFight1", GtowerFlagFight1)

def fightThem2(e):
    combatRet = combat(e, [("Necrolord", 1, 0, 80),("Demon Lord", 4, 2, 40), ("Yevil", 7, 2, 30), ("Dark Sidhe Lord", 3, 2, 20)])
    if (combatRet == 1):
        wonGuard2Fight(e)
    elif (combatRet == 3):
        lostGuard2Fight(e)
    else:
        defeatedGuard2Fight(e)

def wonGuard2Fight(e):
    global GtowerFlagFight2
    showMessage(e, 
        ["With the death of the head of the ExorArcanists, you know Rennibister will be able to rebuild.",
         "There still remains the matter of these trapped souls, whose screams echo in your ears now that their captor has been destroyed",
         "Within in the screams, you can make out some kind of message:",
         "\"..the....mancer....finger...open..\""])
    showMessage(e,
        ["Searching Frantically through the enemies's items again, you see a small glimmer and reach for it..."])
    giveItems(e, ["Ring of Astral Light"])
    showMessage(e,
        ["After taking the ring of Astral Light from the necromancer's body, you hold it up to examine the object.",
         "As you do, rays of a bright yellow light stream forth from the ring, pulsating and moving around rapidly.",
         "You can see the souls more clearly now, and they all seem to be making their way out of their prisons.",
         "With a final great flash, the entire room is filled with light so pure, you lose your vision for a few seconds, ",
         "when you regain your sight, the room is clear of all the spirits."])
    GtowerFlagFight2 = 1
    setTag(e, "GtowerFlagFight2", GtowerFlagFight2)
    #set olias plot, so that he knows you've won
    setTag(e, "OliasPlt1", 5)

def lostGuard2Fight(e):
    showMesasge(e,
        ["You fall to the Necrolord's horde"])

def defeatedGuard2Fight(e):
    global GtowerFlagFight2
    showMessage(e,
        ["You manage to evade your opponents, but you know these spirits, as well as Rennibister, MUST be freed!"])
    GtowerFlagFight2 = 2
    setTag(e, "GtowerFlagFight2", GtowerFlagFight1)
    
