package ajcon.helpers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class DocWriter {
    Map<String, Object> createdObjects;
    String fileName;
    String outputDirectory;
    String html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:h=\"http://java.sun.com/jsf/html\" xmlns:f=\"http://java.sun.com/jsf/core\">";
    String head = "<h:head><title> AJCON Project Output</title></h:head><h:body><h:form>";
    String footer = "</h:form></h:body></html>";

    String ourProjectPath;
    
    public DocWriter(Map<String, Object> m, String output, String ourProjectPath) {
        createdObjects = m;
        this.outputDirectory = output;
        this.ourProjectPath = ourProjectPath;
    }

    public Map<String, Object> getCreatedObjects() {
        return createdObjects;
    }

    public void setCreatedObjects(Map<String, Object> createdObjects) {
        this.createdObjects = createdObjects;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getOutputDirectory() {
        return outputDirectory;
    }

    public void setOutputDirectory(String outputDirectory) {
        this.outputDirectory = outputDirectory;
    }

    
    public void write() throws IOException{
        boolean success = new File(outputDirectory+"WebContent").mkdir();
        if(success || new File(outputDirectory+"WebContent").exists()){
            boolean success3 = new File(outputDirectory + "WebContent\\META-INF").mkdir();
            if(success3 || new File(outputDirectory + "WebContent\\META-INF").exists()){
                File w = new File(outputDirectory+"WebContent\\META-INF\\MANIFEST.MF");
                BufferedWriter bw = new BufferedWriter(new FileWriter(w));
                bw.write("Manifest-Version: 1.0\n"+"Class-Path:");
                bw.close();
            }

            boolean success2 = new File(outputDirectory + "WebContent\\WEB-INF").mkdir();

            if(success2 || (new File(outputDirectory + "WebContent\\WEB-INF").exists())){
                String facesconfig = "<?xml version=\"1.0\"?> <faces-config xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd\" version=\"2.0\"> </faces-config>";
                String webxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <web-app xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://java.sun.com/xml/ns/javaee\" xmlns:web=\"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\" version=\"2.5\"> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>";

                File w = new File(outputDirectory+"WebContent\\WEB-INF\\web.xml");
                BufferedWriter bw = new BufferedWriter(new FileWriter(w));
                bw.write(webxml);
                bw.close();

                w = new File(outputDirectory+"WebContent\\WEB-INF\\faces-config.xml");
                bw = new BufferedWriter(new FileWriter(w));
                bw.write(facesconfig);
                bw.close();

                w = new File(outputDirectory+"WebContent\\index.jsp");
                bw = new BufferedWriter(new FileWriter(w));
                bw.write("");
                bw.close();
                
                w = new File(outputDirectory+"WebContent\\"+fileName+".xhtml");
                bw = new BufferedWriter(new FileWriter(w));

                bw.write(html);
                bw.write(head);
                
                Collection c = createdObjects.values();
                Iterator itr = c.iterator();

                while(itr.hasNext())
                    bw.write(itr.next().toString());

                bw.write(footer);
                bw.close();

                boolean success4 = new File(outputDirectory + "WebContent\\WEB-INF\\lib").mkdir();
                if(success4 || new File(outputDirectory + "WebContent\\WEB-INF\\lib").exists())
                copyLibraries(ourProjectPath + "jsf-jar-copy.bat", ourProjectPath + "jsf-jar", outputDirectory + "WebContent\\WEB-INF\\lib");
            }
        }
    }

    public void copyLibraries(String bathPath, String src, String dest){
        initializeBatFile(bathPath, src ,dest);
        String command = "cmd /c "+ bathPath;  // bat dosyası path i verilecek

        try {
                Process proc = null;
                proc = Runtime.getRuntime().exec(command);
                proc.waitFor();

        }
        catch (Exception e)
        {
                e.printStackTrace();
        }
        
    }

    private void initializeBatFile(String batFilePath, String sourcePath, String destPath) {
       FileOutputStream fileOut = null;

        String eof = "\n";
        try {
                fileOut = new FileOutputStream(batFilePath, false);

                fileOut.write(("copy " + sourcePath+ " " + destPath).getBytes());
                fileOut.write(eof.getBytes(), 0, 1);

           

                fileOut.close();


        }
        catch (Exception e) {
                e.printStackTrace();
        }

       // System.out.println("dfvdfbgdf");
    }
}
