from eventAPI import *
from SDLKeys import SDLK_n, SDLK_y, SDLK_l, SDLK_f
from APIUtils import *
#import genericEmptyHouse

lFlagU = 0
YeodPlt = 0

def fireEvent(e):
    global lFlagU, YeodPlt
    lFlagU = getTag(e, "lFlagU")
    YeodPlt = getTag(e, "YeodPlt")
    if ((not issetall(YeodPlt, 0x1)) or
        (lFlagU == 1)):
        # He isn't here
        #genericEmptyHouse.fireEvent(e)
        return 1;
    elif (YeodPlt & 0x2):
        # He's already mad...
        madYeodaugh(e)
    else:
        setTag(e, "PltF1", getTag(e, "PltF1") & ~0x2) # turn off townie gangs
        showPicture(e, "Yeodaugh")
        if (optionInput(e,
            ["\"It is you who are to help me? Something of grave importance have I to tell you, hmm? I will tell you thus. You are with me are you not?\""],
            ["(N)o, we are not.",
             "(Y)es, we are!"],
            [SDLK_n, SDLK_y]) == SDLK_n):
            declineYeodaugh(e)
        else:
            listenToYeodaugh(e)
    setTag(e, "lFlagU", lFlagU)
    setTag(e, "YeodPlt", YeodPlt)
    return 1

def listenToYeodaugh(e):
    global lFlagU, YeodPlt
    optionInput(e,
        ["\"Wise you are to listen. The soldiers were right, but you should have stolen the vial. The soldiers are good and true, but they have been gravely deceived and they know not who is behind this foul malignancy. You must make ammends to all it seems.",
         "",
         "\"Make haste you will to see the lovely dark elven woman in a small 'L' shaped house in the southeastern part of towne. Go and do this. May the strength of the 'Way' be with you.\"",
         "",
         "After speaking with the wise old gnome, you feel somewhat refreshed..."],
        ["(L)eave this humble abode"], [SDLK_l])
    x, c = 0, getPartyCount(e)
    lFlagU = 1
    while (x < c):
        modifyBP(e, x, 10)
        modifyHP(e, x, 10)
        modifySP(e, x, 10)
        x += 1
    # Taire will be at home now
    setTag(e, "TairePlt1", 0x1)
    YeodPlt &= ~0x2

def declineYeodaugh(e):
    if (optionInput(e,
        ["\"Will you listen or will it be the HARD way for you?\"",
         "",
         "He glares, awaiting your response..."],
        ["(L)isten, we will.", "(F)ight us you must!"],
        [SDLK_l, SDLK_f]) == SDLK_l):
        listenToYeodaugh(e)
    else:
        fightYeodaugh(e)

def madYeodaugh(e):
    if (optionInput(e,
        ["\"Are you ready to listen or will it be the HARD way for you, again?!\"",
         "",
         "He glares, awaiting your response..."],
        ["(L)isten, we will.", "(F)ight us you must!"],
        [SDLK_l, SDLK_f]) == SDLK_l):
        listenToYeodaugh(e)
    else:
        fightYeodaugh(e)

def fightYeodaugh(e):
    global YeodPlt
    playSpecAudio(e, "combat")
    combatRet = combat(e, [("Yeodaugh", 1, 0, 30), ("Psi Knight", 1, 0, 10), ("Psi Knight", 1, 0, 10)])
    if (combatRet == 1):
        # Won
        showPicture(e, "House")
        optionInput(e,
            ["Having defeated the master Psi Lord and his Knights, you look around for something interesting to show the guards and find nothing.",
             "",
             "You suddenly hear a voice in the air...",
             "",
             "\"You fight well, but we will be back. We spirit leapt to avoid further bloodshed. Come back when you fight for good... \""],
            ["(L)eave the house"], [SDLK_l])
    elif (combatRet != 3):
        showMessage(e,
            ["A rather wide eyed half-elf Psi Knight looks on with concern, but decides there's not much reason in helping you at this point. He backs away and awaits Yeodaugh and the tall bearded human Psi Knight.",
             "",
             "Yeodaugh looks down at you...",
             "",
             "\"Leave me and return only when virtuous you have become.\""])
    YeodPlt |= 0x2
    setTag(e, "MallornPlt1", getTag(e, "MallornPlt1") | 1)
