/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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.sts.options;

import org.jclouds.http.options.BaseHttpRequestOptions;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.collect.Multimap;

/**
 * @see <a href=
 *      "http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html"
 *      />
 */
public class AssumeRoleOptions extends BaseHttpRequestOptions implements Cloneable {

   // long as this is a more typical unit for duration, hence less casting
   private Long durationSeconds;
   private String policy;
   private String externalId;

   /**
    * A unique identifier that is generated by a third party for each of their customers. 
    */
   public AssumeRoleOptions externalId(String externalId) {
      this.externalId = externalId;
      return this;
   }

   /**
    * The duration, in seconds, that the credentials should remain valid. 12
    * hours is default. 15 minutes is current minimum.
    */
   public AssumeRoleOptions durationSeconds(long durationSeconds) {
      this.durationSeconds = durationSeconds;
      return this;
   }

   /**
    * A supplemental policy that can be associated with the temporary security credentials. 
    */
   public AssumeRoleOptions policy(String policy) {
      this.policy = policy;
      return this;
   }

   public static class Builder {

      /**
       * @see AssumeRoleOptions#externalId
       */
      public static AssumeRoleOptions externalId(String externalId) {
         return new AssumeRoleOptions().externalId(externalId);
      }

      /**
       * @see AssumeRoleOptions#durationSeconds
       */
      public static AssumeRoleOptions durationSeconds(long durationSeconds) {
         return new AssumeRoleOptions().durationSeconds(durationSeconds);
      }

      /**
       * @see AssumeRoleOptions#policy
       */
      public static AssumeRoleOptions policy(String policy) {
         return new AssumeRoleOptions().policy(policy);
      }
   }

   @Override
   public Multimap<String, String> buildFormParameters() {
      Multimap<String, String> params = super.buildFormParameters();
      if (externalId != null)
         params.put("ExternalId", externalId.toString());
      if (durationSeconds != null)
         params.put("DurationSeconds", durationSeconds.toString());
      if (policy != null)
         params.put("Policy", policy);
      return params;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public int hashCode() {
      return Objects.hashCode(externalId, durationSeconds, policy);
   }

   @Override
   public AssumeRoleOptions clone() {
      return new AssumeRoleOptions().externalId(externalId).durationSeconds(durationSeconds)
            .policy(policy);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      AssumeRoleOptions other = AssumeRoleOptions.class.cast(obj);
      return Objects.equal(this.externalId, other.externalId)
            && Objects.equal(this.durationSeconds, other.durationSeconds)
            && Objects.equal(this.policy, other.policy);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String toString() {
      return MoreObjects.toStringHelper(this).omitNullValues().add("externalId", externalId)
            .add("durationSeconds", durationSeconds).add("policy", policy).toString();
   }
}
