package net.cobem.grizzly.habbohotel.users.items.trading;

import gnu.trove.map.hash.THashMap;
import net.cobem.grizzly.communication.EventResponse;
import net.cobem.grizzly.communication.HeaderLibrary;
import net.cobem.grizzly.communication.composers.inventory.DisposeItemFromInventoryComposer;
import net.cobem.grizzly.communication.composers.inventory.UpdateUserInventoryComposer;
import net.cobem.grizzly.habbohotel.sessions.Session;
import net.cobem.grizzly.habbohotel.users.items.InventoryItem;

public class TradeObject {
    private Session Partner;
    private Session Me;

    THashMap<Integer, InventoryItem> OfferedItems;

    private boolean Accepted;
    private boolean Confirmed;

    public TradeObject(Session Me, Session Partner) {
        this.Partner = Partner;
        this.Me = Me;

        this.OfferedItems = new THashMap<>();
    }

    public boolean addOffer(InventoryItem Item) {
        this.OfferedItems.put(Item.ID, Item);

        return true;
    }
    public boolean removeOffer(InventoryItem Item) {
        this.OfferedItems.remove(Item.ID);

        return true;
    }

    public THashMap<Integer, InventoryItem> getOffers() {
        return this.OfferedItems;
    }

    public boolean cancelTrade() {
        EventResponse msg = new EventResponse();

        msg.Initialize(HeaderLibrary.CloseTradeEvent);

        msg.addInt(Me.getHabbo().getID());
        msg.addInt(2);

        Me.sendResponse(msg);
        Partner.sendResponse(msg);

        Me.getHabbo().setTrade(null);
        Partner.getHabbo().setTrade(null);

        Me.getActor().removeStatus("trd", true);
        Partner.getActor().removeStatus("trd", true);
        return true;
    }

    public boolean completeTrade() {

        Confirmed = true;

        if ((Accepted && Partner.getHabbo().getTrade().accepted()) && (Partner.getHabbo().getTrade().confirmed())) {
            //finish it off!!

            for(InventoryItem item : this.OfferedItems.values()) {
                Me.getHabbo().getItems().remove(item.ID, true);

                Me.sendResponse(DisposeItemFromInventoryComposer.compose(item.ID));

                Partner.getHabbo().getItems().add(item.getBase(), true, null);
            }

            for (InventoryItem item : Partner.getHabbo().getTrade().getOffers().values()) {
                Partner.getHabbo().getItems().remove(item.ID, true);

                Partner.sendResponse(DisposeItemFromInventoryComposer.compose(item.ID));

                Me.getHabbo().getItems().add(item.getBase(), true, null);
            }

            Me.sendResponse(UpdateUserInventoryComposer.compose());
            Partner.sendResponse(UpdateUserInventoryComposer.compose());

            EventResponse msg = new EventResponse();

            msg.Initialize(HeaderLibrary.CloseCompletedTradeEvent);

            Me.sendResponse(msg);
            Partner.sendResponse(msg);

            Me.getHabbo().setTrade(null);
            Partner.getHabbo().setTrade(null);

            Me.getActor().removeStatus("trd", true);
            Partner.getActor().removeStatus("trd", true);
        }
        return true;
    }

    public boolean acceptTrade(boolean accept) {

        this.Accepted = accept;

        EventResponse msg = new EventResponse();

        msg.Initialize(HeaderLibrary.AcceptTradeEvent);

        msg.addInt(Me.getHabbo().getID());
        msg.addInt(accept ? 1 : 0);

        Me.sendResponse(msg);
        Partner.sendResponse(msg);

        if (accept && Partner.getHabbo().getTrade().accepted()) {
            msg.Initialize(HeaderLibrary.CompleteTradeEvent);

            Me.sendResponse(msg);
            Partner.sendResponse(msg);
        }
        return true;
    }

    public boolean updateUsers() {
        EventResponse msg = new EventResponse();

        msg.Initialize(HeaderLibrary.UpdateTradeEvent);

        msg.addInt(Me.getHabbo().getID());
        msg.addInt(OfferedItems.size());

        for(InventoryItem item : OfferedItems.values()) {
            msg.addInt(item.ID);
            msg.addString(item.getBase().Type.toLowerCase());
            msg.addInt(item.ID);
            msg.addInt(item.getBase().Sprite);
            msg.addInt(0);
            msg.addBool(true);
            msg.addInt(0);
            msg.addString("");
            msg.addInt(0);
            msg.addInt(0);
            msg.addInt(0);

            if (item.getBase().Type.equals("s")) {
                msg.addInt(0);
            }
        }

        msg.addInt(Partner.getHabbo().getID());
        msg.addInt(Partner.getHabbo().getTrade().getOffers().size());

        for(InventoryItem item : Partner.getHabbo().getTrade().getOffers().values()) {
            msg.addInt(item.ID);
            msg.addString(item.getBase().Type.toLowerCase());
            msg.addInt(item.ID);
            msg.addInt(item.getBase().Sprite);
            msg.addInt(0);
            msg.addBool(true);
            msg.addInt(0);
            msg.addString("");
            msg.addInt(0);
            msg.addInt(0);
            msg.addInt(0);

            if (item.getBase().Type.equals("s")) {
                msg.addInt(0);
            }
        }

        Me.sendResponse(msg);
        Partner.sendResponse(msg);
        return true;
    }

    public boolean accepted() {
        return Accepted;
    }

    public boolean confirmed() {
        return Confirmed;
    }
}
