/*
 * -\-\-
 * Spotify Apollo API Interfaces
 * --
 * Copyright (C) 2013 - 2015 Spotify AB
 * --
 * 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 com.spotify.apollo;

import com.google.auto.value.AutoValue;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import javax.annotation.Nullable;

/**
 * An immutable response. Each invocation of a 'withXXX' method returns a new instance,
 * making it safe to share instances between threads, at the cost of some efficiency.
 */
@AutoValue
abstract class ResponseImpl<T> implements Response<T> {

  static final Response<?> OK = createInternal(Status.OK, Optional.empty());

  // TODO Make it @Memoized once we upgrade to auto-value 1.4+
  @Override
  @Deprecated
  public Map<String, String> headers() {
    return internalHeadersImpl().asMap();
  }

  @Override
  public List<Map.Entry<String, String>> headerEntries() {
    return internalHeadersImpl().entries();
  }

  @Override
  public Optional<String> header(String name) {
    return internalHeadersImpl().get(name);
  }

  abstract Headers internalHeadersImpl();

  @Override
  public Response<T> withHeader(String header, String value) {
    // Allow overriding values
    final Map<String, String> newHeaders = new LinkedHashMap<>();
    newHeaders.putAll(headers());
    newHeaders.put(header, value);

    return createInternal(status(), Headers.create(newHeaders), payload());
  }

  @Override
  public Response<T> withHeaders(Map<String, String> headers) {
    // Allow overriding values
    final Map<String, String> newHeaders = new LinkedHashMap<>();
    newHeaders.putAll(headers());
    newHeaders.putAll(headers);

    return createInternal(status(), Headers.create(newHeaders), payload());
  }

  @Override
  public <P> Response<P> withPayload(@Nullable P newPayload) {
    return createInternal(status(), internalHeadersImpl(), Optional.ofNullable(newPayload));
  }

  static <T> Response<T> create(StatusType statusCode) {
    Objects.requireNonNull(statusCode);

    //noinspection unchecked
    return statusCode == Status.OK
        ? (Response<T>) OK
        : createInternal(statusCode, Optional.<T>empty());
  }

  static <T> Response<T> create(StatusType statusCode, T payload) {
    Objects.requireNonNull(statusCode);
    return createInternal(statusCode, Optional.of(payload));
  }

  static <T> Response<T> create(T payload) {
    return createInternal(Status.OK, Optional.of(payload));
  }

  private static <T> Response<T> createInternal(StatusType statusCode, Optional<T> payload) {
    return createInternal(
        statusCode,
        Headers.EMPTY,
        payload);
  }

  private static <T> Response<T> createInternal(
      StatusType statusCode,
      Headers headers,
      Optional<T> payload) {
    return new AutoValue_ResponseImpl<>(
        statusCode,
        payload,
        headers);
  }
}
