// The MIT License (MIT)
//
// Copyright (c) 2015, 2017 Arian Fornaris
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions: The above copyright notice and this permission
// notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
package phasereditor.assetpack.ui.refactorings;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.NullChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;

import phasereditor.assetpack.core.AssetModel;
import phasereditor.assetpack.ui.AssetPackUI;
import phasereditor.project.core.ProjectCore;

/**
 * @author arian
 *
 */
public class AssetFileDeleteParticipant extends DeleteParticipant {

	private IFile _file;
	private List<AssetModel> _result;

	@Override
	protected boolean initialize(Object element) {
		if (!(element instanceof IFile)) {
			return false;
		}

		IFile file = (IFile) element;

		if (!ProjectCore.isWebContentFile(file)) {
			return false;
		}

		_file = file;

		_result = AssetPackUI.findAssetResourceReferences(file);

		return !_result.isEmpty();
	}

	@Override
	public String getName() {
		return "Delete asset resource.";
	}

	@Override
	public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context)
			throws OperationCanceledException {

		RefactoringStatus status = new RefactoringStatus();

		String relpath = _file.getProjectRelativePath().toPortableString();

		for (AssetModel asset : _result) {
			String packname = asset.getPack().getFile().getName();
			status.addWarning("The asset pack entry '" + asset.getKey() + "' in '" + packname + "' requires the file '"
					+ relpath + "'");
		}

		// TODO: compute the affected canvas by the delete

		return status;
	}

	@Override
	public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
		return new NullChange();
	}

}

@@ -1207,17 +1207,71 @@
 .util.Li
-s
+nkedHashSet;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0A%0Aimpo
@@ -1820,16 +1820,179 @@
 tModel;%0A
+import phasereditor.assetpack.core.AssetPackCore;%0Aimport phasereditor.assetpack.core.FindAssetReferencesResult;%0Aimport phasereditor.assetpack.core.IAssetConsumer;%0A
 import p
@@ -3148,58 +3148,514 @@
 %0A%0A%09%09
-// TODO: compute the affected canvas by the delete
+Set%3CIFile%3E files = new LinkedHashSet%3C%3E();%0A%09%09for (AssetModel asset : _result) %7B%0A%09%09%09for (IAssetConsumer consumer : AssetPackCore.requestAssetConsumers()) %7B%0A%09%09%09%09FindAssetReferencesResult refs = consumer.getAssetReferences(asset, pm);%0A%09%09%09%09for (IFile file : refs.getFiles()) %7B%0A%09%09%09%09%09if (!files.contains(file)) %7B%0A%09%09%09%09%09%09files.add(file);%0A%09%09%09%09%09%09status.addWarning(%22The file '%22 + file.getName() + %22' indirectly uses the file '%22%0A%09%09%09%09%09%09%09%09+ _file.getProjectRelativePath().toPortableString() + %22'.%22);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%09

delete asset file refactoring checks for asset consumers.