require "recipecode"

Recipe = Recipe or {}
Recipe.OnCreate = Recipe.OnCreate or {}

function Recipe.OnCreate.HalberdShift(craftRecipeData, character)
	local sourceItem = craftRecipeData:getAllConsumedItems():get(0);
	local result = craftRecipeData:getAllCreatedItems():get(0);
    local player = getPlayer();
    local items = player:getInventory():getItems();
	print("craftRecipeData : " .. tostring(craftRecipeData))
	print("sourceItem : " .. tostring(sourceItem))
	print("result : " .. tostring(result))
    for i = 0, items:size()-1 do
        local item = items:get(i)
        if item:getFullType() == sourceItem:getFullType() then
		
            local modData = result:getModData()
            for k, v in pairs(item:getModData()) do
                modData[k] = v
            end
			
            if player:getPrimaryHandItem() == sourceItem then
                player:setPrimaryHandItem(result)
                player:setSecondaryHandItem(result)
            end			
            
			local hotbar = getPlayerHotbar(player:getPlayerNum())
			if hotbar and hotbar.attachedItems[1] == sourceItem then
				local slot = sourceItem:getAttachedSlot()
                local slotType = sourceItem:getAttachedSlotType()
                local attachedModel = sourceItem:getAttachedToModel()
                print("slot : " .. tostring(slot))
				print("slotType : " .. tostring(slotType))
				print("attachedModel : " .. tostring(attachedModel))
                player:removeAttachedItem(sourceItem)
                
                player:setAttachedItem(attachedModel, result)
                
                result:setAttachedSlot(slot)
                result:setAttachedSlotType(slotType)
                result:setAttachedToModel(attachedModel)

                syncItemFields(player, result)
			end
			
            return
        end
    end
end