package org.helioviewer.jhv.gui.states;

import org.helioviewer.base.logging.Log;
import org.helioviewer.gl3d.gui.GL3DCameraMouseController;
import org.helioviewer.gl3d.gui.GL3DCameraSelectorModel;
import org.helioviewer.gl3d.gui.GL3DTopToolBar;
import org.helioviewer.gl3d.model.GL3DInternalPluginConfiguration;
import org.helioviewer.gl3d.plugin.GL3DPluginController;
import org.helioviewer.jhv.display.Displayer;
import org.helioviewer.jhv.gui.ViewchainFactory;
import org.helioviewer.jhv.gui.components.TopToolBar;
import org.helioviewer.jhv.gui.components.statusplugins.RenderModeStatusPanel;
import org.helioviewer.jhv.gui.controller.MainImagePanelMousePanController;
import org.helioviewer.jhv.gui.interfaces.ImagePanelInputController;
import org.helioviewer.viewmodel.view.ComponentView;
import org.helioviewer.viewmodel.view.opengl.GL3DSceneGraphView;

public class GuiState implements State {

    private final boolean is3d;
    private final ViewchainFactory viewchainFactory;

    private static TopToolBar topToolBar;
    private ComponentView mainComponentView;
    private RenderModeStatusPanel renderModeStatus;

    public GuiState(ViewchainFactory viewchainFactory, boolean is3d) {
        this.viewchainFactory = viewchainFactory;
        this.is3d = is3d;
    }

    @Override
    public void activate() {
        if (is3d) {
            GL3DCameraSelectorModel.getInstance().activate(this.mainComponentView.getAdapter(GL3DSceneGraphView.class));
            GL3DPluginController.getInstance().setPluginConfiguration(new GL3DInternalPluginConfiguration());
            GL3DPluginController.getInstance().loadPlugins();
        }
    }

    @Override
    public void deactivate() {
        getMainComponentView().deactivate();
    }

    @Override
    public boolean createViewChains() {
        Log.info("Start creating view chains");

        boolean firstTime = (mainComponentView == null);
        // Create main view chain
        ViewchainFactory mainFactory = this.viewchainFactory;
        mainComponentView = mainFactory.createViewchainMain(mainComponentView, false);

        return firstTime;
    }

    @Override
    public boolean recreateViewChains(State previousState) {
        Displayer.getSingletonInstance().removeListeners();

        if (previousState == null || previousState.getMainComponentView() == null) {
            return this.createViewChains();
        } else {
            mainComponentView = previousState.getMainComponentView();
            return false;
        }
    }

    @Override
    public ViewStateEnum getType() {
        if (is3d) {
            return ViewStateEnum.View3D;
        } else {
            return ViewStateEnum.View2D;
        }
    }

    @Override
    public TopToolBar getTopToolBar() {
        if (topToolBar == null) {
            if (is3d) {
                topToolBar = new GL3DTopToolBar();
            } else {
                topToolBar = new TopToolBar();
            }
        }

        return topToolBar;
    }

    @Override
    public ComponentView getMainComponentView() {
        return mainComponentView;
    }

    public RenderModeStatusPanel getRenderModeStatus() {
        return renderModeStatus;
    }

    @Override
    public ViewchainFactory getViewchainFactory() {
        return this.viewchainFactory;
    }

    @Override
    public ImagePanelInputController getDefaultInputController() {
        if (is3d) {
            return new GL3DCameraMouseController();
        } else {
            return new MainImagePanelMousePanController();
        }
    }

}

@@ -1324,337 +1324,14 @@
 ) %7B%0A
-        if (is3d) %7B%0A            GL3DCameraSelectorModel.getInstance().activate(this.mainComponentView.getAdapter(GL3DSceneGraphView.class));%0A            GL3DPluginController.getInstance().setPluginConfiguration(new GL3DInternalPluginConfiguration());%0A            GL3DPluginController.getInstance().loadPlugins();%0A        %7D%0A
     %7D%0A
+
 %0A   
@@ -1376,53 +1376,8 @@
 ) %7B%0A
-        getMainComponentView().deactivate();%0A
     
@@ -1722,16 +1722,296 @@
 false);%0A
+        GL3DCameraSelectorModel.getInstance().activate(this.mainComponentView.getAdapter(GL3DSceneGraphView.class));%0A        GL3DPluginController.getInstance().setPluginConfiguration(new GL3DInternalPluginConfiguration());%0A        GL3DPluginController.getInstance().loadPlugins();
 %0A       

move activate/deactivate