#Embedded file name: eve/common/script/dogma/dogmaItems\characterDogmaItem.py
from eve.common.script.dogma.dogmaItems.shipFittableDogmaItem import ShipFittableDogmaItem
import util
import weakref
import sys

class CharacterDogmaItem(ShipFittableDogmaItem):
    __guid__ = 'dogmax.CharacterDogmaItem'

    def __init__(self, dogmaLocation, item):
        super(CharacterDogmaItem, self).__init__(dogmaLocation, item)
        self.fittedItems = {}

    def Load(self, item, instanceRow):
        super(CharacterDogmaItem, self).Load(item, instanceRow)
        self.ownerID = self.itemID
        attrs = self.attributes
        char = self.dogmaLocation.GetCharacter(item.itemID, flush=True)
        if util.IsDustCharacter(item.itemID):
            attrs[const.attributeIntelligence] = 20
            attrs[const.attributeCharisma] = 20
            attrs[const.attributePerception] = 20
            attrs[const.attributeMemory] = 20
            attrs[const.attributeWillpower] = 20
            return
        attrs[const.attributeIntelligence] = char.intelligence
        attrs[const.attributeCharisma] = char.charisma
        attrs[const.attributePerception] = char.perception
        attrs[const.attributeMemory] = char.memory
        attrs[const.attributeWillpower] = char.willpower

    def SetLocation(self, locationID, locationDogmaItem, flagID):
        if flagID != const.flagPilot:
            raise RuntimeError('CharacterDogmaItem::SetLocation - flag not pilot (%s, %s)' % (locationID, flagID))
        oldData = self.GetLocationInfo()
        self.location = weakref.proxy(locationDogmaItem)
        self.dogmaLocation.characterContext[self.itemID] = locationID
        self.dogmaLocation.pilotsByShipID[locationID] = self.itemID
        self.flagID = flagID
        locationDogmaItem.RegisterPilot(self)
        return oldData

    def UnsetLocation(self, locationDogmaItem):
        locationDogmaItem.UnregisterPilot(self)
        self.location = None
        try:
            del self.dogmaLocation.pilotsByShipID[locationDogmaItem.itemID]
        except KeyError:
            self.dogmaLocation.LogInfo('CharacterDogmaItem::UnsetLocation - pilot entry already cleared', locationDogmaItem.itemID)

    def RegisterFittedItem(self, dogmaItem, flagID):
        self.dogmaLocation.moduleListsByShipGroup[self.itemID][dogmaItem.groupID].add(dogmaItem.itemID)
        self.fittedItems[dogmaItem.itemID] = weakref.proxy(dogmaItem)

    def UnregisterFittedItem(self, dogmaItem):
        groupID = dogmaItem.groupID
        itemID = dogmaItem.itemID
        try:
            self.dogmaLocation.moduleListsByShipGroup[self.itemID][groupID].remove(itemID)
        except KeyError:
            self.dogmaLocation.LogError("UnregisterFittedItem::Tried to remove item from mlsg but group wasn't there", strx(dogmaItem))
            sys.exc_clear()
        except IndexError:
            self.dogmaLocation.LogError("UnregisterFittedItem::Tried to remove item from mlsg but it wasn't there", strx(dogmaItem))
            sys.exc_clear()

        try:
            del self.fittedItems[dogmaItem.itemID]
        except KeyError:
            self.dogmaLocation.LogError("UnregisterFittedItem::Tried to remove item from fittedItems but it wasn't there", strx(dogmaItem.itemID))

    def Unload(self):
        super(CharacterDogmaItem, self).Unload()
        itemID = self.itemID
        for itemKey in self.fittedItems.keys():
            self.dogmaLocation.UnloadItem(itemKey)

        if itemID in self.dogmaLocation.characterContext:
            del self.dogmaLocation.characterContext[itemID]
        if itemID in self.dogmaLocation.externalsByPilot:
            del self.dogmaLocation.externalsByPilot[itemID]
        if self.itemID in self.dogmaLocation.moduleListsByShipGroup:
            del self.dogmaLocation.moduleListsByShipGroup[self.itemID]

    def OnItemLoaded(self):
        self.dogmaLocation.LoadItemsInLocation(self.itemID)

    def CanFitItem(self, dogmaItem, flagID):
        if flagID in (const.flagSkill,
         const.flagSkillInTraining,
         const.flagBooster,
         const.flagImplant):
            return True
        return False

    def ValidFittingFlag(self, flagID):
        if flagID in (const.flagSkill,
         const.flagSkillInTraining,
         const.flagBooster,
         const.flagImplant):
            return True
        return False

    def GetFittedItems(self):
        return self.fittedItems

    def GetEnvironmentInfo(self):
        return util.KeyVal(itemID=self.itemID, shipID=self.GetShipID(), charID=self.itemID, otherID=None, targetID=None, effectID=None)

    def GetPersistables(self):
        ret = super(CharacterDogmaItem, self).GetPersistables()
        ret.update(self.fittedItems.keys())
        return ret

    def _FlushEffects(self):
        dogmaLM = self.dogmaLocation
        dogmaLM.AddIgnoreOwnerEventsCount(self.itemID)
        try:
            stackTraceCount = 0
            for fittedItem in self.fittedItems.itervalues():
                stackTraceCount += fittedItem.FlushEffects()

            stackTraceCount += super(CharacterDogmaItem, self)._FlushEffects()
        finally:
            dogmaLM.DecreaseOwnerRequiredEventsCount(self.itemID)

        return stackTraceCount

    def GetPilot(self):
        return self.itemID
