/**
 * Licensed to jclouds, Inc. (jclouds) under one or more
 * contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  jclouds licenses this file
 * to you 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 org.jclouds.cloudstack.features;

import java.util.Set;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.jclouds.cloudstack.binders.BindTemplateMetadataToQueryParams;
import org.jclouds.cloudstack.domain.AsyncCreateResponse;
import org.jclouds.cloudstack.domain.ExtractMode;
import org.jclouds.cloudstack.domain.Template;
import org.jclouds.cloudstack.domain.TemplateMetadata;
import org.jclouds.cloudstack.domain.TemplatePermission;
import org.jclouds.cloudstack.filters.QuerySigner;
import org.jclouds.cloudstack.options.AccountInDomainOptions;
import org.jclouds.cloudstack.options.CreateTemplateOptions;
import org.jclouds.cloudstack.options.DeleteTemplateOptions;
import org.jclouds.cloudstack.options.ExtractTemplateOptions;
import org.jclouds.cloudstack.options.ListTemplatesOptions;
import org.jclouds.cloudstack.options.RegisterTemplateOptions;
import org.jclouds.cloudstack.options.UpdateTemplateOptions;
import org.jclouds.cloudstack.options.UpdateTemplatePermissionsOptions;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.OnlyElement;
import org.jclouds.rest.annotations.QueryParams;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.annotations.Unwrap;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;

import com.google.common.util.concurrent.ListenableFuture;

/**
 * Provides asynchronous access to cloudstack via their REST API.
 * <p/>
 * 
 * @see TemplateClient
 * @see <a
 *      href="http://download.cloud.com/releases/2.2.0/api_2.2.12/TOC_User.html"
 *      />
 * @author Adrian Cole
 */
@RequestFilters(QuerySigner.class)
@QueryParams(keys = "response", values = "json")
@SkipEncoding(',')
public interface TemplateAsyncClient {

   /**
    * @see TemplateClient#createTemplate
    */
   @GET
   @QueryParams(keys = "command", values = "createTemplate")
   @Unwrap
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<AsyncCreateResponse> createTemplate(
         @BinderParam(BindTemplateMetadataToQueryParams.class) TemplateMetadata templateMetadata,
         CreateTemplateOptions... options);

   /**
    * @see TemplateClient#registerTemplate
    */
   @GET
   @QueryParams(keys = "command", values = "registerTemplate")
   @SelectJson("template")
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<Set<Template>> registerTemplate(
         @BinderParam(BindTemplateMetadataToQueryParams.class) TemplateMetadata templateMetadata,
         @QueryParam("format") String format, @QueryParam("hypervisor") String hypervisor,
         @QueryParam("url") String url, @QueryParam("zoneid") long zoneId, RegisterTemplateOptions... options);

   /**
    * @see TemplateClient#updateTemplate
    */
   @GET
   @QueryParams(keys = "command", values = "updateTemplate")
   @SelectJson("template")
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<Template> updateTemplate(@QueryParam("id") long id, UpdateTemplateOptions... options);

   /**
    * @see TemplateClient#copyTemplate
    */
   @GET
   @QueryParams(keys = "command", values = "copyTemplate")
   @Unwrap
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<AsyncCreateResponse> copyTemplateToZone(@QueryParam("id") long id,
         @QueryParam("sourcezoneid") long sourceZoneId, @QueryParam("destzoneid") long destZoneId);

   /**
    * @see TemplateClient#deleteTemplate
    */
   @GET
   @QueryParams(keys = "command", values = "deleteTemplate")
   @Unwrap
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<AsyncCreateResponse> deleteTemplate(@QueryParam("id") long id, DeleteTemplateOptions... options);

   /**
    * @see TemplateClient#listTemplates
    */
   @GET
   @QueryParams(keys = { "command", "templatefilter" }, values = { "listTemplates", "executable" })
   @SelectJson("template")
   @Consumes(MediaType.APPLICATION_JSON)
   @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
   ListenableFuture<Set<Template>> listTemplates();

   /**
    * @see TemplateClient#listTemplates(ListTemplatesOptions)
    */
   @GET
   @QueryParams(keys = "command", values = "listTemplates")
   @SelectJson("template")
   @Consumes(MediaType.APPLICATION_JSON)
   @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
   ListenableFuture<Set<Template>> listTemplates(ListTemplatesOptions options);

   /**
    * @see TemplateClient#getTemplate
    */
   @GET
   // templatefilter required in at least 2.2.8 version
   @QueryParams(keys = { "command", "templatefilter" }, values = { "listTemplates", "executable" })
   @SelectJson("template")
   @OnlyElement
   @Consumes(MediaType.APPLICATION_JSON)
   @ExceptionParser(ReturnNullOnNotFoundOr404.class)
   ListenableFuture<Template> getTemplateInZone(@QueryParam("id") long templateId, @QueryParam("zoneid") long zoneId);

   /**
    * @see TemplateClient#updateTemplatePermissions
    */
   @GET
   @QueryParams(keys = "command", values = "updateTemplatePermissions")
   ListenableFuture<Void> updateTemplatePermissions(@QueryParam("id") long id,
         UpdateTemplatePermissionsOptions... options);

   /**
    * @see TemplateClient#listTemplatePermissions
    */
   @GET
   @QueryParams(keys = "command", values = "listTemplatePermissions")
   @Unwrap
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<Set<TemplatePermission>> listTemplatePermissions(@QueryParam("id") long id,
         AccountInDomainOptions... options);

   /**
    * @see TemplateClient#extractTemplate
    */
   @GET
   @QueryParams(keys = "command", values = "extractTemplate")
   @Unwrap
   @Consumes(MediaType.APPLICATION_JSON)
   ListenableFuture<AsyncCreateResponse> extractTemplate(@QueryParam("id") long id,
         @QueryParam("mode") ExtractMode mode, @QueryParam("zoneid") long zoneId, ExtractTemplateOptions... options);
}
