/*
 * Copyright 2018 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.tasks.v2beta2;

import com.google.api.core.BetaApi;
import com.google.cloud.tasks.v2beta2.CloudTasksGrpc.CloudTasksImplBase;
import com.google.iam.v1.GetIamPolicyRequest;
import com.google.iam.v1.Policy;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.iam.v1.TestIamPermissionsRequest;
import com.google.iam.v1.TestIamPermissionsResponse;
import com.google.protobuf.Empty;
import com.google.protobuf.GeneratedMessageV3;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

@javax.annotation.Generated("by GAPIC")
@BetaApi
public class MockCloudTasksImpl extends CloudTasksImplBase {
  private ArrayList<GeneratedMessageV3> requests;
  private Queue<Object> responses;

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

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

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

  public void setResponses(List<GeneratedMessageV3> 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 listQueues(
      ListQueuesRequest request, StreamObserver<ListQueuesResponse> responseObserver) {
    Object response = responses.remove();
    if (response instanceof ListQueuesResponse) {
      requests.add(request);
      responseObserver.onNext((ListQueuesResponse) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void getQueue(
      GetQueueRequest request,
      StreamObserver<com.google.cloud.tasks.v2beta2.Queue> responseObserver) {
    Object response = responses.remove();
    if (response instanceof com.google.cloud.tasks.v2beta2.Queue) {
      requests.add(request);
      responseObserver.onNext((com.google.cloud.tasks.v2beta2.Queue) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void createQueue(
      CreateQueueRequest request,
      StreamObserver<com.google.cloud.tasks.v2beta2.Queue> responseObserver) {
    Object response = responses.remove();
    if (response instanceof com.google.cloud.tasks.v2beta2.Queue) {
      requests.add(request);
      responseObserver.onNext((com.google.cloud.tasks.v2beta2.Queue) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void updateQueue(
      UpdateQueueRequest request,
      StreamObserver<com.google.cloud.tasks.v2beta2.Queue> responseObserver) {
    Object response = responses.remove();
    if (response instanceof com.google.cloud.tasks.v2beta2.Queue) {
      requests.add(request);
      responseObserver.onNext((com.google.cloud.tasks.v2beta2.Queue) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void deleteQueue(DeleteQueueRequest request, StreamObserver<Empty> responseObserver) {
    Object response = responses.remove();
    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("Unrecognized response type"));
    }
  }

  @Override
  public void purgeQueue(
      PurgeQueueRequest request,
      StreamObserver<com.google.cloud.tasks.v2beta2.Queue> responseObserver) {
    Object response = responses.remove();
    if (response instanceof com.google.cloud.tasks.v2beta2.Queue) {
      requests.add(request);
      responseObserver.onNext((com.google.cloud.tasks.v2beta2.Queue) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void pauseQueue(
      PauseQueueRequest request,
      StreamObserver<com.google.cloud.tasks.v2beta2.Queue> responseObserver) {
    Object response = responses.remove();
    if (response instanceof com.google.cloud.tasks.v2beta2.Queue) {
      requests.add(request);
      responseObserver.onNext((com.google.cloud.tasks.v2beta2.Queue) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void resumeQueue(
      ResumeQueueRequest request,
      StreamObserver<com.google.cloud.tasks.v2beta2.Queue> responseObserver) {
    Object response = responses.remove();
    if (response instanceof com.google.cloud.tasks.v2beta2.Queue) {
      requests.add(request);
      responseObserver.onNext((com.google.cloud.tasks.v2beta2.Queue) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void getIamPolicy(GetIamPolicyRequest request, StreamObserver<Policy> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Policy) {
      requests.add(request);
      responseObserver.onNext((Policy) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void setIamPolicy(SetIamPolicyRequest request, StreamObserver<Policy> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Policy) {
      requests.add(request);
      responseObserver.onNext((Policy) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void testIamPermissions(
      TestIamPermissionsRequest request,
      StreamObserver<TestIamPermissionsResponse> responseObserver) {
    Object response = responses.remove();
    if (response instanceof TestIamPermissionsResponse) {
      requests.add(request);
      responseObserver.onNext((TestIamPermissionsResponse) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void listTasks(
      ListTasksRequest request, StreamObserver<ListTasksResponse> responseObserver) {
    Object response = responses.remove();
    if (response instanceof ListTasksResponse) {
      requests.add(request);
      responseObserver.onNext((ListTasksResponse) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void getTask(GetTaskRequest request, StreamObserver<Task> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Task) {
      requests.add(request);
      responseObserver.onNext((Task) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void createTask(CreateTaskRequest request, StreamObserver<Task> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Task) {
      requests.add(request);
      responseObserver.onNext((Task) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void deleteTask(DeleteTaskRequest request, StreamObserver<Empty> responseObserver) {
    Object response = responses.remove();
    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("Unrecognized response type"));
    }
  }

  @Override
  public void leaseTasks(
      LeaseTasksRequest request, StreamObserver<LeaseTasksResponse> responseObserver) {
    Object response = responses.remove();
    if (response instanceof LeaseTasksResponse) {
      requests.add(request);
      responseObserver.onNext((LeaseTasksResponse) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void acknowledgeTask(
      AcknowledgeTaskRequest request, StreamObserver<Empty> responseObserver) {
    Object response = responses.remove();
    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("Unrecognized response type"));
    }
  }

  @Override
  public void renewLease(RenewLeaseRequest request, StreamObserver<Task> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Task) {
      requests.add(request);
      responseObserver.onNext((Task) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void cancelLease(CancelLeaseRequest request, StreamObserver<Task> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Task) {
      requests.add(request);
      responseObserver.onNext((Task) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }

  @Override
  public void runTask(RunTaskRequest request, StreamObserver<Task> responseObserver) {
    Object response = responses.remove();
    if (response instanceof Task) {
      requests.add(request);
      responseObserver.onNext((Task) response);
      responseObserver.onCompleted();
    } else if (response instanceof Exception) {
      responseObserver.onError((Exception) response);
    } else {
      responseObserver.onError(new IllegalArgumentException("Unrecognized response type"));
    }
  }
}
