// 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;

    public static byte[] getOncoKBDataInBytes(String version, String fileName) throws HttpClientErrorException, IOException, NoPropertyException {
        GHBlob ghBlob = getGHBlob(version, fileName);
        return IOUtils.toByteArray(ghBlob.read());
    }

    private static GHBlob getGHBlob(String version, String fileName) throws IOException, NoPropertyException {
        GHRepository repo = getOncoKBDataRepo();
        List<GHContent> contents = new ArrayList<>();
        try {
            contents = repo.getDirectoryContent("/RELEASE/" + version);
        } catch (Exception e) {
            // in this case, is the directory is not available.
            throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
        }

        Optional<GHContent> matchedContent = contents.stream().filter(content -> content.getName().equals(fileName)).findFirst();
        if (matchedContent.isPresent()) {
            String sha = matchedContent.get().getSha();
            GHBlob ghBlob = repo.getBlob(sha);
            return ghBlob;
        } else {
            throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
        }
    }

    public static String getOncoKBSqlDumpFileName(String version) {
        if (version == null) {
            return "";
        }
// 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;


    public static byte[] getOncoKBDataInBytes(String version, String fileName) throws HttpClientErrorException, IOException, NoPropertyException {
        GHBlob ghBlob = getGHBlob(version, fileName);
        return IOUtils.toByteArray(ghBlob.read());
    }

    private static GHBlob getGHBlob(String version, String fileName) throws IOException, NoPropertyException {
        GHRepository repo = getOncoKBDataRepo();
        List<GHContent> contents = new ArrayList<>();
        try {
            contents = repo.getDirectoryContent("/RELEASE/" + version);
        } catch (Exception e) {
            // in this case, is the directory is not available.
            throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
        }

        Optional<GHContent> matchedContent = contents.stream().filter(content -> content.getName().equals(fileName)).findFirst();
        if (matchedContent.isPresent()) {
            String sha = matchedContent.get().getSha();
            GHBlob ghBlob = repo.getBlob(sha);
            return ghBlob;
        } else {
            throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
        }
    }

    public static String getOncoKBSqlDumpFileName(String version) {
        if (version == null) {
            return "";
        }
        return "oncokb_" + version.replace(".", "_") + ".sql" + FileExtension.GZ.getExtension();