/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * 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.
 */
package org.spongepowered.common.item.inventory.util;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.crafting.CraftingGridInventory;
import org.spongepowered.common.item.inventory.adapter.impl.comp.CraftingGridInventoryAdapter;
import org.spongepowered.common.item.inventory.adapter.impl.comp.CraftingInventoryAdapter;
import org.spongepowered.common.item.inventory.lens.Fabric;
import org.spongepowered.common.item.inventory.lens.comp.CraftingGridInventoryLens;
import org.spongepowered.common.item.inventory.lens.impl.comp.CraftingGridInventoryLensImpl;
import org.spongepowered.common.item.inventory.lens.impl.fabric.IInventoryFabric;
import org.spongepowered.common.item.inventory.lens.impl.slots.SlotLensImpl;

import java.util.Iterator;
import java.util.Optional;

public final class InventoryUtil {

    private InventoryUtil() {}

    public static CraftingGridInventory toSpongeInventory(InventoryCrafting inv) {
        IInventoryFabric fabric = new IInventoryFabric(inv);
        CraftingGridInventoryLens lens = new CraftingGridInventoryLensImpl(0, inv.getWidth(), inv.getHeight(), inv.getWidth(), SlotLensImpl::new);

        return new CraftingGridInventoryAdapter(fabric, lens);
    }

    public static InventoryCrafting toNativeInventory(CraftingGridInventory inv) {
        Fabric<IInventory> fabric = ((CraftingInventoryAdapter) inv).getFabric();
        Iterator<IInventory> inventories = fabric.allInventories().iterator();
        InventoryCrafting inventoryCrafting = (InventoryCrafting) inventories.next();

        if (inventories.hasNext()) {
            throw new IllegalStateException("Another inventory found: " + inventories.next());
        }

        return inventoryCrafting;
    }

    public static Optional<Inventory> getDoubleChestInventory(TileEntityChest chest) {
        // BlockChest#getContainer(World, BlockPos, boolean) without isBlocked() check
        for (EnumFacing enumfacing : EnumFacing.Plane.HORIZONTAL) {
            BlockPos blockpos = chest.getPos().offset(enumfacing);

            TileEntity tileentity1 = chest.getWorld().getTileEntity(blockpos);

            if (tileentity1 instanceof TileEntityChest && tileentity1.getBlockType() == chest.getBlockType()) {

                InventoryLargeChest inventory;

                if (enumfacing != EnumFacing.WEST && enumfacing != EnumFacing.NORTH) {
                    inventory = new InventoryLargeChest("container.chestDouble", chest, (TileEntityChest) tileentity1);
                } else {
                    inventory = new InventoryLargeChest("container.chestDouble", (TileEntityChest) tileentity1, chest);
                }

                return Optional.of((Inventory) inventory);
            }
        }
        return Optional.empty();
    }

}

@@ -2933,32 +2933,36 @@
 ric = ((Crafting
+Grid
 InventoryAdapter
@@ -2993,17 +2993,13 @@
     
-Iterator%3C
+for (
 IInv
@@ -3004,17 +3004,16 @@
 nventory
-%3E
  invento
@@ -3017,13 +3017,11 @@
 ntor
-ies =
+y :
  fab
@@ -3044,67 +3044,94 @@
 es()
-.iterator();%0A        InventoryCrafting inventoryCrafting = 
+) %7B%0A            if (inventory instanceof InventoryCrafting) %7B%0A                return (
 (Inv
@@ -3158,29 +3158,20 @@
 ntor
-ies.next(
+y
 );%0A
-%0A
         
 if (
@@ -3170,41 +3170,24 @@
     
-if (inventories.hasNext()) %7B%0A    
+    %7D%0A        %7D%0A
     
@@ -3227,85 +3227,55 @@
 on(%22
-Another inventory found: %22 + inventories.next());%0A        %7D%0A%0A        return i
+Invalid CraftingGridInventory. Could not find I
 nven
@@ -3286,16 +3286,19 @@
 Crafting
+.%22)
 ;%0A    %7D%0A

fix CraftingGridInventory to native inventory fixes #1716