import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Core
{
  private static String IMAGE_DESTINATION_FOLDER = ;
  
  public static class pathFile { public pathFile() {}
    
    public static void mainWrite() { try { File file = new File("path.txt");
        file.createNewFile();
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        if ((Core.Bundle.getPath() != null) && (Core.Bundle.getPath() != "")) {
          bw.write(Core.Bundle.getPath());
          bw.flush();
          bw.close();
          reading.setRead(Core.Bundle.getPath());
        }
        System.out.println(file.createNewFile());
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    public static void mainRead() {
      try {
        File file = new File("path.txt");
        file.createNewFile();
        java.io.FileReader fr = new java.io.FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String read; if ((read = br.readLine()) != null) {
          Core.Bundle.setPath(read);
          System.out.println(read);
          Interface.t1.setText(read);
        }
        br.close();
      } catch (IOException e) {
        e.printStackTrace(); } }
    
    public static class reading { public static String read;
      
      public reading() {}
      
      public static String getRead() { Core.pathFile.mainWrite();
        return read;
      }
      
      public static void setRead(String reads) { read = reads; }
    } }
  
  public Core() {}
  
  public static class Bundle { public static String path1;
    public static String spath;
    
    public Bundle() {}
    
    public static String getPath() { return path1; }
    
    public static void setPath(String path) {
      path1 = path;
    }
    
    public static String getSPath() { return spath; }
    
    public static void setSPath(String spath1) {
      spath = spath1;
    }
    
    public static boolean getPage() { return page; }
    public static String strURL;
    
    public static void setPage(boolean spage) { page = spage; }
    
    public static String getStrURL(Element imageElement) {
      strURL = getPage() ? imageElement.attr("abs:href") : imageElement.attr("abs:src");
      return strURL; }
    
    public static boolean page; }
  
  public static void makeFolder(String lines, int i) { String path = lines.substring(lines.lastIndexOf("/") + 1);
    String[] pathf = path.split(".html");
    String spath = pathf[0];
    System.out.println("1");
    Bundle.setSPath(spath);
    System.out.println("2");
    File theDir = new File(Bundle.getPath() + "/" + spath);
    System.out.println("3");
    if (!theDir.exists()) {
      System.out.println("creating directory: " + theDir.getName());
      boolean result = false;
      try {
        theDir.mkdir();
        result = true;
      }
      catch (SecurityException localSecurityException) {}
      if (result)
        System.out.println("DIR created");
    }
  }
  
  public static void mainSoup(String strURL) throws IOException, InterruptedException {
    System.setProperty("http.agent", "Mozilla/5.0");
    Document document = org.jsoup.Jsoup.connect(strURL).timeout(100000).get();
    org.jsoup.select.Elements imageElements = null;
    if (Bundle.getPage() == true) {
      imageElements = document.select("a[href$=.gif], a[href$=.jpg], a[href$=.png], a[href$=.bmp], a[href$=.webm]");
    } else if (!Bundle.getPage()) {
      imageElements = document.select("img");
    }
    for (Element imageElement : imageElements) {
      String strImageURL = Bundle.getStrURL(imageElement);
      downloadImage(strImageURL);
    }
  }
  
  private static void downloadImage(String strImageURL) { String strImageName = strImageURL.substring(strImageURL.lastIndexOf("/") + 1);
    System.out.println("Saving: " + strImageName + ", from: " + strImageURL);
    try {
      URL urlImage = new URL(strImageURL);
      InputStream in = urlImage.openStream();
      byte[] buffer = new byte['က'];
      int n = -1;
      OutputStream os = new java.io.FileOutputStream(IMAGE_DESTINATION_FOLDER + "/" + Bundle.getSPath() + "/" + strImageName);
      while ((n = in.read(buffer)) != -1) {
        os.write(buffer, 0, n);
      }
      os.close();
      System.out.println("Image saved");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}