##### NOTE, ALWAYS CALL ' CHANGE SETTINGS ' AFTER EVERY METHOD
##### THIS HELPS FIX THE BUGGY GUI SYSTEM

##### NOTE: YOU NEED TO IMPORT * FROM ItemIDs INTERFACE!

####################################################
#  _____                      _                    #
# |_   _|                    | |                   #
#   | |  _ ____   _____ _ __ | |_ ___  _ __ _   _  #
#   | | | '_ \ \ / / _ \ '_ \| __/ _ \| '__| | | | #
#  _| |_| | | \ V /  __/ | | | || (_) | |  | |_| | #
# |_____|_| |_|\_/ \___|_| |_|\__\___/|_|   \__, | #
#                                            __/ | #
#                                           |___/  #
####################################################
                                           

############### ADDING AND REMOVING ITEMS #######################
def drop_item(itemId, quantity, durability, slotNumber):
    #Make sure items can be dropped:
    for i in Player.inventorySlots:
        if i.itemId == itemId and i.quantity <= 0: return
        
    #Create new dropped item to be shown on ground
    DroppedItem.dropItemsWithOffsetAsStack(itemId, quantity, 10, Player.center.x, Player.center.y, durability)
    
    # Remove the item from the inventory
    remove_item_with_slot_number(itemId, quantity, slotNumber)
    
    Inventory.changeSettings()
    
def remove_item(itemId, quantity):
    for slot in Player.inventorySlots:
        if slot.itemId == itemId:
            temp = slot.quantity
            slot.quantity -= quantity
            quantity -= temp
            slot.mouseClicked = 0
    
            if slot.quantity <= 0: slot.itemId = slot.quantity = slot.durability = 0
                
            # if the amount of things to remove has been satisfied
            if quantity <= 0: break
    Inventory.changeSettings()

def remove_item_with_slot_number(itemId, quantity, slotNumber):
    for i in Player.inventorySlots:
        if i.slotNumber == slotNumber:
            if i.itemId == itemId:
                i.quantity -= quantity
                i.mouseClicked = False
                if i.quantity <= 0: i.itemId = i.quantity = i.durability = 0
                break
            
    Inventory.changeSettings()

def remove_equipment(itemId, quantity, slotNumber):
    for i in Player.equipmentSlots:
        if i.slotNumber == slotNumber:
            if i.itemId == itemId:
                i.quantity -= quantity
                
                #### FOR LIGHTS, TORCHES etc
                if i.itemId == TORCH: Player.playerLights.remove(Player.torchLight)
                            
                if i.quantity <= 0: i.itemId = i.quantity = 0
                
                i.durability = 0
                i.mouseClicked = False
                break
            
    Inventory.changeSettings()

def add_item(itemId, quantity, durability):
    # CANT ADD ITEM IF THE INVEN IS FULL
    if inventory_is_full(itemId, quantity): return
            
    # IF THE ITEM IS STACKABLE, RUN THIS LOOP
    if InvenHelper.isStackable(itemId):
        for i in Player.inventorySlots:
            if i.itemId == itemId and (i.quantity + quantity) <= InvenHelper.getStackableAmount(itemId):
                i.mouseClicked = False
                i.quantity += quantity
                Inventory.changeSettings()
                return
                
    # IF THE ITEM IS NOT STACKABLE, OR FAILED THE ABOVE LOOP, RUN THIS LOOP
    for i in Player.inventorySlots:
        if(i.itemId == 0):
            i.mouseClicked = False
            i.itemId = itemId
            i.quantity = quantity
            i.durability = durability
            i.equippable = InvenHelper.isEquippable(itemId)
            Inventory.changeSettings()
            return
        
############### END ADDING AND REMOVING ITEMS #################################################################

################ MISC ##################################################################################

def inventory_is_full(itemId, quantity):
    if InvenHelper.isStackable(itemId): # IF ITEM IS STACKABLE, RUN THIS LOOP
        for slot in Player.inventorySlots:
            # CHECK IF THE (QUANTITY + THE ADDED QUANTITY) IS <= THE MAX QUANTITY
            WOULD_BE_QUANTITY = slot.quantity + quantity
            if slot.itemId == itemId and WOULD_BE_QUANTITY <= InvenHelper.getStackableAmount(itemId): return False
        
    # IF IT IS NOT STACKABLE, OR THE ITEM FAILED THE FIRST LOOP, RUN THIS LOOP
    for slot in Player.inventorySlots:
        if slot.itemId == NULL_ITEM: return False
            
    # IF NONE OF THE ABOVE CONDITIONS COULD BE MET, THE INVENTORY MUST BE FULL
    return True

def inventory_contains(itemId, quantity):
    count = 0
    for slot in Player.inventorySlots:
        if slot.itemId == itemId: count += slot.quantity
    return count >= quantity

############## END MISC #######################################################################################

        