#Asher's Tomb - level 1 crypt thing
from eventAPI import getTag, setTag, giveItems, optionInput, combat, modifyXP, getPartyCount
from APIUtils import isclearall, showMessage, showPicture
from SDLKeys import SDLK_l, SDLK_b, SDLK_e, SDLK_f

CryptThing1 = 0

def fireEvent(e):
    global CryptThing1
    CryptThing1 = getTag(e, "CryptThing1")
    if (CryptThing1 == 0):
        firstMeet(e)
    elif (CryptThing1 == 1):
        returnMeet(e)
    elif (CryptThing1 == 2):
        fightHim(e)
    elif (CryptThing1 == 3):
        grantPassage(e)
    else:
        return 1
   
    return 0

def firstMeet(e):
    showMessage(e,
        ["As you approach the doors, a rather large, ungainly thing slowly rises from the floor",
         "It appears to have been covered in the dust and dirt of a few hundred years, but begins to take shape before your eyes",
         "it begins to speak, in a coarse, unnatural voice:",
         "\"Those who would seek out the master of this place would need passage to the door behind me,",
         "but I must know you would not seek to harm master Asher.\""])
    showPicture(e, "Wight")
    setTag(e, "CryptThing1", 1)
    returnMeet(e)

def returnMeet(e):
    showPicture(e, "Wight")
    keyPress = optionInput(e, ["Tell me, why would you seek out master Asher?"],
        ["(E)nd his torment", "(B)anish his evil", "(L)earn his secrets", "(F)ind help for our cause"],
        [SDLK_e, SDLK_b, SDLK_l, SDLK_f])

    if(keyPress == SDLK_e):
        showMessage(e, ["The only tormented ones here will be you and your fellows!"])
        setTag(e, "CryptThing1", 2)
        fightHim(e)
    elif(keyPress == SDLK_b):
        showMessage(e, ["Many have tried, you may have met what remains of them on the way here"])
        setTag(e, "CryptThing1", 2)
        fightHim(e)
    elif(keyPress == SDLK_f):
        showMessage(e, ["Master Asher cares not for charity, especially for the living!"])
        setTag(e, "CryptThing1", 2)
        fightHim(e)
    elif(keyPress == SDLK_l):
        showMessage(e, ["Wisdom and Learning are the only things Master honors, you may proceed"])
        x, c = 0, getPartyCount(e)
        while (x < c):
            modifyXP(e, x, 100000)
            x += 1
        setTag(e, "CryptThing1", 3)
        grantPassage(e)

def fightHim(e):
    combatRet = combat(e, [("Wight", 10, 2, 10), ("Lich", 5, 2, 30), ("Crypt Thing", 1, 0, 50)])
    if (combatRet == 1):
        wonCryptThingFight(e)
    elif (combatRet == 3):
        lostCryptThingFight(e)
    else:
        defeatedCryptThingFight(e)

def wonCryptThingFight(e):
    global CryptThing1
    showMessage(e, 
        ["With the last blow delivered to the Crypt Thing, it crumbles into dust, hopefully forever"])
    setTag(e, "CryptThing1", 4)

def lostCryptThingFight(e):
    showMesasge(e,
        ["With your last gasp of breath, you see your enemies smiling as they know your end is near"])

def defeatedCryptThingFight(e):
    global CryptThing1
    showMessage(e,
        ["Your retreating footsteps fall on deaf ears"])
    setTag(e, "CryptThing1", 2)
    
