package de.nedelosk.modularmachines.api.modules.models;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.common.base.Function;

import de.nedelosk.modularmachines.api.modules.IModelInitHandler;
import de.nedelosk.modularmachines.api.modules.items.IModuleContainer;
import de.nedelosk.modularmachines.api.modules.position.EnumStoragePositions;
import de.nedelosk.modularmachines.api.modules.position.IStoragePosition;
import de.nedelosk.modularmachines.api.modules.state.IModuleState;
import de.nedelosk.modularmachines.api.modules.storage.IStorage;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.vertex.VertexFormat;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.model.IModelState;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ModelHandlerCasing extends ModelHandler implements IModelHandler, IModelInitHandler {

	private final ResourceLocation casing;
	private final ResourceLocation casing_left;
	private final ResourceLocation casing_right;

	public ModelHandlerCasing(IModuleContainer container) {
		super("casings", container);
		casing = getModelLocation("casing");
		casing_left = getModelLocation("side_left");
		casing_right= getModelLocation("side_right");
	}

	@Override
	public void initModels(IModuleContainer container) {
		getModelOrDefault(casing);
		getModelOrDefault(casing_left);
		getModelOrDefault(casing_right);
	}

	@Override
	public void reload(IModuleState state, IStorage storage, IModelState modelState, VertexFormat format, Function bakedTextureGetter) {
		List<IBakedModel> models = new ArrayList<>();
		if(state.getModular() != null){
			Set<IStoragePosition> positions = state.getModular().getStorages().keySet();
			if(!positions.contains(EnumStoragePositions.LEFT)){
				models.add(getBakedModel(casing_left, modelState, format, bakedTextureGetter));
			}
			if(!positions.contains(EnumStoragePositions.RIGHT)){
				models.add(getBakedModel(casing_right, modelState, format, bakedTextureGetter));
			}
		}
		models.add(getBakedModel(casing, modelState, format, bakedTextureGetter));
		bakedModel = new BakedMultiModel(models);
	}
}

@@ -212,33 +212,153 @@
 modules.
-IModelInitHandler
+EnumModuleSizes;%0Aimport de.nedelosk.modularmachines.api.modules.IModelInitHandler;%0Aimport de.nedelosk.modularmachines.api.modules.IModule
 ;%0Aimport
@@ -694,15 +694,166 @@
 s.st
-orage.I
+ate.IModuleStateClient;%0Aimport de.nedelosk.modularmachines.api.modules.storage.IStorage;%0Aimport de.nedelosk.modularmachines.api.modules.storage.module.IModule
 Stor
@@ -2498,16 +2498,298 @@
 tter));%0A
+%09%09for(IModuleState moduleState : ((IModuleStorage)storage).getModules())%7B%0A%09%09%09IModule module = moduleState.getModule();%0A%09%09%09if(((IModuleStateClient)moduleState).getModelHandler() != null)%7B%0A%09%09%09%09models.add(ModuleModelHelper.getModel(moduleState, storage, modelState, format));%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09bakedM

Fix casing modules