package org.opendroidphp.app.common.tasks;

import android.os.Environment;

import org.apache.commons.io.FileUtils;
import org.opendroidphp.app.Constants;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import eu.chainfire.libsuperuser.Shell;

public class ConnectServer implements Runnable {

    protected final static String CHANGE_PERMISSION = "/system/bin/chmod 755 ";

    protected final static String SERVER_DAEMON_LIGHTTPD = "lighttpd";
    protected final static String SERVER_DAEMON_NGINX = "nginx";

    protected static String EXTERNAL_DIRECTORY = Environment.getExternalStorageDirectory().getPath() + "/droidphp/";
    protected String SERV_PORT_REGEX = "server.port.*";
    protected String baseShell;
    protected String basePort;
    protected String serverDaemon;

    /**
     * Set shell binary to use. Usually "sh" or "su"
     *
     * @param shell Shell to use
     */

    public ConnectServer setShell(String shell) {
        baseShell = shell;
        return this;
    }

    /**
     * Set port to use
     *
     * @param port port to use
     */

    public ConnectServer setServerPort(String port) {
        basePort = port;
        return this;
    }

    /**
     * Set Server Daemon to use
     *
     * @param server server daemon to use
     */

    public ConnectServer setServer(String server) {

        if (server == null || server.equals("")) {
            server = SERVER_DAEMON_LIGHTTPD;
        }
        serverDaemon = server;
        return this;
    }


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


    protected void initialize() {
        List<String> command = new ArrayList<String>();

        command.add(CHANGE_PERMISSION.concat(Constants.INTERNAL_LOCATION + "/scripts/server-sh.sh"));

        try {

            checkFilesystem();

            if (basePort != null) {
                changeServerData(basePort);
            }

        } catch (Exception e) {

            e.printStackTrace();
        }

        String daemon = serverDaemon.equals(SERVER_DAEMON_NGINX) ? "nginx" : "lighttpd";
        command.add(Constants.INTERNAL_LOCATION + "/scripts/server-sh.sh ".concat(daemon));

        String commands[] = new String[command.size()];
        int i = 0;
        //@TODO: simple way to convert List to String[]
        for (String eachCommand : command) {
            commands[i] = eachCommand;
            i++;
        }
        command.clear();

        if (baseShell == null) baseShell = "sh";

        Shell.run(baseShell, commands, null, false);
    }

    protected void checkFilesystem() {

        String[] filesUri = new String[]{
                Constants.PROJECT_LOCATION + "/logs/lighttpd",
                Constants.PROJECT_LOCATION + "/logs/mysql",
                Constants.PROJECT_LOCATION + "/logs/php",
                Constants.PROJECT_LOCATION + "/logs/nginx",
                Constants.PROJECT_LOCATION + "/conf",
                Constants.PROJECT_LOCATION + "/conf/nginx/logs",
                Constants.PROJECT_LOCATION + "/hosts/nginx",
                Constants.PROJECT_LOCATION + "/hosts/lighttpd",
                Constants.PROJECT_LOCATION + "/sessions",
                Constants.INTERNAL_LOCATION + "/tmp",
                Constants.EXTERNAL_STORAGE + "/htdocs"
        };

        for (String fileUri : filesUri) {

            File file = new File(fileUri);
            if (!file.exists()) file.mkdirs();
        }
    }

    protected void changeServerData(String port) {

        try {

            File confFile = new File(EXTERNAL_DIRECTORY + "/conf/lighttpd.conf");

            String sb = FileUtils.readFileToString(confFile, "UTF-8");
            sb.replaceFirst(SERV_PORT_REGEX, String.format(Locale.ENGLISH, "server.port                              = %s", port));

            FileUtils.writeStringToFile(confFile, sb, "UTF-8");
        } catch (Exception e) {

        }
    }
}

@@ -73,48 +73,8 @@
 t;%0A%0A
-import org.apache.commons.io.FileUtils;%0A
 impo
@@ -108,16 +108,16 @@
 stants;%0A
+
 %0Aimport 
@@ -184,33 +184,8 @@
 ist;
-%0Aimport java.util.Locale;
 %0A%0Aim
@@ -612,64 +612,8 @@
 /%22;%0A
-    protected String SERV_PORT_REGEX = %22server.port.*%22;%0A
     
@@ -1050,24 +1050,108 @@
 ing port) %7B%0A
+        if (port == null %7C%7C port.equals(%22%22)) %7B%0A            port = %228080%22;%0A        %7D%0A
         base
@@ -1821,103 +1821,8 @@
 );%0A%0A
-            if (basePort != null) %7B%0A                changeServerData(basePort);%0A            %7D%0A%0A
     
@@ -1983,24 +1983,25 @@
 httpd%22;%0A
+%0A
         
 command.
@@ -1996,89 +1996,214 @@
     
-command.add(Constants.INTERNAL_LOCATION + %22/scripts/server-sh.sh %22.concat(daemon)
+String shellScript = String.format(%22%25s /scripts/server-sh.sh %25s %25s%22,%0A                Constants.INTERNAL_LOCATION,%0A                daemon,%0A                basePort%0A        );%0A%0A        command.add(shellScript
 );%0A%0A
@@ -3412,16 +3412,16 @@
 leUri);%0A
+
         
@@ -3478,475 +3478,7 @@
    %7D
-%0A%0A    protected void changeServerData(String port) %7B%0A%0A        try %7B%0A%0A            File confFile = new File(EXTERNAL_DIRECTORY + %22/conf/lighttpd.conf%22);%0A%0A            String sb = FileUtils.readFileToString(confFile, %22UTF-8%22);%0A            sb.replaceFirst(SERV_PORT_REGEX, String.format(Locale.ENGLISH, %22server.port                              = %25s%22, port));%0A%0A            FileUtils.writeStringToFile(confFile, sb, %22UTF-8%22);%0A        %7D catch (Exception e) %7B%0A%0A        %7D%0A    %7D
 %0A%7D%0A

Fix error in changing server port