<%@ page import="java.io.*, java.util.*, java.security.*" %>
<%@ page import="com.google.gson.Gson" %>


<%!
public String getPluginFilePath(String rFilePath){
      return "/plugins/" + rFilePath;
}
public String getRealPath(ServletContext application, String path) {
    return application.getRealPath(getPluginFilePath(path));
}
public boolean ifFileExist(ServletContext application, String path) {
    String filePath = application.getRealPath(path);
    if (filePath == null) return false; // Prevent NullPointerException
    File file = new File(filePath);
    return file.exists() && file.isFile();
}
public boolean isVitaraMapsPresent(ServletContext application) {
    String vitaraMapsPath = application.getRealPath(getPluginFilePath("VitaraMaps/"));;
    if (vitaraMapsPath != null) {
        File vitaraMapsDir = new File(vitaraMapsPath);
        return vitaraMapsDir.exists() && vitaraMapsDir.isDirectory();
    }
    return false;
}
public boolean isVitaraChartsPresent(ServletContext application) {
    String vitaraChartsPath = application.getRealPath(getPluginFilePath("VitaraCharts/"));;
    if (vitaraChartsPath != null) {
        File vitaraChartsDir = new File(vitaraChartsPath);
        return vitaraChartsDir.exists() && vitaraChartsDir.isDirectory();
    }
    return false;
}
public boolean isPluginPresent(ServletContext application, String plugin) {
    String pluginPath = application.getRealPath(getPluginFilePath(plugin));;
    if (pluginPath != null) {
        File pluginDir = new File(pluginPath);
        return pluginDir.exists() && pluginDir.isDirectory();
    }
    return false;
}
public Properties readStateProperties(String path) {
    Properties prop = new Properties();
    try (InputStream input = new FileInputStream(path)) {
        prop.load(input);
    } catch (IOException e) {
        //e.printStackTrace();
    }
    return prop;
}
public String getMD5Checksum(String filePath) throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    FileInputStream fis = new FileInputStream(new File(filePath));
    

    byte[] dataBytes = new byte[1024];
    int bytesRead;
    
    while ((bytesRead = fis.read(dataBytes)) != -1) {
        md.update(dataBytes, 0, bytesRead);
    }
    fis.close();
    
    byte[] mdBytes = md.digest();
    StringBuilder sb = new StringBuilder();
    for (byte b : mdBytes) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}
Map<String, String> resolutionMessages = new HashMap<String, String>() {{
    put("E01", "Incomplete Deployment: Missing Files, Please redeploy the plugin.");
    put("E02", "Unclean Deployment: Duplicate Files Found, Delete the existing plugin & redeploy.");
    put("E03", "Unclean Deployment: Wrong Folders Found, Delete the existing plugin & redeploy.");
    put("E04", "Missing Files: Please redeploy the plugin.");
    put("E05", "Files Damaged: Please redeploy the plugin.");
    put("E06", "Missing License file: Please add License.txt file with correct license");
    put("E07", "Contact Vitara Support for license");
    put("E08", "Please Add Correct build for this platform");
    put("E09", "Incorrect Map plugin Deployed. Please deploy platform-specific build");
    put("E10", "Incorrect Charts plugin Deployed. Please deploy platform-specific build");
    put("E11", "Please deploy same version of Map & Charts plugins");
    put("E12", "custom css file not present. please redeploy plugin");
    put("E13", "global.txt file not present. please redeploy plugin");
    put("E14", "Verify custom configuration files. if changes aren't done by you please redeploy plugin");
}};
%>
