/*
 * Copyright © 2012-2015 Cask Data, 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 co.cask.cdap.cli.command.app;

import co.cask.cdap.api.artifact.ArtifactScope;
import co.cask.cdap.cli.ArgumentName;
import co.cask.cdap.cli.CLIConfig;
import co.cask.cdap.cli.ElementType;
import co.cask.cdap.cli.english.Article;
import co.cask.cdap.cli.english.Fragment;
import co.cask.cdap.cli.util.AbstractAuthCommand;
import co.cask.cdap.cli.util.FilePathResolver;
import co.cask.cdap.client.ApplicationClient;
import co.cask.cdap.proto.Id;
import co.cask.cdap.proto.artifact.AppRequest;
import co.cask.cdap.proto.artifact.ArtifactSummary;
import co.cask.common.cli.Arguments;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.inject.Inject;

import java.io.File;
import java.io.FileReader;
import java.io.PrintStream;

/**
 * Deploys an application from an existing artifact.
 */
public class CreateAppCommand extends AbstractAuthCommand {
  private static final Gson GSON = new Gson();
  private final ApplicationClient applicationClient;
  private final FilePathResolver resolver;

  @Inject
  public CreateAppCommand(ApplicationClient applicationClient, FilePathResolver resolver, CLIConfig cliConfig) {
    super(cliConfig);
    this.applicationClient = applicationClient;
    this.resolver = resolver;
  }

  @Override
  public void perform(Arguments arguments, PrintStream output) throws Exception {
    String appName = arguments.get(ArgumentName.APP.toString());
    Id.Application appId = Id.Application.from(cliConfig.getCurrentNamespace(), appName);

    String artifactName = arguments.get(ArgumentName.ARTIFACT_NAME.toString());
    String artifactVersion = arguments.get(ArgumentName.ARTIFACT_VERSION.toString());
    ArtifactScope artifactScope = ArtifactScope.valueOf(arguments.get(ArgumentName.SCOPE.toString()).toUpperCase());
    ArtifactSummary artifact = new ArtifactSummary(artifactName, artifactVersion, artifactScope);

    JsonObject config = new JsonObject();
    String configPath = arguments.getOptional(ArgumentName.APP_CONFIG_FILE.toString());
    if (configPath != null) {
      File configFile = resolver.resolvePathToFile(configPath);
      try (FileReader reader = new FileReader(configFile)) {
        ConfigFile configContents = GSON.fromJson(reader, ConfigFile.class);
        config = configContents.config;
      }
    }

    AppRequest<JsonObject> appRequest = new AppRequest<>(artifact, config);
    applicationClient.deploy(appId, appRequest);
    output.println("Successfully created application");
  }

  @Override
  public String getPattern() {
    return String.format("create app <%s> <%s> <%s> <%s> [<%s>]", ArgumentName.APP, ArgumentName.ARTIFACT_NAME,
                         ArgumentName.ARTIFACT_VERSION, ArgumentName.SCOPE, ArgumentName.APP_CONFIG_FILE);
  }

  @Override
  public String getDescription() {
    return String.format("Creates %s from an artifact with optional configuration. If configuration is needed, it " +
      "must be given as a file whose contents are a JSON Object containing the application config. " +
      "For example, the file contents could contain: '{ \"config\": { \"stream\": \"purchases\" } }'. In this case, " +
      "the application would recieve '{ \"stream\": \"purchases\" }' as its config object.",
      Fragment.of(Article.A, ElementType.APP.getName()));
  }

  // simple class for deserializing contents of config file.
  private static final class ConfigFile {
    private JsonObject config;
  }
}

@@ -1249,16 +1249,58 @@
 Object;%0A
+import com.google.gson.reflect.TypeToken;%0A
 import c
@@ -1324,16 +1324,16 @@
 Inject;%0A
-
 %0Aimport 
@@ -1400,16 +1400,47 @@
 tStream;
+%0Aimport java.lang.reflect.Type;
 %0A%0A/**%0A *
@@ -1554,16 +1554,112 @@
 mmand %7B%0A
+  private static final Type configType = new TypeToken%3CAppRequest%3CJsonObject%3E%3E() %7B %7D.getType();%0A
   privat
@@ -2951,33 +2951,41 @@
     
-ConfigFile configContents
+AppRequest%3CJsonObject%3E appRequest
  = G
@@ -3009,24 +3009,18 @@
 er, 
-C
+c
 onfig
-File.class
+Type
 );%0A 
@@ -3039,29 +3039,30 @@
 g = 
-configContents.c
+appRequest.getC
 onfig
+()
 ;%0A  
@@ -4008,16 +4008,16 @@
 ject.%22,%0A
+
       Fr
@@ -4073,146 +4073,7 @@
 %0A  %7D
-%0A%0A  // simple class for deserializing contents of config file.%0A  private static final class ConfigFile %7B%0A    private JsonObject config;%0A  %7D
 %0A%7D%0A

switch to actuall app request object instead of a custom object.