package cz.mrq.vocloud.managedbeans;

import cz.mrq.vocloud.helper.EditPanel;
import org.apache.commons.io.FileUtils;
import org.primefaces.component.panel.Panel;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.zeroturnaround.zip.ZipUtil;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * CreateSomJob
 *
 * @author Lumir Mrkva (lumir.mrkva@topmonks.com)
 */
@ManagedBean(name = "createSomJob")
@ViewScoped
public class CreateSomJob extends CreateJob {

    File parameters;

    Boolean zip = false;
    Boolean config = false;
    EditPanel editPanel = new EditPanel();

    static String CONFIG_FILE = "config.json";

    @Override
    public String getJobType() {
        return "SOM";
    }

    @Override
    public void postInit() {
        List<File> files = new ArrayList<>();
        for (File file : uploadedFiles) {
            if (file.getName().endsWith(".zip") && !file.getName().equals("results.zip")) {
                if (zip = ZipUtil.containsEntry(file, CONFIG_FILE)) {
                    parameters = file;
                    files.add(file);
                }
            }
            if (config = file.getName().equals(CONFIG_FILE)) {
                editPanel.setFileContents(file);
            }
        }
        uploadedFiles = files;
    }

    @Override
    protected File prepareParameters() {
        ZipUtil.replaceEntry(parameters, CONFIG_FILE, editPanel.getFileContents().getBytes());
        return parameters;
    }

    @Override
    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile uploaded = event.getFile();
        if (uploaded.getFileName().endsWith(".zip")) {
            File file = new File(uploadDir, uploaded.getFileName());
            copyUploadedFile(file, uploaded);
            if (zip = ZipUtil.containsEntry(file, CONFIG_FILE)) {
                uploadedFiles.add(file);
                parameters = file;
                String config = new String(ZipUtil.unpackEntry(file, CONFIG_FILE));
                editPanel.setFileContents(config);
            }
        }
    }

    @Override
    public void handleSave() {

    }

    public Boolean getZip() {
        return zip;
    }

    public EditPanel getEditPanel() {
        return editPanel;
    }
}

@@ -1022,24 +1022,73 @@
 ayList%3C%3E();%0A
+        editPanel.getPanel().setCollapsed(true);%0A
         for 
@@ -1342,22 +1342,84 @@
     
-files.add(file
+editPanel.setFileContents(new String(ZipUtil.unpackEntry(file, CONFIG_FILE))
 );%0A 
@@ -1421,34 +1421,24 @@
 ));%0A        
-        %7D%0A
             
@@ -1441,121 +1441,42 @@
     
-%7D%0A            if (config = file.getName().equals(CONFIG_FILE)) %7B%0A                editPanel.setFileContents(file);
+files.add(file);%0A                %7D
 %0A   
@@ -1585,24 +1585,94 @@
 ameters() %7B%0A
+        File params = new File(getJobFolder(), parameters.getName());%0A
         ZipU
@@ -1693,20 +1693,16 @@
 ry(param
-eter
 s, CONFI
@@ -2161,31 +2161,25 @@
     
-uploadedFiles.add(
+parameters = 
 file
-)
 ;%0A  
@@ -2184,32 +2184,50 @@
                 
+uploadedFiles.add(
 parameters = fil
@@ -2212,39 +2212,33 @@
 s.add(parameters
- = file
+)
 ;%0A              
@@ -2243,24 +2243,34 @@
     
-String config = 
+editPanel.setFileContents(
 new 
@@ -2319,58 +2319,8 @@
 LE))
-;%0A                editPanel.setFileContents(config
 );%0A 

save config to job folder