import sys
import traceback
from SDLKeys import SDLK_SPACE, SDLK_y, SDLK_n
import random
from eventAPI import *

def issetall(val, mask):
    return ((val & mask) == mask)
    #return input("issetall: return 0 or 1?")

def isclearall(val, mask):
    return ((val & mask) == 0)
    #return input("isclearall: return 0 or 1?")

def issetany(val, mask):
    return ((val & mask) != 0)
    #return input("issetany: return 0 or 1?")

def isclearany(val, mask):
    return ((val & mask) != mask)
    #return input("isclearany: return 0 or 1?")

def showMessage(eventPtr, msg, oi=optionInput):
    oi(eventPtr, msg, ["(SPACE) to Continue"], [SDLK_SPACE])
    return 0

def poseYesNo(eventPtr, msg, oi=optionInput):
    return (oi(eventPtr, msg, ["(Y)es", "(N)o"], [SDLK_y, SDLK_n]) == SDLK_y)

def splitXP(eventPtr, XP):
    x, c = 0, getPartyCount(eventPtr)
    xpEach = int(XP / c)
    while (x < c):
        modifyXP(eventPtr, x, xpEach)
        x += 1

def firstPC(eventPtr):
    x, c = 0, getPartyCount(eventPtr)
    while (x < c):
        if (isPC(eventPtr, x)):
            return x
        x += 1
    return 0

def firstHasItem(eventPtr, item):
    x, c = 0, getPartyCount(eventPtr)
    while (x < c):
        if (hasItem(eventPtr, x, item)):
            return x
        x += 1
    return -1

def removeAllItems(eventPtr, item):
    holder = firstHasItem(eventPtr, item)
    while (holder > -1):
        removeItem(eventPtr, holder, item)
        holder = firstHasItem(eventPtr, item)

def giveGoldToFirstPC(eventPtr, gp):
    x, c = 0, getPartyCount(eventPtr)
    while (x < c):
        if (isPC(eventPtr, x)):
            modifyGold(eventPtr, x, gp)
            return
        else:
            x=x+1

def dice(num, die, extra):
    retVal = extra
    d = 0
    while d < num:
        retVal += random.randrange(1, die)
        d += 1
    return retVal

def guardedChest(e, varName, exterminatedFlag, lootedFlag, guardList, wonMsgList,
                 alreadyWonMsgList, lootedMsgList, chestType, chestDiff, gold,
                 itemList, randItems, chestText, picture):
    plotVar = getTag(e, varName)
    if (not isclearany(plotVar, exterminatedFlag | lootedFlag)):
        return 1
    if (not issetall(plotVar, exterminatedFlag)):
        combatRet = combat(e, guardList)
        if (combatRet != 1):
            return 0
        showPicture(e, picture)
        showMessage(e, wonMsgList)
        plotVar |= exterminatedFlag
        setTag(e, varName, plotVar)
    else:
        showPicture(e, picture)
        showMessage(e, alreadyWonMsgList)
    chestRet = chest(e, chestType, chestDiff, gold, itemList, randItems, chestText)
    if (chestRet != 1):
        return 0
    showMessage(e, lootedMsgList)
    plotVar |= lootedFlag
    setTag(e, varName, plotVar)
    return 1

# for some reason, the for loop throws a TypeError on a regular (but strange
#  basis...
def dice2(num, die, extra):
    print "dice.1", num, die, extra
    retVal = extra
    print "dice.2", retVal
    r = range(num)
    print r
    i = 0
    pass
    try:
        for i in r:
            # print "dice.3", retVal
            retVal += random.randrange(1, die)
            # print "dice.3.5", retVal
            # print i, r
    except TypeError:
        print "TypeError?", sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
    print "dice.4"
    return retVal

# Returns the year associated with 'inTime' (as an integer)
def gameTimeYear(inTime):
    return int(inTime/2096640)+1

# Returns the month associated with 'inTime' (as an integer)
def gameTimeMonth(inTime):
    return int((inTime-((gameTimeYear(inTime)-1)*2096640))/161280)+1

# Returns the day associated with 'inTime' (as an integer)
def gameTimeDay(inTime):
    return int(((inTime-((gameTimeYear(inTime)-1)*2096640))-((gameTimeMonth(inTime)-1)*161280))/5760)+1

# Returns the hour associated with 'inTime' (as an integer)
def gameTimeHour(inTime):
    return int((((inTime-((gameTimeYear(inTime)-1)*2096640))-((gameTimeMonth(inTime)-1)*161280))-((gameTimeDay(inTime)-1)*5760))/240)

# Returns the minute associated with 'inTime' (as an integer)
def gameTimeMinute(inTime):
    return int(((((inTime-((gameTimeYear(inTime)-1)*2096640))-((gameTimeMonth(inTime)-1)*161280))-((gameTimeDay(inTime)-1)*5760))-(gameTimeHour(inTime)*240))/4)

# Returns the second associated with 'inTime' (as an integer)
def gameTimeSecond(inTime):
    return int((((((inTime-((gameTimeYear(inTime)-1)*2096640))-((gameTimeMonth(inTime)-1)*161280))-((gameTimeDay(inTime)-1)*5760))-(gameTimeHour(inTime)*240))-(gameTimeMinute(inTime)*4))*15)

# Takes a game time in ticks, and returns a string that looks like
# YYY:MM:DD:HH:MM:SS
def gameTimeParse(inTime):
    return "%02d:%02d:%02d:%02d:%02d:%02d"%(gameTimeYear(inTime),
                                            gameTimeMonth(inTime),
                                            gameTimeDay(inTime),
                                            gameTimeHour(inTime),
                                            gameTimeMinute(inTime),
                                            gameTimeSecond(inTime))
