package com.l7tech.external.assertions.gatewaygraphql.server.resolver.mutation;

import com.l7tech.external.assertions.gatewaygraphql.EntityTypeInfo;
import com.l7tech.external.assertions.gatewaygraphql.server.data.EntityMappingInstruction;
import com.l7tech.external.assertions.gatewaygraphql.server.data.WebApiServiceDTO;
import com.l7tech.external.assertions.gatewaygraphql.server.input.EntityPropertyInput;
import com.l7tech.external.assertions.gatewaygraphql.server.util.Util;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.gateway.common.service.ServiceHeader;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.policy.Policy;
import com.l7tech.server.policy.PolicyVersionManager;
import com.l7tech.server.service.ServiceManager;
import graphql.schema.DataFetchingEnvironment;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import lombok.Generated;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.transaction.PlatformTransactionManager;

@Deprecated
public class WebApiServiceMutationResolver
   extends EntityManagerAwareGraphQLMutationResolver<PublishedService, ServiceHeader, ServiceManager, WebApiServiceDTO, WebApiServiceDTO.WebApiServiceInput> {
   @Generated
   private static final Logger b;
   private final PolicyVersionManager c;
   private static final String[] d;

   public WebApiServiceMutationResolver(PlatformTransactionManager var1, BeanFactory var2) {
      this(EntityTypeInfo.WEB_API_SERVICE, var1, var2);
   }

   public WebApiServiceMutationResolver(EntityTypeInfo var1, PlatformTransactionManager var2, BeanFactory var3) {
      String[] var4 = d;
      super(var1, var2, (ServiceManager)var3.getBean(var4[1], ServiceManager.class));
      this.c = (PolicyVersionManager)var3.getBean(var4[0], PolicyVersionManager.class);
   }

   @Override
   protected Logger getLogger() {
      return b;
   }

   public WebApiServiceDTO.WebApiServicesPayload setWebApiServices(List<WebApiServiceDTO.WebApiServiceInput> var1, DataFetchingEnvironment var2) {
      WebApiServiceDTO.WebApiServicesPayload var3 = new WebApiServiceDTO.WebApiServicesPayload();
      this.transformNoDefEntityMappings(var1, var2);
      return var1.isEmpty() ? var3 : this.execute(var2, (var3x, var4) -> {
         var1.forEach(var4x -> this.doUpsertSilently(var4x, var3x, var3, var4));
         return var3;
      });
   }

   public WebApiServiceDTO.WebApiServicesPayload deleteWebApiServices(List<String> var1, DataFetchingEnvironment var2) {
      WebApiServiceDTO.WebApiServicesPayload var3 = new WebApiServiceDTO.WebApiServicesPayload();
      return var1.isEmpty() ? var3 : this.execute(var2, (var3x, var4) -> {
         var1.forEach(var4x -> this.doDeleteSilently(var4x, var3x, var3, var4));
         return var3;
      });
   }

   protected WebApiServiceDTO doCreate(WebApiServiceDTO.WebApiServiceInput var1) throws ObjectModelException {
      WebApiServiceDTO var2 = (WebApiServiceDTO)super.doCreate(var1);
      this.c.checkpointPolicy(var2.entity().getPolicy(), true, true);
      return var2;
   }

   protected WebApiServiceDTO doUpdate(WebApiServiceDTO.WebApiServiceInput var1, PublishedService var2) throws ObjectModelException {
      WebApiServiceDTO var3 = (WebApiServiceDTO)super.doUpdate(var1, var2);

      try {
         if (Util.isPolicyVersioningEnabled()) {
            this.c.checkpointPolicy(var3.entity().getPolicy(), true, false);
         }

         return var3;
      } catch (ObjectModelException var4) {
         throw a(var4);
      }
   }

   protected PublishedService toEntity(WebApiServiceDTO.WebApiServiceInput var1) {
      return new PublishedService();
   }

   protected WebApiServiceDTO toEntityDTO(PublishedService var1) {
      return new WebApiServiceDTO(var1);
   }

   protected WebApiServiceDTO.WebApiServiceInput toEntityInput(EntityMappingInstruction var1) {
      WebApiServiceDTO.WebApiServiceInput var2 = new WebApiServiceDTO.WebApiServiceInput();
      var2.setResolutionPath(var1.getIdentityValue());
      return var2;
   }

   protected void setEntity(WebApiServiceDTO.WebApiServiceInput var1, PublishedService var2) {
      var2.setName(var1.getName());
      var2.setSoap(false);
      var2.setWssProcessingEnabled(false);
      var2.setHttpMethods(new HashSet<>(var1.getMethodsAllowed()));
      Policy var3 = var2.getPolicy();
      var3.setXml(var1.getPolicy().getXml());
      if (this.isNewEntity(var3)) {
         var3.setVersion(1);
         var3.setVersionActive(true);
         var3.setGuid(Util.getOrCreateGuid(var1.getGuid()));
      }

      var2.setRoutingUri(var1.getResolutionPath());
      var2.setFolder(this.getOrCreateFolderSafely(var1, var1.getFolderPath()));
      var2.setDisabled(!var1.getEnabled());
      var2.setTracingEnabled(var1.getTracingEnabled());
      var2.setWssProcessingEnabled(var1.getWssProcessingEnabled());
      if (var1.getProperties() != null) {
         var2.setProperties(var1.getProperties().stream().collect(Collectors.toMap(EntityPropertyInput::getName, EntityPropertyInput::getValue)));
      }
   }

   protected void resetEntitySelectively(WebApiServiceDTO.WebApiServiceInput var1, PublishedService var2) {
      var2.setProperties(new HashMap());
   }

   @Override
   protected boolean isFindMultipleEnabled() {
      return true;
   }

   protected PublishedService findByInput(WebApiServiceDTO.WebApiServiceInput var1) throws FindException {
      return this.b(var1.getResolutionPath());
   }

   protected Collection<PublishedService> findMultipleByInput(WebApiServiceDTO.WebApiServiceInput var1) throws FindException {
      return this.c(var1.getResolutionPath());
   }

   private PublishedService b(String var1) throws FindException {
      Collection var2 = this.c(var1);

      try {
         if (var2.size() == 1) {
            return this.a((PublishedService)var2.iterator().next());
         }
      } catch (FindException var3) {
         throw a(var3);
      }

      return null;
   }

   private Collection<PublishedService> c(String var1) throws FindException {
      return this.a(this.entityManager.findByRoutingUri(var1));
   }

   protected boolean isInterestedService(PublishedService var1) {
      return var1 != null && !var1.isSoap() && !var1.isInternal();
   }

   private PublishedService a(PublishedService var1) {
      return this.isInterestedService(var1) ? var1 : null;
   }

   private Collection<PublishedService> a(Collection<PublishedService> var1) {
      return var1.stream().filter(this::isInterestedService).collect(Collectors.toList());
   }

   static {
      String[] var5 = new String[2];
      int var3 = 0;
      String var2 = "\b~MZX\b\u0001\u001dcRZT\u001f\u001a\u0019\u007f@T^\u0003\u000e\u000btSER\u001225pOR\\\u0014%";
      int var4 = "\b~MZX\b\u0001\u001dcRZT\u001f\u001a\u0019\u007f@T^\u0003\u000e\u000btSER\u001225pOR\\\u0014%".length();
      char var1 = 20;
      int var0 = -1;

      while (true) {
         String var10002 = c(22, f(var2.substring(++var0, var0 + var1)));
         byte var10001 = -1;
         var5[var3++] = var10002;
         if ((var0 += var1) >= var4) {
            d = var5;
            b = Logger.getLogger(WebApiServiceMutationResolver.class.getName());
            return;
         }

         var1 = var2.charAt(var0);
      }
   }

   private static ObjectModelException a(ObjectModelException var0) {
      return var0;
   }

   private static char[] f(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 65);
      }

      return var10000;
   }

   private static String c(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 110;
            case 1 -> 7;
            case 2 -> 55;
            case 3 -> 37;
            case 4 -> 45;
            case 5 -> 103;
            default -> 65;
         });
      }

      return new String(var10001).intern();
   }
}
