package me.rkfg.xmpp.bot.plugins.game.event;

import static me.rkfg.xmpp.bot.plugins.game.misc.Attrs.*;
import static me.rkfg.xmpp.bot.plugins.game.misc.Utils.*;

import java.util.Optional;

import me.rkfg.xmpp.bot.plugins.game.IPlayer;
import me.rkfg.xmpp.bot.plugins.game.World;
import me.rkfg.xmpp.bot.plugins.game.item.IItem;
import me.rkfg.xmpp.bot.plugins.game.item.ISlot;
import me.rkfg.xmpp.bot.plugins.game.misc.TypedAttribute;
import me.rkfg.xmpp.bot.plugins.game.misc.Utils;
import me.rkfg.xmpp.bot.plugins.game.repository.IObjectRepository;

public class SearchEvent extends AbstractEvent {

    public static final String TYPE = "searchevent";

    public SearchEvent() {
        super(TYPE);
    }

    @Override
    public void apply() {
        getTarget().as(PLAYER_OBJ).ifPresent(p -> {
            Optional<? extends IItem> result = tryRandomItem(p);
            if (!result.isPresent()) {
                p.log("Вы не смогли обнаружить ничего полезного.");
                return;
            }
            result.ifPresent(item -> {
                final Optional<ISlot> slot = item.getFittingSlot().flatMap(p::getSlot);
                Optional<IItem> slotItem = slot.flatMap(ISlot::getItem);
                final String slotName = unboxString(item.getFittingSlot().map(TypedAttribute::getAccusativeName));
                final String itemDesc = unboxString(item.getDescription());
                if (!slot.isPresent() || slotItem.isPresent()) {
                    p.enqueueEvent(new ItemPickupEvent(item));
                    p.log("Вы нашли %s: %s [№%d в рюкзаке]", slotName, itemDesc, p.getBackpack().size());
                } else {
                    p.enqueueEquipItem(item);
                    p.log("Вы нашли %s: %s", slotName, itemDesc);
                }
            });
        });
    }

    public Optional<? extends IItem> tryRandomItem(IPlayer p) {
        int search = p.getStat(ATK) + p.getStat(DEF) + p.getStat(STM) + p.getStat(LCK) + Utils.drn();
        int territory = 30 + Utils.drn();
        int diff = search - territory;
        if (diff < 1) {
            return Optional.empty();
        }
        int tier = 1;
        if (diff > 10 && diff < 16) {
            tier = 2;
        }
        if (diff > 15 && diff < 21) {
            tier = 3;
        }
        if (diff > 20) {
            tier = 4;
        }
        return getRandomItem(tier);
    }

    public static Optional<? extends IItem> getRandomItem(int tier) {
        Optional<? extends IItem> found = Optional.empty();
        while (tier > 0 && !found.isPresent()) {
            int type = Utils.dice("1d3");
            Optional<IObjectRepository<? extends IItem>> repo;
            switch (type) {
            case 1:
                repo = Optional.of(World.THIS.getWeaponRepository());
                break;
            case 2:
                repo = Optional.of(World.THIS.getArmorRepository());
                break;
            case 3:
                repo = Optional.of(World.THIS.getUsableRepository());
                break;
            default:
                return Optional.empty();
            }
            final int t = tier;
            found = repo.flatMap(r -> r.getRandomObjectByTier(t));
            tier--;
        }
        return found;
    }
}

@@ -1395,16 +1395,149 @@
 ription(
+Verbosity.WITH_PARAMS));%0A                final String itemDescVerbose = capitalize(unboxString(item.getDescription(Verbosity.VERBOSE)
 ));%0A    
@@ -1600,71 +1600,8 @@
 )) %7B
-%0A                    p.enqueueEvent(new ItemPickupEvent(item));
 %0A   
@@ -1655,16 +1655,20 @@
 %D1%80%D1%8E%D0%BA%D0%B7%D0%B0%D0%BA%D0%B5%5D
+. %25s
 %22, slotN
@@ -1708,35 +1708,31 @@
 ze()
-);%0A                %7D else %7B
+ + 1, itemDescVerbose);
 %0A   
@@ -1762,24 +1762,66 @@
 eueE
-quipItem
+vent(new ItemPickupEvent
 (item)
+)
 ;%0A
+                %7D else %7B%0A
     
@@ -1855,16 +1855,20 @@
 %D0%B0%D1%88%D0%BB%D0%B8 %25s:
+ %25s.
  %25s%22, sl
@@ -1883,16 +1883,79 @@
 itemDesc
+, itemDescVerbose);%0A                    p.enqueueEquipItem(item
 );%0A     

Add verbose description on item search