package tv.pluto.utils;

import com.plutotv.core.enums.CoreEnvironments;
import com.plutotv.core.utils.CoreEnvironmentUtils;
import java.util.Arrays;
import java.util.Optional;
import org.springframework.stereotype.Component;

@Component
public class CustomCoreEnvironmentUtils extends CoreEnvironmentUtils {

  public String getCustomEnvironment(String url) {

    if (url.contains("pubste4")) return "PUBSTE4";
    else if (url.contains("pubste5")) return "PUBSTE5";

    Optional<CoreEnvironments> coreEnvironment = this.getEnvironmentHelper(url);
    CoreEnvironments environment;

    environment = coreEnvironment.orElse(CoreEnvironments.DEFAULT);

    return environment.toString().replace("_EKS", "");
  }

  private Optional<CoreEnvironments> getEnvironmentHelper(String url) {
    return Optional.of(
        Arrays.stream(CoreEnvironments.values())
            .filter((x) -> url.toLowerCase().contains(x.getPartialBaseUrl()))
            .findFirst()
            .orElseThrow(
                () ->
                    new RuntimeException(
                        "getEnvironment() :: The URL '"
                            + url
                            + "' provided did not adhere to Pluto TV's standardized naming convention.  URL naming conventions => *"
                            + CoreEnvironments.getPartialBaseUrls())));
  }
}
