package org.aguinore;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

class Downloader {
    private final String pathToFiles;

    Downloader(String pathToFiles) {
        this.pathToFiles = pathToFiles;
    }

    void saveDocs() {
        File baseDir = new File(pathToFiles);
        if (!baseDir.isDirectory()) {
            throw new IllegalArgumentException("Provided path must be a directory");
        }

        File[] files = baseDir.listFiles();
        if (files == null) {
            throw new IllegalArgumentException("Provided path must be a nonempty directory");
        }

        for (File file : files) {
            if (file.isFile() && file.getName().endsWith(".html")) {
                parseHtmlFile(file.getAbsolutePath());
            }
        }
    }

    private void parseHtmlFile(String fileName) {
        System.out.println(fileName);
        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = br.readLine()) != null) {
               parseHtmlLine(line);
            }
        } catch (FileNotFoundException ex) {
            System.err.println("File not found " + fileName);
        } catch (IOException ex) {
            System.err.println("Something got wrong with file " + fileName + ": ");
            ex.printStackTrace();
        }
    }

    private void parseHtmlLine(String line) {
        String docLink;
        String pattern = "iframe\" src=\"";
        int index = line.indexOf(pattern);
        if (index > 0) {
            int questionMarkIndex = line.indexOf("?");
            if (questionMarkIndex > 0) {
                docLink = line.substring(index + pattern.length(), questionMarkIndex);
                int startOfFileName = docLink.lastIndexOf("/") + 1;
                String fileName = docLink.substring(startOfFileName);
                System.out.println(docLink);
                saveDoc(docLink, fileName);
            }
        }
    }

    private void saveDoc(String docLink, String fileNameTosave) {
        URL url = null;

        try {
            url = new URL(docLink);
            InputStream in = url.openStream();
            String placeToSave = pathToFiles + File.separator + Paths.get(fileNameTosave);
            System.out.println("saving to " + placeToSave);
            Files.copy(in, Paths.get(placeToSave), StandardCopyOption.REPLACE_EXISTING);
            in.close();
        } catch (MalformedURLException mue) {
            System.err.println("URL not found " + docLink);
        } catch (IOException ioe) {
            System.err.println("Something got wrong with opening stream from url " + url.getPath() + ": ");
            ioe.printStackTrace();
        }
    }
}

@@ -190,16 +190,64 @@
 yOption;
+%0Aimport java.util.HashMap;%0Aimport java.util.Map;
 %0A%0Aclass 
@@ -1984,32 +1984,43 @@
 ring fileName = 
+renameFile(
 docLink.substrin
@@ -2029,32 +2029,33 @@
 startOfFileName)
+)
 ;%0A              
@@ -2135,32 +2135,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -2152,32 +2152,420 @@
        %7D%0A    %7D%0A%0A
+    private String renameFile(String fileName) %7B%0A        String stopThePress = %22_vk_com_stopthepress%22;%0A        String correctedName = fileName.replace(stopThePress, %22%22);%0A        while(correctedName.contains(%22__%22)) %7B%0A            correctedName = correctedName.replaceAll(%22__%22, %22_%22);%0A        %7D%0A        correctedName = correctedName.replaceAll(%22_%22, %22 %22);%0A        return correctedName;%0A    %7D%0A%0A
     private void

rename file. remove "stop_the_press" mark, replace "_" to " "