/*
 * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author GeyserMC
 * @link https://github.com/GeyserMC/Geyser
 */

package org.geysermc.geyser.inventory;

import lombok.Getter;
import lombok.Setter;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.entity.type.player.PlayerEntity;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.Hand;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponentTypes;
import org.jetbrains.annotations.Range;

import java.util.List;
import java.util.Map;

@Getter
public class PlayerInventory extends Inventory {
    /**
     * Stores the held item slot, starting at index 0.
     * Add 36 in order to get the network item slot.
     */
    @Setter
    private int heldItemSlot;

    @NonNull
    private GeyserItemStack cursor = GeyserItemStack.EMPTY;

    public PlayerInventory(GeyserSession session) {
        super(session, 0, 46, null);
        heldItemSlot = 0;
    }

    @Override
    public int getOffsetForHotbar(@Range(from = 0, to = 8) int slot) {
        return slot + 36;
    }

    public void setCursor(@NonNull GeyserItemStack newCursor, GeyserSession session) {
        updateItemNetId(cursor, newCursor, session);
        cursor = newCursor;
    }

    /**
     * Checks if the player is holding the specified item in either hand
     *
     * @param item The item to look for
     * @return If the player is holding the item in either hand
     */
    public boolean isHolding(@NonNull Item item) {
        return getItemInHand().is(item) || getOffhand().is(item);
    }

    public GeyserItemStack getItemInHand(@NonNull Hand hand) {
        return hand == Hand.OFF_HAND ? getOffhand() : getItemInHand();
    }

    public GeyserItemStack getItemInHand() {
        if (36 + heldItemSlot > this.size) {
            GeyserImpl.getInstance().getLogger().debug("Held item slot was larger than expected!");
            return GeyserItemStack.EMPTY;
        }
        return items[36 + heldItemSlot];
    }

    // TODO other equipment slots
    public Map<EquipmentSlot, GeyserItemStack> getEquipment() {
        return Map.of(
            EquipmentSlot.MAIN_HAND, getItemInHand(),
            EquipmentSlot.OFF_HAND, items[45],
            EquipmentSlot.BOOTS, items[8],
            EquipmentSlot.LEGGINGS, items[7],
            EquipmentSlot.CHESTPLATE, items[6],
            EquipmentSlot.HELMET, items[5]
        );
    }

    public void setItemInHand(@NonNull GeyserItemStack item) {
        if (36 + heldItemSlot > this.size) {
            GeyserImpl.getInstance().getLogger().debug("Held item slot was larger than expected!");
            return;
        }
        items[36 + heldItemSlot] = item;
    }

    @Override
    public void setItem(int slot, @NonNull GeyserItemStack newItem, GeyserSession session) {
        final PlayerEntity entity = session.getPlayerEntity();
        if (slot == getOffsetForHotbar(heldItemSlot)  && entity.getFlag(EntityFlag.USING_ITEM) && newItem.is(Items.CROSSBOW)) {
            List<ItemStack> chargedProjectiles = newItem.getComponent(DataComponentTypes.CHARGED_PROJECTILES);

            // On Java, when you finished charging a projectile into (a crossbow), the client can keep holding down
            // right click and keep using it, however and Bedrock if you do that and allow the client to keep using the item
            // it will loop the crossbow state, so we'll have to forcefully stop them from using the crossbow, and tell
            // the Java server to stop using the item, because Bedrock will never send RELEASE_ITEM (even if they stop holding down right click)
            // causing a de-sync, so we'll have to do it ourselves.

            // This is checked here so we'll only do this when the server charged the crossbow.
            if (chargedProjectiles != null && !chargedProjectiles.isEmpty()) {
                entity.setFlag(EntityFlag.USING_ITEM, false);
                entity.updateBedrockMetadata();

                session.releaseItem();
                session.setLastChargedProjectilesTime(System.currentTimeMillis());
            }
        }

        super.setItem(slot, newItem, session);
    }

    @Override
    public boolean shouldConfirmContainerClose() {
        return false;
    }

    public GeyserItemStack getOffhand() {
        return items[45];
    }
}
