/**
 * 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.compute.domain;

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

import java.net.URI;
import java.util.Map;
import java.util.Set;

import org.jclouds.javax.annotation.Nullable;

import org.jclouds.compute.domain.internal.NodeMetadataImpl;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.domain.LoginCredentials;
import org.jclouds.domain.LoginCredentials.Builder;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

/**
 * @author Adrian Cole
 */
public class NodeMetadataBuilder extends ComputeMetadataBuilder {
   private NodeState state;
   private Set<String> publicAddresses = Sets.newLinkedHashSet();
   private Set<String> privateAddresses = Sets.newLinkedHashSet();
   @Nullable
   private LoginCredentials credentials;
   @Nullable
   private String group;
   private int loginPort = 22;
   @Nullable
   private String imageId;
   @Nullable
   private Hardware hardware;
   @Nullable
   private OperatingSystem os;
   @Nullable
   private String hostname;

   public NodeMetadataBuilder() {
      super(ComputeType.NODE);
   }

   public NodeMetadataBuilder loginPort(int loginPort) {
      this.loginPort = loginPort;
      return this;
   }

   public NodeMetadataBuilder state(NodeState state) {
      this.state = checkNotNull(state, "state");
      return this;
   }

   public NodeMetadataBuilder publicAddresses(Iterable<String> publicAddresses) {
      this.publicAddresses = ImmutableSet.copyOf(checkNotNull(publicAddresses, "publicAddresses"));
      return this;
   }

   public NodeMetadataBuilder privateAddresses(Iterable<String> privateAddresses) {
      this.privateAddresses = ImmutableSet.copyOf(checkNotNull(privateAddresses, "privateAddresses"));
      return this;
   }

   /**
    * <h4>will be removed in jclouds 1.4.0</h4>
    * 
    * @see LoginCredentials#shouldAuthenticateSudo
    */
   @Deprecated
   public NodeMetadataBuilder adminPassword(@Nullable String adminPassword) {
      if (adminPassword != null) {
         Builder builder = credentials != null ? credentials.toBuilder() : LoginCredentials
               .builder();
         builder.authenticateSudo(true);
         builder.password(adminPassword);
         this.credentials = builder.build();
      }
      return this;
   }

   /**
    * <h4>will be removed in jclouds 1.4.0</h4>
    * 
    * @see #credentials(LoginCredentials)
    */
   @Deprecated
   public NodeMetadataBuilder credentials(@Nullable Credentials credentials) {
      return credentials(LoginCredentials.fromCredentials(credentials));
   }
   
   public NodeMetadataBuilder credentials(@Nullable LoginCredentials credentials) {
      this.credentials = credentials;
      return this;
   }


   public NodeMetadataBuilder group(@Nullable String group) {
      this.group = group;
      return this;
   }

   public NodeMetadataBuilder imageId(@Nullable String imageId) {
      this.imageId = imageId;
      return this;
   }

   public NodeMetadataBuilder hardware(@Nullable Hardware hardware) {
      this.hardware = hardware;
      return this;
   }

   public NodeMetadataBuilder operatingSystem(@Nullable OperatingSystem os) {
      this.os = os;
      return this;
   }

   public NodeMetadataBuilder hostname(String hostname) {
      this.hostname = hostname;
      return this;
   }

   @Override
   public NodeMetadataBuilder id(String id) {
      return NodeMetadataBuilder.class.cast(super.id(id));
   }

   @Override
   public NodeMetadataBuilder tags(Iterable<String> tags) {
      return NodeMetadataBuilder.class.cast(super.tags(tags));
   }

   @Override
   public NodeMetadataBuilder ids(String id) {
      return NodeMetadataBuilder.class.cast(super.ids(id));
   }

   @Override
   public NodeMetadataBuilder providerId(String providerId) {
      return NodeMetadataBuilder.class.cast(super.providerId(providerId));
   }

   @Override
   public NodeMetadataBuilder name(String name) {
      return NodeMetadataBuilder.class.cast(super.name(name));
   }

   @Override
   public NodeMetadataBuilder location(Location location) {
      return NodeMetadataBuilder.class.cast(super.location(location));
   }

   @Override
   public NodeMetadataBuilder uri(URI uri) {
      return NodeMetadataBuilder.class.cast(super.uri(uri));
   }

   @Override
   public NodeMetadataBuilder userMetadata(Map<String, String> userMetadata) {
      return NodeMetadataBuilder.class.cast(super.userMetadata(userMetadata));
   }

   @Override
   public NodeMetadata build() {
      return new NodeMetadataImpl(providerId, name, id, location, uri, userMetadata, tags, group, hardware, imageId,
               os, state, loginPort, publicAddresses, privateAddresses,  credentials, hostname);
   }

   public static NodeMetadataBuilder fromNodeMetadata(NodeMetadata node) {
      return new NodeMetadataBuilder().providerId(node.getProviderId()).name(node.getName()).id(node.getId()).location(
               node.getLocation()).uri(node.getUri()).userMetadata(node.getUserMetadata()).tags(node.getTags()).group(
               node.getGroup()).hardware(node.getHardware()).imageId(node.getImageId()).operatingSystem(
               node.getOperatingSystem()).state(node.getState()).loginPort(node.getLoginPort()).publicAddresses(
               node.getPublicAddresses()).privateAddresses(node.getPrivateAddresses()).credentials(node.getCredentials()).hostname(node.getHostname());
   }

}