/*
 * Copyright 2013-2016 consulo.io
 *
 * 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 consulo.unity3d.unityscript.projectView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.inject.Inject;

import com.intellij.ide.projectView.TreeStructureProvider;
import com.intellij.ide.projectView.ViewSettings;
import com.intellij.ide.util.treeView.AbstractTreeNode;
import com.intellij.lang.javascript.psi.JSFile;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import consulo.ui.RequiredUIAccess;
import consulo.unity3d.module.Unity3dModuleExtensionUtil;
import consulo.unity3d.module.Unity3dRootModuleExtension;

/**
 * @author VISTALL
 * @since 19.07.2015
 */
public class UnityScriptFileProjectViewProvider implements TreeStructureProvider, DumbAware
{
	private final Project myProject;

	@Inject
	public UnityScriptFileProjectViewProvider(Project project)
	{
		myProject = project;
	}

	@Override
	@RequiredUIAccess
	public Collection<AbstractTreeNode> modify(AbstractTreeNode parent, Collection<AbstractTreeNode> children, ViewSettings settings)
	{
		if(!myProject.isInitialized())
		{
			return children;
		}
		Unity3dRootModuleExtension rootModuleExtension = Unity3dModuleExtensionUtil.getRootModuleExtension(myProject);
		if(rootModuleExtension == null)
		{
			return children;
		}

		List<AbstractTreeNode> nodes = new ArrayList<AbstractTreeNode>(children.size());
		for(AbstractTreeNode child : children)
		{
			Object value = child.getValue();
			if(value instanceof JSFile)
			{
				Module moduleForPsiElement = ModuleUtilCore.findModuleForPsiElement((PsiElement) value);
				if(moduleForPsiElement != null)
				{
					nodes.add(new UnityScriptFileNode(myProject, (PsiFile) value, settings));
					continue;
				}
			}
			nodes.add(child);
		}
		return nodes;
	}
}

@@ -643,118 +643,8 @@
 w;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0Aimport javax.inject.Inject;%0A%0A
 impo
@@ -698,16 +698,16 @@
 ovider;%0A
+
 import c
@@ -804,16 +804,70 @@
 eeNode;%0A
+import com.intellij.ide.util.treeView.AbstractTreeUi;%0A
 import c
@@ -996,24 +996,78 @@
 leUtilCore;%0A
+import com.intellij.openapi.progress.ProgressManager;%0A
 import com.i
@@ -1211,24 +1211,71 @@
 si.PsiFile;%0A
+import consulo.annotations.RequiredReadAction;%0A
 import consu
@@ -1415,16 +1415,158 @@
 nsion;%0A%0A
+import javax.annotation.Nonnull;%0Aimport javax.inject.Inject;%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0A%0A
 /**%0A * @
@@ -2003,66 +2003,244 @@
 %7B%0A%09%09
-if(!myProject.isInitialized(
+return AbstractTreeUi.calculateYieldingToWriteAction(() -%3E doModify(children, settings
 ))
+;
 %0A%09
-%09%7B%0A%09%09%09return children;%0A%09%09%7D
+%7D%0A%0A%09@RequiredReadAction%0A%09@Nonnull%0A%09private Collection%3CAbstractTreeNode%3E doModify(Collection%3CAbstractTreeNode%3E children, ViewSettings settings)%0A%09%7B
 %0A%09%09U
@@ -2459,32 +2459,16 @@
 rayList%3C
-AbstractTreeNode
 %3E(childr
@@ -2520,20 +2520,57 @@
 ildren)%0A
-
 %09%09%7B%0A
+%09%09%09ProgressManager.checkCanceled();%0A%0A
 %09%09%09Objec

add checkCanceled