/*
 * Copyright 2022 Google LLC
 *
 * 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.google.cloud.storage;

import static com.google.cloud.storage.StorageV2ProtoUtils.fmtProto;

import com.google.cloud.storage.Crc32cValue.Crc32cLengthKnown;
import com.google.cloud.storage.WriteCtx.WriteObjectRequestBuilderFactory;
import com.google.storage.v2.WriteObjectRequest;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

final class WriteCtx<RequestFactoryT extends WriteObjectRequestBuilderFactory> {

  private final RequestFactoryT requestFactory;

  private final AtomicLong totalSentBytes;
  private final AtomicLong confirmedBytes;
  private final AtomicReference<@Nullable Crc32cLengthKnown> cumulativeCrc32c;

  private WriteCtx(RequestFactoryT requestFactory, @Nullable Crc32cLengthKnown initialValue) {
    this.requestFactory = requestFactory;
    this.totalSentBytes = new AtomicLong(0);
    this.confirmedBytes = new AtomicLong(0);
    this.cumulativeCrc32c = new AtomicReference<>(initialValue);
  }

  static <RFT extends WriteObjectRequestBuilderFactory> WriteCtx<RFT> of(
      RFT rft, @NonNull Hasher hasher) {
    return new WriteCtx<>(rft, hasher.initialValue());
  }

  public RequestFactoryT getRequestFactory() {
    return requestFactory;
  }

  public WriteObjectRequest.Builder newRequestBuilder() {
    return requestFactory.newBuilder();
  }

  public AtomicLong getTotalSentBytes() {
    return totalSentBytes;
  }

  public AtomicLong getConfirmedBytes() {
    return confirmedBytes;
  }

  public AtomicReference<@Nullable Crc32cLengthKnown> getCumulativeCrc32c() {
    return cumulativeCrc32c;
  }

  // TODO: flush this out more
  boolean isDirty() {
    return confirmedBytes.get() == totalSentBytes.get();
  }

  @Override
  public String toString() {
    return "ServerState{"
        + "requestFactory="
        + requestFactory
        + ", totalSentBytes="
        + totalSentBytes
        + ", confirmedBytes="
        + confirmedBytes
        + ", totalSentCrc32c="
        + cumulativeCrc32c
        + '}';
  }

  interface WriteObjectRequestBuilderFactory {
    WriteObjectRequest.Builder newBuilder();

    @Nullable String bucketName();

    static SimpleWriteObjectRequestBuilderFactory simple(WriteObjectRequest req) {
      return new SimpleWriteObjectRequestBuilderFactory(req);
    }
  }

  static final class SimpleWriteObjectRequestBuilderFactory
      implements WriteObjectRequestBuilderFactory {
    private final WriteObjectRequest req;

    private SimpleWriteObjectRequestBuilderFactory(WriteObjectRequest req) {
      this.req = req;
    }

    @Override
    public WriteObjectRequest.Builder newBuilder() {
      return req.toBuilder();
    }

    @Override
    public @Nullable String bucketName() {
      if (req.hasWriteObjectSpec() && req.getWriteObjectSpec().hasResource()) {
        return req.getWriteObjectSpec().getResource().getBucket();
      }
      return null;
    }

    @Override
    public String toString() {
      return "SimpleWriteObjectRequestBuilderFactory{" + "req=" + fmtProto(req) + '}';
    }
  }
}
