/*
 * Copyright 2026 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
 *
 *      https://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.pubsub.v1;

import com.google.api.core.BetaApi;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.Empty;
import com.google.pubsub.v1.CommitSchemaRequest;
import com.google.pubsub.v1.CreateSchemaRequest;
import com.google.pubsub.v1.DeleteSchemaRequest;
import com.google.pubsub.v1.DeleteSchemaRevisionRequest;
import com.google.pubsub.v1.GetSchemaRequest;
import com.google.pubsub.v1.ListSchemaRevisionsRequest;
import com.google.pubsub.v1.ListSchemaRevisionsResponse;
import com.google.pubsub.v1.ListSchemasRequest;
import com.google.pubsub.v1.ListSchemasResponse;
import com.google.pubsub.v1.RollbackSchemaRequest;
import com.google.pubsub.v1.Schema;
import com.google.pubsub.v1.SchemaServiceGrpc.SchemaServiceImplBase;
import com.google.pubsub.v1.ValidateMessageRequest;
import com.google.pubsub.v1.ValidateMessageResponse;
import com.google.pubsub.v1.ValidateSchemaRequest;
import com.google.pubsub.v1.ValidateSchemaResponse;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import javax.annotation.Generated;

@BetaApi
@Generated("by gapic-generator-java")
public class MockSchemaServiceImpl extends SchemaServiceImplBase {
  private List<AbstractMessage> requests;
  private Queue<Object> responses;

  public MockSchemaServiceImpl() {
    requests = new ArrayList<>();
    responses = new LinkedList<>();
  }

  public List<AbstractMessage> getRequests() {
    return requests;
  }

  public void addResponse(AbstractMessage response) {
    responses.add(response);
  }

  public void setResponses(List<AbstractMessage> responses) {
    this.responses = new LinkedList<Object>(responses);
  }

  public void addException(Exception exception) {
    responses.add(exception);
  }

  public void reset() {
    requests = new ArrayList<>();
    responses = new LinkedList<>();
  }

  @Override
  public void createSchema(CreateSchemaRequest request, StreamObserver<Schema> responseObserver) {
    Object response = responses.poll();
    if (response instanceof Schema) {
      requests.add(request);
      responseObserver.onNext(((Schema) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method CreateSchema, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  Schema.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void getSchema(GetSchemaRequest request, StreamObserver<Schema> responseObserver) {
    Object response = responses.poll();
    if (response instanceof Schema) {
      requests.add(request);
      responseObserver.onNext(((Schema) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method GetSchema, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  Schema.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void listSchemas(
      ListSchemasRequest request, StreamObserver<ListSchemasResponse> responseObserver) {
    Object response = responses.poll();
    if (response instanceof ListSchemasResponse) {
      requests.add(request);
      responseObserver.onNext(((ListSchemasResponse) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method ListSchemas, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  ListSchemasResponse.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void listSchemaRevisions(
      ListSchemaRevisionsRequest request,
      StreamObserver<ListSchemaRevisionsResponse> responseObserver) {
    Object response = responses.poll();
    if (response instanceof ListSchemaRevisionsResponse) {
      requests.add(request);
      responseObserver.onNext(((ListSchemaRevisionsResponse) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method ListSchemaRevisions, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  ListSchemaRevisionsResponse.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void commitSchema(CommitSchemaRequest request, StreamObserver<Schema> responseObserver) {
    Object response = responses.poll();
    if (response instanceof Schema) {
      requests.add(request);
      responseObserver.onNext(((Schema) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method CommitSchema, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  Schema.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void rollbackSchema(
      RollbackSchemaRequest request, StreamObserver<Schema> responseObserver) {
    Object response = responses.poll();
    if (response instanceof Schema) {
      requests.add(request);
      responseObserver.onNext(((Schema) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method RollbackSchema, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  Schema.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void deleteSchemaRevision(
      DeleteSchemaRevisionRequest request, StreamObserver<Schema> responseObserver) {
    Object response = responses.poll();
    if (response instanceof Schema) {
      requests.add(request);
      responseObserver.onNext(((Schema) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method DeleteSchemaRevision, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  Schema.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void deleteSchema(DeleteSchemaRequest request, StreamObserver<Empty> responseObserver) {
    Object response = responses.poll();
    if (response instanceof Empty) {
      requests.add(request);
      responseObserver.onNext(((Empty) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method DeleteSchema, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  Empty.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void validateSchema(
      ValidateSchemaRequest request, StreamObserver<ValidateSchemaResponse> responseObserver) {
    Object response = responses.poll();
    if (response instanceof ValidateSchemaResponse) {
      requests.add(request);
      responseObserver.onNext(((ValidateSchemaResponse) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method ValidateSchema, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  ValidateSchemaResponse.class.getName(),
                  Exception.class.getName())));
    }
  }

  @Override
  public void validateMessage(
      ValidateMessageRequest request, StreamObserver<ValidateMessageResponse> responseObserver) {
    Object response = responses.poll();
    if (response instanceof ValidateMessageResponse) {
      requests.add(request);
      responseObserver.onNext(((ValidateMessageResponse) response));
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError(((Exception) response));
    } else {
      responseObserver.onError(
          new IllegalArgumentException(
              String.format(
                  "Unrecognized response type %s for method ValidateMessage, expected %s or %s",
                  response == null ? "null" : response.getClass().getName(),
                  ValidateMessageResponse.class.getName(),
                  Exception.class.getName())));
    }
  }
}
