/*
 * Copyright 2014 defrac inc.
 *
 * 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 defrac.intellij.compiler;

import com.intellij.openapi.compiler.CompileContext;
import defrac.intellij.config.DefracConfig;
import defrac.intellij.config.DefracConfigBase;
import defrac.intellij.facet.DefracFacet;
import defrac.intellij.ipc.DefracCommands;
import defrac.intellij.ipc.DefracIpc;
import defrac.intellij.run.DefracRunConfiguration;
import org.jetbrains.annotations.NotNull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 */
public final class LoadTask extends BooleanBasedCompilerTask {
  @NotNull
  public static final LoadTask INSTANCE = new LoadTask();

  private LoadTask() {
  }

  @NotNull
  @Override
  protected String getDefracCommandName() {
    return DefracCommands.LOAD;
  }

  protected boolean shouldRunForFacet(@NotNull final DefracFacet facet) {
    return !facet.getPlatform().isGeneric();
  }

  @Override
  protected DefracIpc.Executor doCompile(@NotNull final CompileContext context,
                                         @NotNull final DefracRunConfiguration configuration,
                                         @NotNull final DefracFacet facet,
                                         @NotNull final DefracIpc ipc) {

    final DefracConfig config = facet.getConfig();

    if(config == null) {
      reportError(context, "Can't load defrac settings");
      return null;
    }

    // configure settings
    final DefracConfigBase settings = checkNotNull(config.getOrCreatePlatform(facet.getPlatform())).copy();
    settings.setMain(checkNotNull(configuration.getRunClass()));
    settings.setStrict(configuration.isStrict());

    if(configuration.launchInEmulator()) {
      settings.setDeployOnEmulator();
    } else if(configuration.launchOnDevice()) {
      settings.setDeployOnDevice();
    }

    return ipc.load(facet.getPlatform(), settings);
  }
}

@@ -673,16 +673,55 @@
 ontext;%0A
+import defrac.intellij.DefracPlatform;%0A
 import d
@@ -2032,14 +2032,76 @@
 nfig
-Base s
+ localSettings = config.copy();%0A    final DefracConfigBase platformS
 etti
@@ -2119,22 +2119,29 @@
 NotNull(
-config
+localSettings
 .getOrCr
@@ -2178,22 +2178,23 @@
 ()))
-.copy();%0A    s
+;%0A    platformS
 etti
@@ -2249,25 +2249,33 @@
 ss()));%0A    
-s
+platformS
 ettings.setS
@@ -2349,33 +2349,41 @@
 ator()) %7B%0A      
-s
+platformS
 ettings.setDeplo
@@ -2451,17 +2451,25 @@
 %7B%0A      
-s
+platformS
 ettings.
@@ -2520,30 +2520,38 @@
 oad(
-facet.getPlatform(), s
+DefracPlatform.GENERIC, localS
 etti

update of default.settings