/*
 * Copyright 2013-2014 must-be.org
 *
 * 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.dotnet.compiler;

import gnu.trove.THashMap;

import java.util.Map;

import javax.annotation.Nonnull;

import com.intellij.ide.macro.Macro;
import com.intellij.ide.macro.MacroManager;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.impl.SimpleDataContext;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.io.FileUtil;
import consulo.dotnet.module.extension.DotNetRunModuleExtension;
import consulo.dotnet.module.macro.TargetFileExtensionMacro;

/**
 * @author VISTALL
 * @since 27.11.13.
 */
public class DotNetMacroUtil
{
	@Nonnull
	public static DataContext createContext(@Nonnull Module module, boolean debugSymbols)
	{
		Map<Key, Object> map = new THashMap<>();
		map.put(CommonDataKeys.PROJECT, module.getProject());
		map.put(LangDataKeys.MODULE, module);
		if(debugSymbols)
		{
			map.put(TargetFileExtensionMacro.DEBUG_SYMBOLS, Boolean.TRUE);
		}
		return SimpleDataContext.getSimpleContext(map, null);
	}

	@Nonnull
	public static String expandOutputFile(@Nonnull DotNetRunModuleExtension<?> extension)
	{
		return expandOutputFile(extension, false);
	}

	@Nonnull
	public static String expandOutputFile(@Nonnull DotNetRunModuleExtension<?> extension, boolean debugSymbols)
	{
		return expand(extension.getModule(), extension.getOutputDir() + "/" + extension.getFileName(), debugSymbols);
	}

	@Nonnull
	public static String expandOutputDir(@Nonnull DotNetRunModuleExtension<?> extension)
	{
		return expand(extension.getModule(), extension.getOutputDir(), false);
	}

	@Nonnull
	public static String expand(@Nonnull Module module, @Nonnull String path, boolean debugSymbols)
	{
		String newPath;
		try
		{
			newPath = MacroManager.getInstance().expandSilentMarcos(path, true, createContext(module, debugSymbols));
			return FileUtil.toSystemDependentName(newPath);
		}
		catch(Macro.ExecutionCancelledException e)
		{
			return path;
		}
	}
}

@@ -648,24 +648,45 @@
 .THashMap;%0A%0A
+import java.io.File;%0A
 import java.
@@ -2033,24 +2033,177 @@
 Symbols)%0A%09%7B%0A
+%09%09String outputDir = FileUtil.toSystemDependentName(extension.getOutputDir());%0A%09%09if(outputDir.charAt(outputDir.length() - 1) == File.separatorChar)%0A%09%09%7B%0A%09
 %09%09return exp
@@ -2220,32 +2220,44 @@
 ion.getModule(),
+ outputDir +
  extension.getOu
@@ -2254,25 +2254,107 @@
 sion.get
-O
+FileName(), debugSymbols);%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09return expand(extension.getModule(), o
 utputDir
 () + %22/%22
@@ -2349,16 +2349,29 @@
 tDir
-()
  + 
-%22/%22
+File.separatorChar
  + e
@@ -2409,16 +2409,20 @@
 mbols);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@No

allow trailing dir separator - don't duplicate it