package me.jayfella.pluginwizard;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.openide.filesystems.FileLock;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.EditableProperties;
import org.openide.util.Exceptions;
import org.openide.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author jayfella
 */
public class WizardUtils
{
    public static void setModuleBasePackageInProjectXmlFile(FileObject xmlFile, String basePackage)
    {
        try
        {
            Document doc = XMLUtil.parse(new InputSource(new ByteArrayInputStream(xmlFile.asBytes())), false, false, null, null);
            NodeList nl = doc.getDocumentElement().getElementsByTagName("code-name-base");

            if (nl != null)
            {
                for (int i = 0; i < nl.getLength(); i++)
                {
                    Element el = (Element) nl.item(i);

                    if (el.getParentNode() != null && "data".equals(el.getParentNode().getNodeName()))
                    {
                        NodeList nl2 = el.getChildNodes();

                        if (nl2.getLength() > 0)
                        {
                            nl2.item(0).setNodeValue(basePackage);
                        }

                        break;
                    }
                }
            }

            OutputStream out = xmlFile.getOutputStream();

            try
            {
                XMLUtil.write(doc, out, "UTF-8");
            }
            finally
            {
                out.close();
            }
        }
        catch (Exception ex)
        {
            Exceptions.printStackTrace(ex);
        }
    }

    public static void setLicenseInProjectProperties(String projPropPath, String licenseLoc) throws IOException
    {
        File projPropFile = new File(projPropPath);
        FileObject fo = FileUtil.toFileObject(projPropFile);

        String licensePath = "src/" + licenseLoc.replace("\\", "/");

        EditableProperties ep = loadProperties(fo);
        ep.setProperty("license.file", licensePath);
        storeProperties(fo, ep);
    }

    public static void copyJmeLicenseToModule(File basePackageDir) throws IOException
    {
        InputStream licenseFileIs = WizardUtils.class.getClassLoader().getResourceAsStream("me/jayfella/pluginwizard/modulefiles/jme-license.txt");

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;

        try
        {
            br = new BufferedReader(new InputStreamReader(licenseFileIs));

            while ((line = br.readLine()) != null)
            {
                sb.append(line).append(System.getProperty("line.separator"));
            }

        }
        catch (IOException ex)
        {
            Exceptions.printStackTrace(ex);
        }
        finally
        {
            if (br != null)
            {
                try
                {
                    br.close();
                }
                catch (IOException ex)
                {
                    Exceptions.printStackTrace(ex);
                }
            }
        }

        File newLicenseFile = new File(basePackageDir.getAbsolutePath() + File.separatorChar + "jme-license.txt");
        newLicenseFile.createNewFile();

        FileWriter fw = new FileWriter(newLicenseFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(sb.toString());

        bw.close();
    }

    public static void createBundlePropertiesFile(FileObject fo, String pluginName) throws IOException
    {
        EditableProperties ep = loadProperties(fo);
        ep.setProperty("OpenIDE-Module-Name", pluginName);
        storeProperties(fo, ep);
    }

    public static void createModuleManifestFile(FileObject fo, String basePackage) throws IOException
    {
        EditableProperties ep = loadProperties(fo);
        String basePackagePath = basePackage.replace(".", "/");

        ep.setProperty("Manifest-Version", "1.0");
        ep.setProperty("OpenIDE-Module", basePackage);
        ep.setProperty("OpenIDE-Module-Implementation-Version", "0");
        // ep.setProperty("OpenIDE-Module-Layer", basePackagePath + "/layer.xml");
        ep.setProperty("OpenIDE-Module-Localizing-Bundle", basePackagePath + "/Bundle.properties");
        storeProperties(fo, ep);
    }

    public static void addModuleToSuiteProperties(String propertiesFilePath, String pluginBasePackage, String pluginName) throws IOException
    {
        FileObject propFileObject = FileUtil.toFileObject(new File(propertiesFilePath));

        EditableProperties ep = loadProperties(propFileObject);
        String prop = ep.getProperty("modules");

        if (prop.trim().isEmpty())
        {
            prop = "${project." + pluginBasePackage + "}";
        }
        else
        {
            prop += ":${project." + pluginBasePackage + "}";
        }

        prop = prop.replaceAll("('|\")", "\\\\$1");

        /* String additional = new StringBuilder()
                .append('\\')
                .append(String.format("%n"))
                .append("${project.")
                .append(pluginBasePackage)
                .append("}")
                .toString(); */

        ep.setProperty("modules", prop);
        ep.setProperty("project." + pluginBasePackage, pluginName);

        storeProperties(propFileObject, ep);
    }

    private static EditableProperties loadProperties(FileObject propsFO) throws IOException
    {
        InputStream propsIS = propsFO.getInputStream();
        EditableProperties props = new EditableProperties(true);
        try
        {
            props.load(propsIS);
        }
        finally
        {
            propsIS.close();
        }
        return props;
    }

    public static void storeProperties(FileObject propsFO, EditableProperties props) throws IOException
    {
        FileLock lock = propsFO.lock();

        try
        {
            OutputStream os = propsFO.getOutputStream(lock);

            try
            {
                props.store(os);
            }
            finally
            {
                os.close();
            }
        }
        finally
        {
            lock.releaseLock();
        }
    }


}

@@ -5264,330 +5264,8 @@
  %7D%0A%0A
-        prop = prop.replaceAll(%22('%7C%5C%22)%22, %22%5C%5C%5C%5C$1%22);%0A%0A        /* String additional = new StringBuilder()%0A                .append('%5C%5C')%0A                .append(String.format(%22%25n%22))%0A                .append(%22$%7Bproject.%22)%0A                .append(pluginBasePackage)%0A                .append(%22%7D%22)%0A                .toString(); */%0A%0A
     

Fix for EditableProperties backslash error