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

import java.io.File;
import java.util.ArrayList;

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.command.exceptions.CommandException;
import com.interpress_project.modernshare.client.controller.delegate.SVNFolderUtil;
import com.interpress_project.modernshare.client.controller.delegate.exceptions.DeleteFailException;
import com.interpress_project.modernshare.client.controller.model.LocalModelManager;
import com.interpress_project.modernshare.client.controller.model.RootStatus;
import com.interpress_project.modernshare.client.events.LocalViewEvent;
import com.interpress_project.modernshare.client.events.LogEvent;
import com.interpress_project.modernshare.client.events.LogEventType;
import com.interpress_project.modernshare.client.events.ViewEventType;
import com.interpress_project.modernshare.client.view.ViewManager;
import com.interpress_project.modernshare.ipcommon.SystemBase;

/**
 *@stereotype action
 */
public final class DisableTargetAction extends ActionAdaptor {
	private SystemBase sb = SystemBase.getInstance();
	private ViewManager vmgr;
	private String path;

	/**
	 * DislableTargetAction
	 * @param vmgr
	 * @param path
	 */
	public DisableTargetAction(ViewManager vmgr, String path) {
		setText("ホットショットフォルダの登録解除(&U)");
		setToolTipText("ホットショットフォルダの登録を解除します。");
		try {
			setImageDescriptor(ImageFactory.getInstance().getImageDescriptor("unconnected"));
		}
		catch (Exception ex) {
			sb.getLogger().fatal("リソース設定のエラーが発生しました。", ex);
			System.exit(-1);
		}
		this.vmgr = vmgr;
		this.path = path;
	}

	/**
	 * run
	 */
	public void run() {
		sb.getLogger().debug("Enter DisableTargetAction()");

		TaskManager taskmgr = TaskManager.getInstance();
		try {
			taskmgr.stop();

			boolean bAnswer = vmgr.getDialogManager().createYesNoMessageDialog("ホットショットフォルダの登録解除",
			  "ホットショットフォルダ" + path + "の登録を解除します。(データセンタ側のデータは削除されません。)");
			if (bAnswer == false) {
				return;
			}
			/**
			 * Make disable the target.
			 */
			SVNCommandManager cmdmgr = SVNCommandManager.getInstance();
			SVNCommandFactory factory = SVNCommandFactory.getInstance();
			ISVNCommand command = factory.createActiveTargetCommand(path, RootStatus.deactive);
			try {
				cmdmgr.execCommand(command);
			}
			catch (CommandException e) {
			}
			logEventSource.fireEvent(new LogEvent(this, "ホットショットフォルダ" + path + "から管理情報を削除します。お待ちください...", LogEventType.INFO));

			/**
			 * Remove the files...
			 */
			if (new File(path).exists()) {
				SVNFolderUtil futil = SVNFolderUtil.getInstance();
				ArrayList<File> list = futil.getSVNDirListInTarget(new File(path));
				try {
					futil.deleteFiles(list);
				}
				catch (DeleteFailException ex) {
					logEventSource.fireEvent(new LogEvent(this, ex.getMessage(), LogEventType.ERROR, ex));
					return;
				}
			}

			/**
			 * Remove the target in LocalView.
			 */
			LocalModelManager lmmgr = LocalModelManager.getInstance();
			lmmgr.removeRoot(path);

			localViewEventSource.fireEvent(new LocalViewEvent(this, ViewEventType.REFRESH));
			logEventSource.fireEvent(new LogEvent(this, "ホットショットフォルダ" + path + "から管理情報を削除しました。", LogEventType.INFO));
		}
		finally {
			taskmgr.start();
		}
	}
}
