/*******************************************************************************
 * Copyright (c) 2012-2016 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.ide.imageviewer;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.dialogs.CancelCallback;
import org.eclipse.che.ide.api.dialogs.ConfirmCallback;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
import org.eclipse.che.ide.api.editor.AbstractEditorPresenter;
import org.eclipse.che.ide.api.editor.EditorAgent.OpenEditorCallback;
import org.eclipse.che.ide.api.editor.EditorInput;
import org.eclipse.che.ide.api.event.FileEvent;
import org.eclipse.che.ide.api.event.FileEventHandler;
import org.eclipse.che.ide.api.parts.WorkspaceAgent;
import org.vectomatic.dom.svg.ui.SVGResource;

import javax.validation.constraints.NotNull;

/**
 * Is used for displaying images in editor area.
 *
 * @author Ann Shumilova
 */
public class ImageViewer extends AbstractEditorPresenter implements FileEventHandler {

    private ImageViewerResources     resources;
    private CoreLocalizationConstant constant;
    private DialogFactory            dialogFactory;
    private WorkspaceAgent           workspaceAgent;
    private ScrollPanel              editorView;

    @Inject
    public ImageViewer(ImageViewerResources resources,
                       CoreLocalizationConstant constant,
                       DialogFactory dialogFactory,
                       EventBus eventBus,
                       WorkspaceAgent workspaceAgent) {
        this.resources = resources;
        this.constant = constant;
        this.dialogFactory = dialogFactory;
        this.workspaceAgent = workspaceAgent;

        resources.imageViewerCss().ensureInjected();

        eventBus.addHandler(FileEvent.TYPE, this);
    }

    /** {@inheritDoc} */
    @Override
    public void doSave() {
    }

    @Override
    public void doSave(AsyncCallback<EditorInput> callback) {
    }

    /** {@inheritDoc} */
    @Override
    public void doSaveAs() {
    }

    /** {@inheritDoc} */
    @Override
    public void activate() {
    }

    /** {@inheritDoc} */
    @NotNull
    @Override
    public String getTitle() {
        return input.getName();
    }

    /** {@inheritDoc} */
    @Override
    public SVGResource getTitleImage() {
        return input.getSVGResource();
    }

    /** {@inheritDoc} */
    @Override
    public String getTitleToolTip() {
        return null;
    }

    /** {@inheritDoc} */
    @Override
    public void onClose(@NotNull final AsyncCallback<Void> callback) {
        if (isDirty()) {
            dialogFactory.createConfirmDialog(
                    constant.askWindowCloseTitle(),
                    constant.messagesSaveChanges(getEditorInput().getName()),
                    new ConfirmCallback() {
                        @Override
                        public void accepted() {
                            doSave();
                            handleClose();
                            callback.onSuccess(null);
                        }
                    },
                    new CancelCallback() {
                        @Override
                        public void cancelled() {
                            handleClose();
                            callback.onSuccess(null);
                        }
                    }).show();
        } else {
            handleClose();
            callback.onSuccess(null);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void go(AcceptsOneWidget container) {
        VerticalPanel panel = new VerticalPanel();
        panel.setSize("100%", "100%");
        panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
        panel.add(getImage());
        editorView = new ScrollPanel(panel);
        editorView.getElement().getFirstChildElement().getStyle().setHeight(100, Unit.PCT);
        container.setWidget(editorView);
    }

    /**
     * Image to display file with image type.
     *
     * @return {@link Image}
     */
    private Image getImage() {
        String contentLink = input.getFile().getContentUrl();
        Image image = (contentLink != null) ? new Image(contentLink) : new Image();
        image.setStyleName(resources.imageViewerCss().imageViewer());
        return image;
    }

    /** {@inheritDoc} */
    @Override
    protected void initializeEditor(OpenEditorCallback callback) {
    }

    /** {@inheritDoc} */
    @Override
    public void close(final boolean save) {
        // nothing to do
    }

    /** {@inheritDoc} */
    @Override
    public void setVisible(boolean visible) {
        editorView.setVisible(visible);
    }

    /** {@inheritDoc} */
    @Override
    public IsWidget getView() {
        return editorView;
    }

    /** {@inheritDoc} */
    @Override
    public void onFileOperation(FileEvent event) {
        if (event.getOperationType() != FileEvent.FileOperation.CLOSE) {
            return;
        }

        final String eventFilePath = event.getFile().getPath();
        final String filePath = input.getFile().getPath();
        if (filePath.equals(eventFilePath)) {
            workspaceAgent.removePart(this);
        }
    }
}

@@ -1611,24 +1611,83 @@
 entHandler;%0A
+import org.eclipse.che.ide.api.machine.WsAgentURLModifier;%0A
 import org.e
@@ -1723,24 +1723,24 @@
 spaceAgent;%0A
-
 import org.v
@@ -2189,24 +2189,74 @@
 spaceAgent;%0A
+    private WsAgentURLModifier       urlModifier;%0A
     private 
@@ -2564,16 +2564,71 @@
 aceAgent
+,%0A                       WsAgentURLModifier urlModifier
 ) %7B%0A    
@@ -2733,24 +2733,24 @@
 logFactory;%0A
-
         this
@@ -2782,16 +2782,56 @@
 ceAgent;
+%0A        this.urlModifier = urlModifier;
 %0A%0A      
@@ -5296,16 +5296,35 @@
 tLink = 
+urlModifier.modify(
 input.ge
@@ -5346,16 +5346,17 @@
 entUrl()
+)
 ;%0A      

Provide correct content url link for the image