local xhelds = {
[1] = {name = "X-Accuracy(Tier: 1)"},
[2] = {name = "X-Accuracy(Tier: 2)"},
[3] = {name = "X-Accuracy(Tier: 3)"},
[16] = {name = "X-Defense(Tier: 1)"},
[18] = {name = "X-Defense(Tier: 3)"},
[7] = {name = "X-Attack(Tier : 1)"},
[8] = {name = "X-Attack(Tier : 2)"},
[9] = {name = "X-Attack(Tier: 3)"},
[43] = {name = "X-Return(Tier: 1)"},
[44] = {name = "X-Return(Tier: 2)"},
[45] = {name = "X-Return(Tier: 3)"},
[31] = {name = "X-Hellfire(Tier: 1)"},
[32] = {name = "X-Hellfire(Tier: 2)"},
[33] = {name = "X-Hellfire(Tier: 3)"},
[37] = {name = "X-Poison(Tier: 1)"},
[38] = {name = "X-Poison(Tier: 2)"},
[39] = {name = "X-Poison(Tier: 3)"},
[10] = {name = "X-Boost(Tier: 1)"},
[11] = {name = "X-Boost(Tier: 2)"},
[12] = {name = "X-Boost(Tier: 3)"},
[4] = {name = "X-Agility(Tier: 1)"},
[5] = {name = "X-Agility(Tier: 2)"},
[6] = {name = "X-Agility(Tier: 3)"},
[46] = {name = "X-Strafe(Tier: 1)"},
[47] = {name = "X-Strafe(Tier: 2)"},
[48] = {name = "X-Strafe(Tier: 3)"},
[40] = {name = "X-Rage(Tier: 1)"},
[41] = {name = "X-Rage(Tier: 2)"},
[42] = {name = "X-Rage(Tier: 3)"},
[13] = {name = "X-Critical(Tier: 1)"},
[14] = {name = "X-Critical(Tier: 2)"},
[15] = {name = "X-Critical(Tier: 3)"},
[19] = {name = "X-Elemental(Tier: 1)"},
[20] = {name = "X-Elemental(Tier: 2)"},
[21] = {name = "X-Elemental(Tier: 3)"},
[22] = {name = "X-Experience(Tier: 1)"},
[23] = {name = "X-Experience(Tier: 2)"},
[24] = {name = "X-Experience(Tier: 3)"},
[25] = {name = "X-Harden(Tier: 1)"},
[26] = {name = "X-Harden(Tier: 2)"},
[27] = {name = "X-Harden(Tier: 3)"},
[28] = {name = "X-Haste(Tier: 1)"},
[29] = {name = "X-Haste(Tier: 2)"},
[30] = {name = "X-Haste(Tier: 3)"},
[34] = {name = "X-Lucky(Tier: 1)"},
[35] = {name = "X-Lucky(Tier: 2)"},
[36] = {name = "X-Lucky(Tier: 3)"},
[49] = {name = "X-Vitality(Tier: 1)"},
[50] = {name = "X-Vitality(Tier: 2)"},
[51] = {name = "X-Vitality(Tier: 3)"},
}

local yhelds = {
[7] = {name = "Y-Regeneration(Tier: 1)"},
[8] = {name = "Y-Regeneration(Tier: 2)"},
[9] = {name = "Y-Regeneration(Tier: 3)"},
[4] = {name = "Y-Cure(Tier: 1)"},
[5] = {name = "Y-Cure(Tier: 2)"},
[6] = {name = "Y-Cure(Tier: 3)"},
[1] = {name = "Y-Control(Tier: 1)"},
[2] = {name = "Y-Control(Tier: 2)"},
[3] = {name = "Y-Control(Tier: 3)"},
[10] = {name = "Y-Teleport(Tier: 1)"},
[11] = {name = "Y-Teleport(Tier: 2)"},
[12] = {name = "Y-Teleport(Tier: 3)"},
[13] = {name = "Y-Wing(Tier: 1)"},
[14] = {name = "Y-Wing(Tier: 2)"},
[15] = {name = "Y-Wing(Tier: 3)"},
}


function onTradeRequest(cid, target, item)

	for a, b in pairs (pokeballs) do
		if b.use == item.itemid then
			doPlayerSendCancel(cid, "You can't trade this item.")
		return false
		end
	end
	
	if isContainer(item.uid) then
	   local bagItems = getItensUniquesInContainer(item.uid)
	   if #bagItems >= 1 then
	      doPlayerSendCancel(cid, "Has a Unique Item in this bag, you can't trade this item.")     --alterado v1.6
	      return false
       end     
	elseif getItemAttribute(item.uid, "unique") then    --alterado v1.6
	   doPlayerSendCancel(cid, "It is a Unique Item, you can't trade this item.")
	   return false
	end   
	
	if isContainer(item.uid) then
	   local itens = getPokeballsInContainer(item.uid)
	   if #itens >= 1 then                             
	      for i = 1, #itens do
	          local lvl = getItemAttribute(itens[i], "level")
	          local name = getItemAttribute(itens[i], "poke")
	          if not lvl then
	             doItemSetAttribute(itens[i], "level", pokes[name].level)     --alterado v1.6
              end
           end
       end
    elseif isPokeball(item.itemid) then
	   local lvl = getItemAttribute(item.uid, "level")
	   local name = getItemAttribute(item.uid, "poke")
	   if not lvl then
	      doItemSetAttribute(item.uid, "level", pokes[name].level)
       end
    end
    
    if getPlayerStorageValue(cid, 52480) >= 1 then             --alterado v1.6.1
       doPlayerSendTextMessage(cid, 20, "You can't do that while be in a duel!")
       return false
    end
    
    if isPokeball(item.itemid) then
       local name = getItemAttribute(item.uid, "poke")            --alterado v1.8 \/
       local boost = getItemAttribute(item.uid, "boost") or 0
       local nick = getItemAttribute(item.uid, "nick") or ""
    
       local str = "Pokemon in trade: "
       str = str.."Name: "..name.."   Boost: "..boost.."   "
       if nick ~= "" then str = str.."Nick: "..nick.."" end
	   local heldx = getItemAttribute(item.uid, "heldx")
       local heldy = getItemAttribute(item.uid, "heldy")
	   if heldx and heldy then
str = str.."Holding: "..(xhelds[heldx].name).." and "..(yhelds[heldy].name)..". "
elseif heldx then
str = str.."Holding: "..(xhelds[heldx].name)..". "
elseif heldy then
str = str.."Holding: "..(yhelds[heldy].name)..". "
end
       sendMsgToPlayer(target, 20, str)
    end
                                                            --alterado v1.8 \/
    if isContainer(item.uid) then
	   local itens = getPokeballsInContainer(item.uid)
	   if #itens >= 1 then                             
	      for i = 1, #itens do
	          if isPokeball(getThing(itens[i]).itemid) then
	             local name = getItemAttribute(itens[i], "poke")            
                 local boost = getItemAttribute(itens[i], "boost") or 0
                 local nick = getItemAttribute(itens[i], "nick") or ""
				 	   local heldx = getItemAttribute(itens[i], "heldx")
       local heldy = getItemAttribute(itens[i], "heldy")
    
                 local str = "Pokemon in trade: "
                 str = str.."Name: "..name.."   Boost: "..boost.."   "
                 if nick ~= "" then str = str.."Nick: "..nick.."" end
				  if heldx and heldy then
str = str.."Holding: "..(xhelds[heldx].name).." and "..(yhelds[heldy].name)..". "
elseif heldx then
str = str.."Holding: "..(xhelds[heldx].name)..". "
elseif heldy then
str = str.."Holding: "..(yhelds[heldy].name)..". "
end
                 sendMsgToPlayer(target, 20, str)
              end
          end
	   end       
    end
    

return true
end

local function noCap(cid, sid)
	if isCreature(cid) then
		doPlayerSendCancel(cid, "You can't carry more than six pokemons, trade cancelled.")
	end
	if isCreature(sid) then
		doPlayerSendCancel(sid, "You can't carry more than six pokemons, trade cancelled.")
	end
end

function onTradeAccept(cid, target, item, targetItem)

	local pbs = #getPokeballsInContainer(item.uid)
	local cancel = false
	local p1 = 0
	local p2 = 0
	local itemPokeball = isPokeball(item.itemid) and 1 or 0
	local targetItemPokeball = isPokeball(targetItem.itemid) and 1 or 0

    if getPlayerMana(cid) + itemPokeball > 6 then  --alterado v1.6
	   cancel = true
	   p1 = cid
    end
    if getPlayerMana(target) + targetItemPokeball > 6 then  --alterado v1.6
	   cancel = true
	   p2 = target
    end
    
	if pbs > 0 and getCreatureMana(target) + pbs > 6 + targetItemPokeball then
		cancel = true	
		p1 = target
	end

	pbs = #getPokeballsInContainer(targetItem.uid)

	if pbs > 0 and getCreatureMana(cid) + pbs > 6 + itemPokeball then
		cancel = true
		p2 = cid
	end

	if cancel then
		addEvent(noCap, 20, p1, p2)
	return false
	end

	if itemPokeball == 1 and targetItemPokeball == 1 then
		setPlayerStorageValue(cid, 8900, 1)
		setPlayerStorageValue(target, 8900, 1)
	end

return true
end