package electrosphere.client.ui.components;

import electrosphere.audio.VirtualAudioSourceManager.VirtualAudioSourceType;
import electrosphere.client.ui.menu.WindowStrings;
import electrosphere.client.ui.menu.WindowUtils;
import electrosphere.controls.ControlHandler.ControlsState;
import electrosphere.engine.Globals;
import electrosphere.engine.assetmanager.AssetDataStrings;
import electrosphere.engine.signal.Signal.SignalType;
import electrosphere.entity.Entity;
import electrosphere.entity.state.inventory.InventoryUtils;
import electrosphere.renderer.ui.elements.Window;
import electrosphere.renderer.ui.elementtypes.ContainerElement.YogaAlignment;
import electrosphere.renderer.ui.elementtypes.ContainerElement.YogaJustification;
import electrosphere.renderer.ui.elementtypes.NavigableElement.NavigationEventCallback;
import electrosphere.renderer.ui.events.NavigationEvent;

/**
 * The player's inventory
 */
public class PlayerInventoryWindow {
    
    /**
     * Minimum width of the component
     */
    public static final int MIN_WIDTH = 500;

    /**
     * Minimum height of the component
     */
    public static final int MIN_HEIGHT = 500;

    /**
     * Creates a character customizer panel
     * @param race The race of the character
     * @return The panel component
     */
    public static Window createPlayerInventoryWindow(Entity entity){
        int width = 750;
        int height = 750;


        Window rVal = Window.createExpandableCenterAligned(Globals.renderingEngine.getOpenGLState(), width, height);
        rVal.setParentAlignItem(YogaAlignment.Center);
        rVal.setParentJustifyContent(YogaJustification.Center);

        rVal.setOnNavigationCallback(new NavigationEventCallback() {public boolean execute(NavigationEvent event){
            WindowUtils.recursiveSetVisible(Globals.elementService.getWindow(WindowStrings.WINDOW_CHARACTER), false);
            Globals.elementService.unregisterWindow(WindowStrings.WINDOW_CHARACTER);
            if(Globals.cameraHandler.getTrackPlayerEntity()){
                Globals.controlHandler.hintUpdateControlState(ControlsState.MAIN_GAME);
            } else {
                Globals.controlHandler.hintUpdateControlState(ControlsState.IN_GAME_FREE_CAMERA);
            }
            if(Globals.virtualAudioSourceManager != null){
                Globals.virtualAudioSourceManager.createVirtualAudioSource(AssetDataStrings.UI_SFX_INVENTORY_CLOSE, VirtualAudioSourceType.UI, false);
            }
            Globals.renderingEngine.getPostProcessingPipeline().setApplyBlur(false);
            return false;
        }});


        //
        //contents
        //

        if(InventoryUtils.hasEquipInventory(entity)){
            rVal.addChild(EquipmentInventoryPanel.createEquipmentInventoryPanel(entity));
        }

        if(InventoryUtils.hasNaturalInventory(entity)){
            rVal.addChild(NaturalInventoryPanel.createNaturalInventoryPanel(entity));
        }

        if(InventoryUtils.hasToolbarInventory(entity)){
            rVal.addChild(ToolbarInventoryPanel.createToolbarInventoryPanel(entity));
        }

        //
        //Final setup
        //

        Globals.signalSystem.post(SignalType.YOGA_APPLY, rVal);

        

        return rVal;
    }

}
