/*
 * Hyperbox - Virtual Infrastructure Manager
 * Copyright (C) 2013 Maxime Dor
 *
 * http://kamax.io/hbox/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package io.kamax.hboxc.gui.snapshot;

import io.kamax.hbox.comm.Command;
import io.kamax.hbox.comm.HypervisorTasks;
import io.kamax.hbox.comm.Request;
import io.kamax.hbox.comm.in.MachineIn;
import io.kamax.hbox.comm.in.SnapshotIn;
import io.kamax.hbox.comm.out.hypervisor.MachineOut;
import io.kamax.hbox.comm.out.hypervisor.SnapshotOut;
import io.kamax.hboxc.gui.Gui;
import io.kamax.hboxc.gui._Cancelable;
import io.kamax.hboxc.gui._Saveable;
import io.kamax.hboxc.gui.action.CancelAction;
import io.kamax.hboxc.gui.action.LoadingAction;
import io.kamax.hboxc.gui.action.SaveAction;
import io.kamax.hboxc.gui.worker.receiver._SnapshotGetReceiver;
import io.kamax.hboxc.gui.workers.SnapshotGetWorker;
import javax.swing.Action;

public class SnapshotModifyDialog implements _Saveable, _Cancelable, _SnapshotGetReceiver {

    private Action saveAction = new SaveAction(this);
    private SnapshotEditorDialog editor;
    private MachineOut mOut;
    private SnapshotIn snapIn;
    private SnapshotOut snapOut;

    public SnapshotModifyDialog(MachineOut mOut, SnapshotOut snapOut) {
        this.mOut = mOut;
        this.snapOut = snapOut;

        editor = new SnapshotEditorDialog(new SaveAction(this), new CancelAction(this));
    }

    public static void show(MachineOut mOut, SnapshotOut snapOut) {
        SnapshotModifyDialog instance = new SnapshotModifyDialog(mOut, snapOut);
        SnapshotGetWorker.execute(instance, mOut.getServerId(), mOut.getUuid(), snapOut.getUuid());
        instance.editor.setDialogTitle("Edit Snapshot");
        instance.editor.getDialog().pack();
        instance.editor.getDialog().setLocationRelativeTo(instance.editor.getDialog().getParent());
        instance.editor.getDialog().setVisible(true);
    }

    private void hide() {
        editor.getDialog().setVisible(false);
        editor.getDialog().dispose();
    }

    @Override
    public void save() {
        snapIn = new SnapshotIn(snapOut.getUuid());
        if (!editor.getName().contentEquals(snapOut.getName())) {
            snapIn.setName(editor.getName());
        }
        if (!editor.getDescription().contentEquals(snapOut.getDescription())) {
            snapIn.setDescription(editor.getDescription());
        }

        if (snapIn.hasNewData()) {
            Gui.post(new Request(Command.VBOX, HypervisorTasks.SnapshotModify, new MachineIn(mOut), snapIn));
        }

        hide();
    }

    @Override
    public void cancel() {
        hide();
    }

    @Override
    public void loadingStarted() {
        editor.getSaveButton().setAction(LoadingAction.get());
    }

    @Override
    public void loadingFinished(boolean isSuccessful, Throwable t) {
        editor.getSaveButton().setAction(saveAction);
        editor.getSaveButton().setEnabled(isSuccessful);
    }

    @Override
    public void put(String srvId, String vmId, SnapshotOut snapOut) {
        // TODO Auto-generated method stub
    }

}

@@ -1097,39 +1097,8 @@
 ut;%0A
-import io.kamax.hboxc.gui.Gui;%0A
 impo
@@ -1301,32 +1301,96 @@
 ion.SaveAction;%0A
+import io.kamax.hboxc.gui.worker.receiver.AnswerWorkerReceiver;%0A
 import io.kamax.
@@ -1429,32 +1429,81 @@
 hotGetReceiver;%0A
+import io.kamax.hboxc.gui.workers.MessageWorker;%0A
 import io.kamax.
@@ -1535,24 +1535,56 @@
 tGetWorker;%0A
+import io.kamax.tool.AxStrings;%0A
 import javax
@@ -2006,24 +2006,67 @@
 t = snapOut;
+%0A        saveAction = new SaveAction(this);
 %0A%0A        ed
@@ -2097,21 +2097,17 @@
 rDialog(
-new S
+s
 aveActio
@@ -2107,22 +2107,16 @@
 veAction
-(this)
 , new Ca
@@ -2877,39 +2877,50 @@
 f (!
-editor.getName().contentEquals(
+AxStrings.contentEquals(editor.getName(), 
 snap
@@ -3006,16 +3006,40 @@
    if (!
+AxStrings.contentEquals(
 editor.g
@@ -3053,31 +3053,18 @@
 iption()
-.contentEquals(
+, 
 snapOut.
@@ -3206,16 +3206,29 @@
     
-Gui.post
+MessageWorker.execute
 (new
@@ -3310,16 +3310,184 @@
  snapIn)
+, new AnswerWorkerReceiver() %7B%0A%0A                @Override%0A                public void start() %7B%0A                    editor.getSaveButton().setAction(LoadingAction.get()
 );%0A     
@@ -3493,26 +3493,352 @@
     
+ 
-%7D%0A%0A        hide();
+       %7D%0A%0A                @Override%0A                public void success() %7B%0A                    hide();%0A                %7D%0A%0A                @Override%0A                public void fail(Throwable t) %7B%0A                    editor.getSaveButton().setAction(saveAction);%0A                    super.fail(t);%0A                %7D%0A            %7D);%0A        %7D%0A%0A
 %0A   
@@ -4321,42 +4321,202 @@
     
-// TODO Auto-generated method stub
+this.snapOut = snapOut;%0A        editor.setName(snapOut.getName());%0A        editor.setDescription(snapOut.getDescription());%0A        editor.setDialogTitle(%22Snapshot Edit - %22 + snapOut.getName());
 %0A   

Fix snapshot edit dialog