/**
 * Copyright 2014 Microsoft Open Technologies Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.microsoftopentechnologies.intellij.serviceexplorer.azure.storage;

import com.google.common.collect.ImmutableMap;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightVirtualFile;
import com.microsoftopentechnologies.intellij.helpers.UIHelper;
import com.microsoftopentechnologies.intellij.helpers.azure.AzureCmdException;
import com.microsoftopentechnologies.intellij.helpers.azure.sdk.AzureSDKManagerImpl;
import com.microsoftopentechnologies.intellij.helpers.storage.QueueExplorerFileEditorProvider;
import com.microsoftopentechnologies.intellij.model.storage.Queue;
import com.microsoftopentechnologies.intellij.model.storage.StorageAccount;
import com.microsoftopentechnologies.intellij.serviceexplorer.Node;
import com.microsoftopentechnologies.intellij.serviceexplorer.NodeActionEvent;
import com.microsoftopentechnologies.intellij.serviceexplorer.NodeActionListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Map;

public class QueueNode extends Node {
    private static final String QUEUE_MODULE_ID = QueueNode.class.getName();
    private static final String ICON_PATH = "container.png";
    private final Queue queue;
    private final StorageAccount storageAccount;

    public QueueNode(QueueModule parent, StorageAccount storageAccount, Queue queue) {
        super(QUEUE_MODULE_ID, queue.getName(), parent, ICON_PATH, true);

        this.storageAccount = storageAccount;
        this.queue = queue;
    }



    @Override
    protected void onNodeClick(NodeActionEvent ex) {
        if(getOpenedFile() == null) {


            LightVirtualFile queueVirtualFile = new LightVirtualFile(queue.getName() + " [Queue]");
            queueVirtualFile.putUserData(QueueExplorerFileEditorProvider.QUEUE_KEY, queue);
            queueVirtualFile.putUserData(QueueExplorerFileEditorProvider.STORAGE_KEY, storageAccount);

            queueVirtualFile.setFileType(new FileType() {
                @NotNull
                @Override
                public String getName() {
                    return "Queue";
                }

                @NotNull
                @Override
                public String getDescription() {
                    return "Queue";
                }

                @NotNull
                @Override
                public String getDefaultExtension() {
                    return "";
                }

                @Nullable
                @Override
                public Icon getIcon() {
                    return UIHelper.loadIcon("container.png");
                }

                @Override
                public boolean isBinary() {
                    return true;
                }

                @Override
                public boolean isReadOnly() {
                    return false;
                }

                @Override
                public String getCharset(@NotNull VirtualFile virtualFile, @NotNull byte[] bytes) {
                    return "UTF8";
                }
            });

            FileEditorManager.getInstance(getProject()).openFile(queueVirtualFile, true, true);
        }
    }

    @Override
    protected Map<String, Class<? extends NodeActionListener>> initActions() {
        return ImmutableMap.of(
                "View Queue", ViewQueue.class,
                "Delete", DeleteQueue.class,
                "Clear Queue", ClearQueue.class
        );
    }

    private VirtualFile getOpenedFile() {
        FileEditorManager fileEditorManager = FileEditorManager.getInstance(getProject());

        for (VirtualFile editedFile : fileEditorManager.getOpenFiles()) {
            Queue editedQueue = editedFile.getUserData(QueueExplorerFileEditorProvider.QUEUE_KEY);
            StorageAccount editedStorageAccount = editedFile.getUserData(QueueExplorerFileEditorProvider.STORAGE_KEY);

            if(editedStorageAccount != null
                    && editedQueue != null
                    && editedStorageAccount.getName().equals(storageAccount.getName())
                    && editedQueue.getName().equals(queue.getName())) {
                return editedFile;
            }
        }

        return null;
    }

    public class ViewQueue extends NodeActionListener {
        @Override
        public void actionPerformed(NodeActionEvent e) {
            onNodeClick(null);
        }
    }

    public class DeleteQueue extends NodeActionListener {

        @Override
        public void actionPerformed(final NodeActionEvent e) {
            int optionDialog = JOptionPane.showOptionDialog(null,
                    "Are you sure you want to delete the queue \"" + queue.getName() + "\"?",
                    "Service explorer",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    new String[]{"Yes", "No"},
                    null);

            if (optionDialog == JOptionPane.YES_OPTION) {

                VirtualFile openedFile = getOpenedFile();
                if(openedFile != null) {
                    FileEditorManager.getInstance(getProject()).closeFile(openedFile);
                }

                ProgressManager.getInstance().run(new Task.Backgroundable(getProject(), "Deleting queue...", false) {
                    @Override
                    public void run(@NotNull ProgressIndicator progressIndicator) {
                        try {
                            AzureSDKManagerImpl.getManager().deleteQueue(storageAccount, queue);

                            parent.removeAllChildNodes();
                            parent.load();
                        } catch (AzureCmdException ex) {
                            UIHelper.showException("Error deleting queue", ex, "Service explorer", false, true);
                        }
                    }
                });

            }
        }
    }

    public class ClearQueue extends NodeActionListener {

        @Override
        public void actionPerformed(final NodeActionEvent e) {
            int optionDialog = JOptionPane.showOptionDialog(null,
                    "Are you sure you want to clear the queue \"" + queue.getName() + "\"?",
                    "Service explorer",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    new String[]{"Yes", "No"},
                    null);

            if (optionDialog == JOptionPane.YES_OPTION) {

                ProgressManager.getInstance().run(new Task.Backgroundable(getProject(), "Clearing queue...", false) {
                    @Override
                    public void run(@NotNull ProgressIndicator progressIndicator) {
                        try {
                            AzureSDKManagerImpl.getManager().clearQueue(storageAccount, queue);

                            parent.removeAllChildNodes();
                            parent.load();
                        } catch (AzureCmdException ex) {
                            UIHelper.showException("Error clearing queue", ex, "Service explorer", false, true);
                        }
                    }
                });

            }
        }
    }

}

@@ -740,16 +740,76 @@
 bleMap;%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import c
@@ -1475,24 +1475,103 @@
 orProvider;%0A
+import com.microsoftopentechnologies.intellij.helpers.storage.QueueFileEditor;%0A
 import com.m
@@ -7954,80 +7954,952 @@
     
-parent.removeAllChildNodes();%0A                            parent.load();
+ApplicationManager.getApplication().runReadAction(new Runnable() %7B%0A                                @Override%0A                                public void run() %7B%0A                                    VirtualFile file = getOpenedFile();%0A                                    if(file != null) %7B%0A                                        final QueueFileEditor queueFileEditor = (QueueFileEditor) FileEditorManager.getInstance(getProject()).getEditors(file)%5B0%5D;%0A                                        ApplicationManager.getApplication().invokeLater(new Runnable() %7B%0A                                            @Override%0A                                            public void run() %7B%0A                                                queueFileEditor.fillGrid();%0A                                            %7D%0A                                        %7D);%0A                                    %7D%0A                                %7D%0A                            %7D);%0A
 %0A   

Fix issue #269