// Path: core/src/main/java/org/mskcc/cbio/oncokb/apiModels/download/FileExtension.java
// public enum FileExtension {
//     JSON(".json"), TEXT(".txt"), MARK_DOWN(".md"), GZ(".gz");
//     String extension;
// 
//     FileExtension(String extension) {
//         this.extension = extension;
//     }
// 
//     public String getExtension() {
//         return extension;
//     }
// 
//     public void setExtension(String extension) {
//         this.extension = extension;
//     }
// }
// 
// Path: core/src/main/java/org/mskcc/cbio/oncokb/util/FileUtils.java
// public static String getFileName(FileName fileName, FileExtension fileExtension) {
//     return fileName.getName() + fileExtension.getExtension();
// }


import com.sun.mail.iap.ByteArray;
import org.apache.commons.io.IOUtils;
import org.kohsuke.github.GHBlob;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.mskcc.cbio.oncokb.apiModels.download.DownloadAvailability;
import org.mskcc.cbio.oncokb.apiModels.download.FileExtension;
import org.mskcc.cbio.oncokb.apiModels.download.FileName;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.mskcc.cbio.oncokb.util.FileUtils.getFileName;
    private static Boolean checkFileNameExists(List<GHContent> files, String fileName, boolean ignoreCase) {
        return files
            .stream()
            .filter(file -> ignoreCase ? file.getName().equalsIgnoreCase(fileName) : file.getName().equals(fileName))
            .findFirst()
            .isPresent();
    }

    private static Boolean checkSqlDumpExists(List<GHContent> files, String version) {
        return files
            .stream()
            .filter(file -> file.getName().startsWith(getOncoKBSqlDumpFileName(version)))
            .findFirst()
            .isPresent();
    }

    public static List<DownloadAvailability> getDownloadAvailability() throws HttpClientErrorException, IOException, NoPropertyException {
        List<DownloadAvailability> downloadAvailabilities = new ArrayList<>();
        GHRepository repo = getOncoKBDataRepo();
        List<GHContent> versions = repo.getDirectoryContent("/RELEASE/");
        for (GHContent version : versions) {
            List<GHContent> files = new ArrayList<>();
            try {
                files = repo.getDirectoryContent("/RELEASE/" + version.getName());
            } catch (Exception e) {
                // Preventing the item is not a directory, but we want to continue indexing the items.
                continue;
            }
            DownloadAvailability downloadAvailability = new DownloadAvailability(version.getName());

// Path: core/src/main/java/org/mskcc/cbio/oncokb/apiModels/download/FileExtension.java
// public enum FileExtension {
//     JSON(".json"), TEXT(".txt"), MARK_DOWN(".md"), GZ(".gz");
//     String extension;
// 
//     FileExtension(String extension) {
//         this.extension = extension;
//     }
// 
//     public String getExtension() {
//         return extension;
//     }
// 
//     public void setExtension(String extension) {
//         this.extension = extension;
//     }
// }
// 
// Path: core/src/main/java/org/mskcc/cbio/oncokb/util/FileUtils.java
// public static String getFileName(FileName fileName, FileExtension fileExtension) {
//     return fileName.getName() + fileExtension.getExtension();
// }


// Path: core/src/main/java/org/mskcc/cbio/oncokb/util/GitHubUtils.java
import com.sun.mail.iap.ByteArray;
import org.apache.commons.io.IOUtils;
import org.kohsuke.github.GHBlob;
import org.kohsuke.github.GHContent;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.mskcc.cbio.oncokb.apiModels.download.DownloadAvailability;
import org.mskcc.cbio.oncokb.apiModels.download.FileExtension;
import org.mskcc.cbio.oncokb.apiModels.download.FileName;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.mskcc.cbio.oncokb.util.FileUtils.getFileName;

    private static Boolean checkFileNameExists(List<GHContent> files, String fileName, boolean ignoreCase) {
        return files
            .stream()
            .filter(file -> ignoreCase ? file.getName().equalsIgnoreCase(fileName) : file.getName().equals(fileName))
            .findFirst()
            .isPresent();
    }

    private static Boolean checkSqlDumpExists(List<GHContent> files, String version) {
        return files
            .stream()
            .filter(file -> file.getName().startsWith(getOncoKBSqlDumpFileName(version)))
            .findFirst()
            .isPresent();
    }

    public static List<DownloadAvailability> getDownloadAvailability() throws HttpClientErrorException, IOException, NoPropertyException {
        List<DownloadAvailability> downloadAvailabilities = new ArrayList<>();
        GHRepository repo = getOncoKBDataRepo();
        List<GHContent> versions = repo.getDirectoryContent("/RELEASE/");
        for (GHContent version : versions) {
            List<GHContent> files = new ArrayList<>();
            try {
                files = repo.getDirectoryContent("/RELEASE/" + version.getName());
            } catch (Exception e) {
                // Preventing the item is not a directory, but we want to continue indexing the items.
                continue;
            }
            DownloadAvailability downloadAvailability = new DownloadAvailability(version.getName());

            if (checkFileNameExists(files, getFileName(FileName.ALL_ANNOTATED_VARIANTS, FileExtension.TEXT))) {