#locked treasure chest
from eventAPI import *
from APIUtils import *
from SDLKeys import SDLK_p, SDLK_c, SDLK_o, SDLK_b, SDLK_l
from SDLKeys import SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_ESCAPE

rtmelvinFlag = 0

def fireEvent(e):
   global rtmelvinFlag
   rtmelvinFlag = getTag(e, "rtmelvinFlag")
   if (rtmelvinFlag >= 3):
      emptychest(e)
   else:
      showPicture(e, "Treasure")
      showMessage(e, ["There is a chest sitting here in the corner.  It is bound by several locks and appears to be constructed of solid steel."])
      lockedchest(e)
   return 1

def lockedchest(e):
   choice = optionInput(e, ["Before you is a chest.  What will you do?", "", "(P)ick Lock", "(C)ast Trip Trap", "(O)pen it", "(B)ash it", "(L)eave"], [], [SDLK_p, SDLK_c, SDLK_o, SDLK_b, SDLK_l])
   if (choice == SDLK_p):
      picklock(e)
   elif (choice == SDLK_c):
      triptrap(e)
   elif (choice == SDLK_o):
      openchest(e)
   elif (choice == SDLK_b):
      bashchest(e)
   elif (choice == SDLK_l):
      return 1

def picklock(e):
   choicelist = [SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_ESCAPE] 
   choice = optionInput(e, ["Who will attempt to pick the lock?"], ["(F1-F8) for Character", "(ESCAPE) to Cancel"], choicelist)
   placenumber = choicelist.index(choice)
   if (placenumber == 8):
      lockedchest(e)
   elif (placenumber >= getPartyCount(e)):
      picklock(e)
   else:
      showMessage(e, ["You struggle with one of the locks until it finally pops open.", "", "You begin work on another lock, when suddenly the first lock snaps shut once again."])
      lockedchest(e)

def triptrap(e):
   choicelist = [SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_ESCAPE] 
   choice = optionInput(e, ["Who will cast Trip Trap?"], ["(F1-F8) for Character", "(ESCAPE) to Cancel"], choicelist)
   placenumber = choicelist.index(choice)
   if (placenumber == 8):
      lockedchest(e)
   elif (placenumber >= getPartyCount(e)):
      triptrap(e)
   else:
      showMessage(e, ["The chest appears to be warded against such spells."])
      lockedchest(e)

def openchest(e):
   choicelist = [SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_ESCAPE] 
   choice = optionInput(e, ["Who will open?"], ["(F1-F8) for Character", "(ESCAPE) to Cancel"], choicelist)
   placenumber = choicelist.index(choice)
   if (placenumber == 8):
      lockedchest(e)
   elif (placenumber >= getPartyCount(e)):
      openchest(e)
   else:
      showMessage(e, ["You cannot pry it open as long as it is locked."])
      lockedchest(e)

def bashchest(e):
   choicelist = [SDLK_F1, SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5, SDLK_F6, SDLK_F7, SDLK_F8, SDLK_ESCAPE] 
   choice = optionInput(e, ["Who will bash the chest?"], ["(F1-F8) for Character", "(ESCAPE) to Cancel"], choicelist)
   placenumber = choicelist.index(choice)
   if (placenumber == 8):
      lockedchest(e)
   elif (placenumber >= getPartyCount(e)):
      openchest(e)
   else:
      showMessage(e, ["You hit the chest with such force that it makes your head ring.  The chest now has a small scratch on its surface."])
      lockedchest(e)

def emptychest(e):
   showPicture(e, "Treasure")
   showMessage(e, ["There is a large opened chest here.  Whomever opened this was an expert.", "", "Peering inside, you see that the chest is completely empty."])
