package io.mixeway.scanner.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.mixeway.scanner.integrations.model.*;
import org.apache.commons.lang3.StringUtils;

/**
 * @author gsiewruk
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Vulnerability {
    private String name;
    private ScannerType scannerType;
    private String description;
    private String filename;
    private String line;
    private String severity;
    private String packageName;
    private String packageVersion;
    private String recomendations;
    private String references;
    private String category;

    /**
     * for Bandit integration
     * @param banditResult bandit report
     */
    public Vulnerability(BanditResult banditResult) {
        this.name = banditResult.getTest_name();
        this.description = banditResult.getIssue_text();
        this.filename = banditResult.getFilename();
        this.severity = StringUtils.capitalize(banditResult.getIssue_severity());
        this.line = banditResult.getLine_number();
        this.references = banditResult.getMore_info();
        this.scannerType = ScannerType.SAST;
    }

    /**
     * For Dependency Track Integraiton
     * @param dTrackVuln dTrack report
     */
    public Vulnerability(DTrackVuln dTrackVuln) {
        this.name = dTrackVuln.getVulnId();
        this.scannerType = ScannerType.OPENSOURCE;
        this.packageName = dTrackVuln.getComponents().stream().findFirst().orElse(null).getName();
        this.packageVersion = dTrackVuln.getComponents().stream().findFirst().orElse(null).getVersion();
        this.severity = dTrackVuln.getSeverity();
        this.description = dTrackVuln.getDescription();
        this.references = dTrackVuln.getReferences();
        this.recomendations = dTrackVuln.getRecommendation();
    }

    /**
     * For spotbug integration
     * @param bugInstance spotbug report
     */
    public Vulnerability(BugInstance bugInstance, BugPattern bugPattern) {
        this.name = bugPattern != null? bugPattern.getShortDescriptions() : bugInstance.getShortDescription();
        this.severity = getSeverityFromBugInstanceRank(bugInstance.getRank());
        this.filename = bugInstance.getSourceLine().getSourcepath();
        this.line = bugInstance.getSourceLine().getStart();
        this.description = bugPattern != null? bugPattern.getDetails() : bugInstance.getLongMessage();
        this.scannerType = ScannerType.SAST;
        this.category = bugInstance.getCategory();
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    private String getSeverityFromBugInstanceRank(String rank) {
        int rankValue = Integer.parseInt(rank);
        String severity = "Info";
        if (rankValue < 5){
            severity = "Critical";
        } else if (rankValue >=5 && rankValue < 10){
            severity = "High";
        } else if ( rankValue >= 10 && rankValue < 15){
            severity = "Medium";
        } else {
            severity = "Low";
        }
        return severity;
    }

    /**
     * For Progpilot integration
     * @param progPilotVuln
     */
    public Vulnerability(ProgPilotVuln progPilotVuln) {
        this.name = progPilotVuln.getVuln_name();
        this.scannerType = ScannerType.SAST;
        this.line = String.valueOf(progPilotVuln.getSource_line()[0]);
        this.filename =progPilotVuln.getSource_file()[0];
    }

    public String getRecomendations() {
        return recomendations;
    }

    public void setRecomendations(String recomendations) {
        this.recomendations = recomendations;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ScannerType getScannerType() {
        return scannerType;
    }

    public void setScannerType(ScannerType scannerType) {
        this.scannerType = scannerType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getLine() {
        return line;
    }

    public void setLine(String line) {
        this.line = line;
    }

    public String getSeverity() {
        return severity;
    }

    public void setSeverity(String severity) {
        this.severity = severity;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getPackageVersion() {
        return packageVersion;
    }

    public void setPackageVersion(String packageVersion) {
        this.packageVersion = packageVersion;
    }

    public String getReferences() {
        return references;
    }

    public void setReferences(String references) {
        this.references = references;
    }
}
