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 -> {
                getTarget().log(String.format("Вы нашли %s: %s", unboxString(item.getFittingSlot().map(TypedAttribute::getAccusativeName)),
                        unboxString(item.getDescription())));
                final Optional<ISlot> slot = item.getFittingSlot().flatMap(p::getSlot);
                Optional<IItem> slotItem = slot.flatMap(ISlot::getItem);
                if (!slot.isPresent() || slotItem.isPresent()) {
                    p.enqueueEvent(new ItemPickupEvent(item));
                } else {
                    p.enqueueEquipItem(item);
                }
            });
        });
    }

    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;
    }
}

@@ -1067,551 +1067,712 @@
     
-getTarget().log(String.format(%22%D0%92%D1%8B %D0%BD%D0%B0%D1%88%D0%BB%D0%B8 %25s: %25s%22, unboxString(item.getFittingSlot().map(TypedAttribute::getAccusativeName)),%0A                        unboxString(item.getDescription())));%0A                final Optional%3CISlot%3E slot = item.getFittingSlot().flatMap(p::getSlot);%0A                Optional%3CIItem%3E slotItem = slot.flatMap(ISlot::getItem);%0A                if (!slot.isPresent() %7C%7C slotItem.isPresent()) %7B%0A                    p.enqueueEvent(new ItemPickupEvent(item));%0A                %7D else %7B%0A                    p.enqueueEquipItem(item
+final Optional%3CISlot%3E slot = item.getFittingSlot().flatMap(p::getSlot);%0A                Optional%3CIItem%3E slotItem = slot.flatMap(ISlot::getItem);%0A                final String slotName = unboxString(item.getFittingSlot().map(TypedAttribute::getAccusativeName));%0A                final String itemDesc = unboxString(item.getDescription());%0A                if (!slot.isPresent() %7C%7C slotItem.isPresent()) %7B%0A                    p.enqueueEvent(new ItemPickupEvent(item));%0A                    p.log(%22%D0%92%D1%8B %D0%BD%D0%B0%D1%88%D0%BB%D0%B8 %25s: %25s %5B%E2%84%96%25d %D0%B2 %D1%80%D1%8E%D0%BA%D0%B7%D0%B0%D0%BA%D0%B5%5D%22, slotName, itemDesc, p.getBackpack().size());%0A                %7D else %7B%0A                    p.enqueueEquipItem(item);%0A                    p.log(%22%D0%92%D1%8B %D0%BD%D0%B0%D1%88%D0%BB%D0%B8 %25s: %25s%22, slotName, itemDesc
 );%0A 

Print found item number in backpack