/*
 * Copyright 2013-2016 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.run.remote;

import java.util.Collection;

import javax.annotation.Nonnull;

import org.jdom.Element;
import com.intellij.compiler.options.CompileStepBeforeRun;
import com.intellij.diagnostic.logging.LogConfigurationPanel;
import com.intellij.execution.ExecutionBundle;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ModuleBasedConfiguration;
import com.intellij.execution.configurations.RemoteRunProfile;
import com.intellij.execution.configurations.RunConfigurationModule;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.RunConfigurationWithSuppressedDefaultRunAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.options.SettingsEditorGroup;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import consulo.dotnet.debugger.DotNetConfigurationWithDebug;
import consulo.dotnet.execution.DebugConnectionInfo;
import consulo.dotnet.run.DotNetRunKeys;

/**
 * @author VISTALL
 * @since 27-Dec-16
 */
public abstract class DotNetRemoteConfiguration extends ModuleBasedConfiguration<RunConfigurationModule> implements RunConfigurationWithSuppressedDefaultRunAction, CompileStepBeforeRun.Suppressor,
		RemoteRunProfile, DotNetConfigurationWithDebug
{
	public boolean SERVER_MODE;
	public String HOST;
	public int PORT;

	public DotNetRemoteConfiguration(final Project project, ConfigurationFactory configurationFactory)
	{
		super(new RunConfigurationModule(project), configurationFactory);
	}

	@Override
	public void writeExternal(final Element element) throws WriteExternalException
	{
		super.writeExternal(element);
		final Module module = getConfigurationModule().getModule();
		if(module != null)
		{
			writeModule(element);
		}
		DefaultJDOMExternalizer.writeExternal(this, element);
	}

	@Override
	public void readExternal(final Element element) throws InvalidDataException
	{
		super.readExternal(element);
		readModule(element);
		DefaultJDOMExternalizer.readExternal(this, element);
	}

	@Override
	public RunProfileState getState(@Nonnull final Executor executor, @Nonnull final ExecutionEnvironment env) throws ExecutionException
	{
		DotNetRemoteRunState state = new DotNetRemoteRunState(env);
		state.putUserData(DotNetRunKeys.DEBUG_CONNECTION_INFO_KEY, new DebugConnectionInfo(HOST, PORT, SERVER_MODE));
		return state;
	}

	@Override
	@Nonnull
	public SettingsEditor<? extends DotNetRemoteConfiguration> getConfigurationEditor()
	{
		SettingsEditorGroup<DotNetRemoteConfiguration> group = new SettingsEditorGroup<>();
		group.addEditor(ExecutionBundle.message("run.configuration.configuration.tab.title"), new DotNetRemoteConfigurable<>(getProject()));
		group.addEditor(ExecutionBundle.message("logs.tab.title"), new LogConfigurationPanel<>());
		return group;
	}

	@Override
	public Collection<Module> getValidModules()
	{
		return getAllModules();
	}
}

@@ -630,97 +630,8 @@
 e;%0A%0A
-import java.util.Collection;%0A%0Aimport javax.annotation.Nonnull;%0A%0Aimport org.jdom.Element;%0A
 impo
@@ -685,16 +685,16 @@
 oreRun;%0A
+
 import c
@@ -933,293 +933,9 @@
 ons.
-ConfigurationFactory;%0Aimport com.intellij.execution.configurations.ModuleBasedConfiguration;%0Aimport com.intellij.execution.configurations.RemoteRunProfile;%0Aimport com.intellij.execution.configurations.RunConfigurationModule;%0Aimport com.intellij.execution.configurations.RunProfileState
+*
 ;%0Aim
@@ -1599,16 +1599,104 @@
 RunKeys;
+%0Aimport org.jdom.Element;%0A%0Aimport javax.annotation.Nonnull;%0Aimport java.util.Collection;
 %0A%0A/**%0A *
@@ -2358,124 +2358,8 @@
 t);%0A
-%09%09final Module module = getConfigurationModule().getModule();%0A%09%09if(module != null)%0A%09%09%7B%0A%09%09%09writeModule(element);%0A%09%09%7D%0A
 %09%09De
@@ -2497,32 +2497,32 @@
 ataException%0A%09%7B%0A
+
 %09%09super.readExte
@@ -2540,31 +2540,8 @@
 t);%0A
-%09%09readModule(element);%0A
 %09%09De

drop deprecated usage