package net.cobem.grizzly.communication.events.user;

import net.cobem.grizzly.communication.EventRequest;
import net.cobem.grizzly.communication.GameEvent;
import net.cobem.grizzly.communication.composers.user.SendBadgesComposer;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.badges.BadgeObject;

public class WearBadgeEvent implements GameEvent {
    @Override
    public void compose(Session Session, EventRequest Request) {
        //[UNKNOWN][1665] {0}{0}{0}{1}{0}{3}ADM{0}{0}{0}{2}{0}{0}{0}{0}{0}{3}{0}{0}{0}{0}{0}{4}{0}{0}{0}{0}{0}{5}{0}{0}

        boolean RemovingBadge = false;
        int RemovedSlot = 0;

        String Code = null;

        for (int i = 1; i < (Session.getHabbo().getEquippedBadges().size() + (5 - Session.getHabbo().getEquippedBadges().size())); i++) {
            int Slot = Request.readInt();
            Code = Request.readString();

            if (Code.length() >= 3) { //something is there..
               if (Session.getHabbo().getEquippedBadgeByCode(Code) == null) { // it's a new badge!
                   break;
               }
            } else { //Something new is coming or something has been removed..
                if (Session.getHabbo().getBadgeBySlot(Slot) != null) { //this badge has been removed
                    RemovingBadge = true;
                    RemovedSlot = Slot;
                    break;
                } else {
                    // ??
                }
            }

        }

        if (!RemovingBadge) {

            if (Session.getHabbo().getEquippedBadges().size() == 5) {
                return;
            }

            BadgeObject Badge = null;

            for (BadgeObject Obj : Session.getHabbo().getBadges()) {
                if (Obj.Code.equals(Code)) {
                    Badge = Obj;
                }
            }

            Badge.Slot = Session.getHabbo().getEquippedBadges().size() + 1;
            Badge.Equipped = true;

            Badge.append();
        } else {
            BadgeObject Badge = Session.getHabbo().getBadgeBySlot(RemovedSlot);

            Badge.Slot = 0;
            Badge.Equipped = false;

            Badge.append();
        }

        Session.sendResponse(SendBadgesComposer.compose(Session.getHabbo().getBadges(), Session.getHabbo().getEquippedBadges()));
    }
}
