package tv.pluto.grpc.featured_group;

import io.grpc.Channel;
import java.time.Instant;
import java.util.List;
import tv.pluto.grpc.GenericGrpcClient;
import tv.pluto.ip.dal.featuredgroup.FeaturedGroupManagerGrpc;
import tv.pluto.ip.dal.featuredgroup.FeaturedGroups;
import tv.pluto.mongo.entities.featuredgroup.FeaturedGroup;

public class FeaturedGroupClient extends GenericGrpcClient {
  private FeaturedGroupManagerGrpc.FeaturedGroupManagerBlockingStub blockingStub;
  private FeaturedGroups.GetAllPagedRequest.Builder getAllPagedBuilder =
      FeaturedGroups.GetAllPagedRequest.newBuilder();

  public FeaturedGroupClient(Channel channel, boolean isAuth) {
    blockingStub = FeaturedGroupManagerGrpc.newBlockingStub(channel);

    if (isAuth) blockingStub = blockingStub.withInterceptors(CLIENT_INTERCEPTOR_AUTH);
    else blockingStub = blockingStub.withInterceptors(CLIENT_INTERCEPTOR);
  }

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

  public FeaturedGroups.CreateResponse create(FeaturedGroups.CreateRequest createRequest) {
    return blockingStub.create(createRequest);
  }

  public FeaturedGroups.UpdateFieldsResponse updateById(
      FeaturedGroups.UpdateFieldsRequest request) {
    return blockingStub.updateFields(request);
  }

  public FeaturedGroups.CreateRequest getNewFeaturedGroup(
      FeaturedGroup featuredGroupDb, Boolean published, Boolean withConteNtItems) {
    Instant time = Instant.now().minusSeconds(990);
    Instant endTime = Instant.now();
    FeaturedGroups.FeaturedGroup.Builder build = FeaturedGroups.FeaturedGroup.newBuilder();

    if (published != null) {
      build.setPublished(published);
    }

    if (withConteNtItems) {
      FeaturedGroups.ContentItem.Builder contentBuilder =
          FeaturedGroups.ContentItem.newBuilder()
              .setContentType("channel")
              .setContent("659ded58b9adc400084366e2")
              .setPartnerGrid(true)
              .setType("Branded")
              .setStrategy("Espanol")
              .setNotes("notes");
      build.addContentItems(contentBuilder);
    }

    FeaturedGroups.FeaturedGroup featuredGroup =
        build
            .setActiveRegion(featuredGroupDb.getActiveRegion())
            .setName(featuredGroupDb.getName())
            .setAuthor(featuredGroupDb.getAuthor())
            .setStart(time.toString())
            .setEnd(endTime.toString())
            .setSlug(featuredGroupDb.getSlug())
            .build();

    return FeaturedGroups.CreateRequest.newBuilder().setDocument(featuredGroup).build();
  }

  public FeaturedGroups.DeleteByIdsResponse deleteByIds(List<String> listIds) {
    FeaturedGroups.DeleteByIdsRequest request =
        FeaturedGroups.DeleteByIdsRequest.newBuilder().addAllIds(listIds).build();
    return blockingStub.deleteByIds(request);
  }

  public FeaturedGroups.FeaturedGroup getUnpublished(String id) {
    FeaturedGroups.UnpublishFeaturedGroupRequest request =
        FeaturedGroups.UnpublishFeaturedGroupRequest.newBuilder().setId(id).build();
    return blockingStub.unpublishFeaturedGroup(request).getDocument();
  }

  public FeaturedGroups.FeaturedGroup getPublished(String id) {
    FeaturedGroups.PublishFeaturedGroupRequest request =
        FeaturedGroups.PublishFeaturedGroupRequest.newBuilder().setId(id).build();
    return blockingStub.publishFeaturedGroup(request).getDocument();
  }

  public FeaturedGroups.GetOneByFilterResponse getOneByFilter(
      String id,
      List<String> excludeIds,
      String minStartDate,
      List<FeaturedGroups.Filter> filters) {
    FeaturedGroups.GetOneByFilterRequest.Builder build =
        FeaturedGroups.GetOneByFilterRequest.newBuilder();

    if (!id.isEmpty()) build.setId(id);

    if (excludeIds != null) build.addAllExcludeIds(excludeIds);

    if (!minStartDate.isEmpty()) build.setMinStartDate(minStartDate);

    if (filters != null) build.addAllFilters(filters);

    return blockingStub.getOneByFilter(build.build());
  }

  public FeaturedGroups.GetAllPagedSort getSort(String field, int value) {
    return FeaturedGroups.GetAllPagedSort.newBuilder().setField(field).setValue(value).build();
  }

  public FeaturedGroups.FeaturedGroup getTestFeatureGroup(String name, String start) {
    FeaturedGroups.FeaturedGroup createdFeature =
        FeaturedGroups.FeaturedGroup.newBuilder()
            .setName(name)
            .setStart(start)
            .setActiveRegion("ZA")
            // should be a valid author email
            .setAuthor("martin.bascal@pluto.tv")
            .build();

    FeaturedGroups.CreateRequest createRequest =
        FeaturedGroups.CreateRequest.newBuilder().setDocument(createdFeature).build();

    return create(createRequest).getDocument();
  }
}
