/*
 * (c) 2020 Pluto Inc.
 */
package tv.pluto.grpc.vod;

import static tv.pluto.ip.dal.vod.Vod.CFeedsSyncAllIdsRequest;
import static tv.pluto.ip.dal.vod.Vod.CFeedsSyncDocumentsRequest;
import static tv.pluto.ip.dal.vod.Vod.GetActiveEpisodeIdsForCategoryIdRequest;
import static tv.pluto.ip.dal.vod.Vod.GetActiveEpisodeIdsForCategoryIdResponse;
import static tv.pluto.ip.dal.vod.Vod.GetActiveVodCategoryIdsRequest;
import static tv.pluto.ip.dal.vod.Vod.GetActiveVodCategoryIdsResponse;
import static tv.pluto.ip.dal.vod.Vod.GetDocumentsUpdatedBetweenDatesRequest;
import static tv.pluto.ip.dal.vodcategory.VodCategoryOuterClass.GetAllPagedSort;
import static tv.pluto.ip.dal.vodcategory.VodCategoryOuterClass.GetByIdRequest;
import static tv.pluto.ip.dal.vodcategory.VodCategoryOuterClass.GetByIdResponse;
import static tv.pluto.ip.dal.vodcategory.VodCategoryOuterClass.GetByIdsRequest;
import static tv.pluto.ip.dal.vodcategory.VodCategoryOuterClass.GetByIdsResponse;

import io.grpc.Channel;
import java.util.List;
import java.util.logging.Logger;
import tv.pluto.grpc.GenericGrpcClient;
import tv.pluto.ip.dal.vod.Vod;
import tv.pluto.ip.dal.vod.VodManagerGrpc;
import tv.pluto.ip.dal.vodcategory.VodCategoryManagerGrpc;

/** Class represents gRPC client for <b>vod.proto</b> file */
public class VodClient extends GenericGrpcClient {
  private static final Logger logger = Logger.getLogger(VodClient.class.getName());

  private VodManagerGrpc.VodManagerBlockingStub blockingStub;
  private VodCategoryManagerGrpc.VodCategoryManagerBlockingStub blockingStubNew;

  public VodClient(Channel channel) {
    this.getDocumentsUpdatedBetweenDatesRequestBuilder =
        GetDocumentsUpdatedBetweenDatesRequest.newBuilder();
    this.getCFeedsAllIdsBuilder = CFeedsSyncAllIdsRequest.newBuilder();
    this.getCFeedsSyncDocumentsBuilder = CFeedsSyncDocumentsRequest.newBuilder();
    blockingStub = VodManagerGrpc.newBlockingStub(channel);
    blockingStub = blockingStub.withInterceptors(CLIENT_INTERCEPTOR);
    blockingStubNew = VodCategoryManagerGrpc.newBlockingStub(channel);
    blockingStubNew = blockingStubNew.withInterceptors(CLIENT_INTERCEPTOR);
  }

  /**
   * Method represents gRPC method GetActiveVodCategoryIds from <b>vod.proto</b> file
   *
   * @param regionId active region id
   * @return GetActiveVodCategoryIdsResponse object
   */
  public GetActiveVodCategoryIdsResponse getActiveVodCategoryIds(String regionId) {
    logger.info("Getting info for vod category with active region id: " + regionId + " ...");
    GetActiveVodCategoryIdsRequest request =
        GetActiveVodCategoryIdsRequest.newBuilder().setActiveRegion(regionId).build();

    return blockingStub.getActiveVodCategoryIds(request);
  }

  /**
   * Method represents gRPC method GetActiveEpisodeIdsForCategoryId from <b>vod.proto</b> file
   *
   * @param categoryId category id
   * @return GetActiveEpisodeIdsForCategoryIdResponse object
   */
  public GetActiveEpisodeIdsForCategoryIdResponse getActiveEpisodeIdsForCategoryId(
      String categoryId) {
    logger.info("Getting info for active episodes with category id: " + categoryId + " ...");
    GetActiveEpisodeIdsForCategoryIdRequest request =
        GetActiveEpisodeIdsForCategoryIdRequest.newBuilder().setId(categoryId).build();

    return blockingStub.getActiveEpisodeIdsForCategoryId(request);
  }

  public GetByIdResponse getById(String id) {
    GetByIdRequest request = GetByIdRequest.newBuilder().setId(id).build();

    return blockingStubNew.getById(request);
  }

  public Vod.GetActiveEpisodesForCategoryIdResponse getActiveEpisodesForCategoryId(String id) {
    Vod.GetActiveEpisodesForCategoryIdRequest request =
        Vod.GetActiveEpisodesForCategoryIdRequest.newBuilder().setId(id).build();

    return blockingStub.getActiveEpisodesForCategoryId(request);
  }

  public GetByIdsResponse getByIds(List<String> ids, List<GetAllPagedSort> sort) {
    GetByIdsRequest request = GetByIdsRequest.newBuilder().addAllIds(ids).addAllSort(sort).build();

    return blockingStubNew.getByIds(request);
  }

  public Vod.BulkDeviceUpdateResponse bulkdDeviceUpdate(
      List<String> devices,
      List<String> includeGroup,
      List<String> excludeGroup,
      List<String> removeGroup) {
    Vod.BulkDeviceUpdateRequest.Builder builder = Vod.BulkDeviceUpdateRequest.newBuilder();

    if (devices != null) builder.addAllDevices(devices);

    if (includeGroup != null) builder.addAllIncludeGroup(includeGroup);

    if (excludeGroup != null) builder.addAllExcludeGroup(excludeGroup);

    if (removeGroup != null) builder.addAllRemoveGroup(removeGroup);

    return blockingStub.bulkDeviceUpdate(builder.build());
  }
}
