package com.ml.api;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.config.Named;
import com.google.cloud.datastore.*;
import com.ml.Constants;
import com.ml.bean.GalaxyResponse;
import com.ml.bean.MyBean;
import com.ml.bean.Weather;
import com.ml.core.BuildGalaxy;

/**
 * Created by JesusLeon on 15/01/2017.
 */
@Api(name = "galaxy",
        version = "v1",
        namespace = @ApiNamespace(ownerDomain = "galaxy.ml.com",
                ownerName = "galaxy.ml.com",
                packagePath = ""),
        clientIds = {Constants.WEB_CLIENT_ID})

public class GalaxyApi {
    // Create an authorized Datastore service using Application Default Credentials.
    private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

    /**
     * A simple endpoint method that takes a name and says Hi back
     */
    @ApiMethod(name = "sayHi")
    public MyBean sayHi(@Named("name") String name) {
        MyBean response = new MyBean();
        response.setData("Hi, " + name);


        // Instantiates a client
        Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

        // The kind for the new entity
        String kind = "Task";
        // The name/ID for the new entity
        String taskName = "sampletask1";
        // The Cloud Datastore key for the new entity
        Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);

        // Prepares the new entity
        Entity task = Entity.newBuilder(taskKey)
                .set("description", "Buy milk by " + name)
                .build();

        // Saves the entity
        datastore.put(task);

        System.out.printf("Saved %s: %s%n", task.getKey().getName(), task.getString("description"));


        return response;
    }

    @ApiMethod(httpMethod = ApiMethod.HttpMethod.GET, path = "clima", name = "clima")
    public Weather getWeatherDay(@Named("dia") int day) {
        KeyFactory keyFactory = datastore.newKeyFactory().setKind("Weather");
        Transaction transaction = datastore.newTransaction();
        Entity dayEntity = transaction.get(keyFactory.newKey(day));

        Weather weather = new Weather();
        weather.setDay(day);

        if (dayEntity != null) {
            weather.setWeather(dayEntity.getString("clima"));
        } else {
            BuildGalaxy buildGalaxy = new BuildGalaxy();
            weather.setWeather(buildGalaxy.weatherDay(day, datastore));
        }

        return weather;
    }

    @ApiMethod(httpMethod = ApiMethod.HttpMethod.GET, path = "galaxy", name = "galaxy")
    public GalaxyResponse buildGalaxy(@Named("dias") int days) {
        BuildGalaxy buildGalaxy = new BuildGalaxy();
        buildGalaxy.build(days, datastore);

        KeyFactory keyFactory = datastore.newKeyFactory().setKind("Galaxy");
        Transaction transaction = datastore.newTransaction();
        Entity galaxyEntity = transaction.get(keyFactory.newKey(days));

        GalaxyResponse response = new GalaxyResponse();
        response.setDroughtWeather(galaxyEntity.getLong("num-dias-clima-seco"));
        response.setRainingWeather(galaxyEntity.getLong("num-dias-clima-lluvioso"));
        response.setOptimalWeather(galaxyEntity.getLong("num-dias-optimas-condiciones"));
        response.setMaxRainingDay(galaxyEntity.getLong("dia-que-mas-llovio"));

        return response;
    }

//    @ApiMethod(httpMethod = ApiMethod.HttpMethod.GET, path = "galaxy", name = "values")
//    public GalaxyResponse getGalaxy(@Named("dia") String numberOfDays) {
//        KeyFactory keyFactory = datastore.newKeyFactory().setKind("Galaxy");
//        Transaction transaction = datastore.newTransaction();
//        Entity galaxyEntity = transaction.get(keyFactory.newKey("Galaxy-" + numberOfDays));
//
//        GalaxyResponse response = new GalaxyResponse();
//        response.setDroughtPeriod((int) galaxyEntity.getLong("numdroughtperiod"));
//
//        return response;
//    }

}

@@ -321,36 +321,8 @@
 e;%0D%0A
-import com.ml.bean.MyBean;%0D%0A
 impo
@@ -346,16 +346,16 @@
 ather;%0D%0A
+
 import c
@@ -707,94 +707,8 @@
  %7B%0D%0A
-    // Create an authorized Datastore service using Application Default Credentials.%0D%0A
     
@@ -802,1054 +802,8 @@
 %0D%0A%0D%0A
-    /**%0D%0A     * A simple endpoint method that takes a name and says Hi back%0D%0A     */%0D%0A    @ApiMethod(name = %22sayHi%22)%0D%0A    public MyBean sayHi(@Named(%22name%22) String name) %7B%0D%0A        MyBean response = new MyBean();%0D%0A        response.setData(%22Hi, %22 + name);%0D%0A%0D%0A%0D%0A        // Instantiates a client%0D%0A        Datastore datastore = DatastoreOptions.getDefaultInstance().getService();%0D%0A%0D%0A        // The kind for the new entity%0D%0A        String kind = %22Task%22;%0D%0A        // The name/ID for the new entity%0D%0A        String taskName = %22sampletask1%22;%0D%0A        // The Cloud Datastore key for the new entity%0D%0A        Key taskKey = datastore.newKeyFactory().setKind(kind).newKey(name);%0D%0A%0D%0A        // Prepares the new entity%0D%0A        Entity task = Entity.newBuilder(taskKey)%0D%0A                .set(%22description%22, %22Buy milk by %22 + name)%0D%0A                .build();%0D%0A%0D%0A        // Saves the entity%0D%0A        datastore.put(task);%0D%0A%0D%0A        System.out.printf(%22Saved %25s: %25s%25n%22, task.getKey().getName(), task.getString(%22description%22));%0D%0A%0D%0A%0D%0A        return response;%0D%0A    %7D%0D%0A%0D%0A
     
@@ -2424,615 +2424,14 @@
 e;%0D%0A
+
     %7D%0D%0A
-%0D%0A//    @ApiMethod(httpMethod = ApiMethod.HttpMethod.GET, path = %22galaxy%22, name = %22values%22)%0D%0A//    public GalaxyResponse getGalaxy(@Named(%22dia%22) String numberOfDays) %7B%0D%0A//        KeyFactory keyFactory = datastore.newKeyFactory().setKind(%22Galaxy%22);%0D%0A//        Transaction transaction = datastore.newTransaction();%0D%0A//        Entity galaxyEntity = transaction.get(keyFactory.newKey(%22Galaxy-%22 + numberOfDays));%0D%0A//%0D%0A//        GalaxyResponse response = new GalaxyResponse();%0D%0A//        response.setDroughtPeriod((int) galaxyEntity.getLong(%22numdroughtperiod%22));%0D%0A//%0D%0A//        return response;%0D%0A//    %7D%0D%0A%0D%0A
 %7D%0D%0A

Remove unnecessary comment code