package com.interpress_project.modernshare.client.controller.actions;

import com.interpress_project.modernshare.client.controller.command.ISVNCommand;
import com.interpress_project.modernshare.client.controller.command.SVNCommandFactory;
import com.interpress_project.modernshare.client.controller.command.SVNCommandManager;
import com.interpress_project.modernshare.client.controller.command.TaskManager;
import com.interpress_project.modernshare.client.controller.delegate.RemoteSVNManager;
import com.interpress_project.modernshare.client.controller.delegate.exceptions.ConnectionFailException;
import com.interpress_project.modernshare.client.controller.delegate.exceptions.InternalFailException;
import com.interpress_project.modernshare.client.controller.model.ISVNFileItem;
import com.interpress_project.modernshare.client.events.GUIEvent;
import com.interpress_project.modernshare.client.events.GUIEventType;
import com.interpress_project.modernshare.client.events.LogEvent;
import com.interpress_project.modernshare.client.events.LogEventType;
import com.interpress_project.modernshare.client.view.ViewManager;

/**
 *@stereotype action
 */
public final class RemoteDeleteAction extends ActionAdaptor {
	private final ViewManager vmgr;
	private final RemoteSVNManager svnmgr = RemoteSVNManager.getInstance();
	private final SVNCommandManager cmdmgr = SVNCommandManager.getInstance();
	private final SVNCommandFactory factory = SVNCommandFactory.getInstance();
	private ISVNFileItem[] items = null;

	/**
	 * RegisterTargetAction
	 * @param vmgr
	 */
	public RemoteDeleteAction(ViewManager vmgr, ISVNFileItem[] items) {
		setText("削除(&D)");
		setToolTipText("データセンタからユーザ・リソースを削除します。");
		try {
			setImageDescriptor(ImageFactory.getInstance().getImageDescriptor("delete"));
		}
		catch (Exception ex) {
			sb.getLogger().fatal("リソース設定のエラーが発生しました。", ex);
			System.exit(-1);
		}
		this.vmgr = vmgr;
		this.items = items;
	}

	/**
	 * run
	 */
	public void run() {
		TaskManager taskmgr = TaskManager.getInstance();
		try {
			taskmgr.stop();

			boolean bResult = vmgr.getDialogManager().createYesNoMessageDialog("データセンタのリソース削除確認",
			  "データセンタに格納されたリソースを削除します。削除後、ホットショット・フォルダをDataCenter -> PCモードで更新してください。 ");
			if (bResult == false) {
				return;
			}

			guiEventSource.fireEvent(new GUIEvent(this, "リモート・リソースを削除しています...", GUIEventType.START));

			try {
				svnmgr.connect();
			}
			catch (ConnectionFailException ex) {
				logEventSource.fireEvent(new LogEvent(this, ex.getMessage(), LogEventType.ERROR, ex));
				vmgr.getDialogManager().createErrorMessageDialog(ex.getMessage());
				return;
			}

			try {
				svnmgr.deleteRemoteItem(items);
			}
			catch (InternalFailException ex) {
				logEventSource.fireEvent(new LogEvent(this, ex.getMessage(), LogEventType.ERROR, ex));
				vmgr.getDialogManager().createErrorMessageDialog(ex.getMessage());
				return;
			}

			/**
			 * Refresh remote view.
			 */
			ISVNCommand command = factory.createRemoteRefreshCommand();
			cmdmgr.addCommand(command);
		}
		finally {
			guiEventSource.fireEvent(new GUIEvent(this, GUIEventType.FINISH));
			taskmgr.start();
		}
	}
}
