package com.containersol.minimesos.main;

import com.beust.jcommander.Parameters;
import com.containersol.minimesos.MinimesosException;
import com.containersol.minimesos.cluster.MesosCluster;
import com.containersol.minimesos.config.AppConfig;
import com.containersol.minimesos.config.ClusterConfig;
import com.containersol.minimesos.config.ConfigParser;
import com.containersol.minimesos.config.ConsulConfig;
import com.containersol.minimesos.config.MarathonConfig;
import com.containersol.minimesos.config.MesosAgentConfig;
import com.containersol.minimesos.config.MesosMasterConfig;
import com.containersol.minimesos.config.RegistratorConfig;
import com.containersol.minimesos.config.ZooKeeperConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;

import static java.lang.String.format;

/**
 * Initializes a default minimesosFile in the directory where minimesos is run
 */
@Parameters(separators = "=", commandDescription = "Initialize a minimesosFile")
public class CommandInit implements Command {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommandInit.class);

    public static final String CLINAME = "init";

    public static final String DEFAULT_HOST_USERID = "1000";

    @Override
    public boolean validateParameters() {
        return true;
    }

    @Override
    public String getName() {
        return CLINAME;
    }

    @Override
    public void execute() throws MinimesosException {
        File minimesosFile = new File(MesosCluster.getHostDir(), ClusterConfig.DEFAULT_CONFIG_FILE);

        if (minimesosFile.exists()) {
            throw new MinimesosException("A minimesosFile already exists in this directory");
        }

        String fileContent = getConfigFileContent();

        Path minimesosPath = Paths.get(minimesosFile.getAbsolutePath());
        try {
            Files.write(minimesosPath, fileContent.getBytes());
        } catch (IOException e) {
            throw new MinimesosException(format("Could not initialize minimesosFile: %s", e.getMessage()), e);
        }

        LOGGER.info("Initialized minimesosFile in this directory");

        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            UserPrincipal owner = lookupService.lookupPrincipalByName(DEFAULT_HOST_USERID);
            Files.setOwner(minimesosPath, owner);
        } catch (IOException e) {
            throw new MinimesosException("NOTE: minimesosFile remains owned by root instead of user ID " + DEFAULT_HOST_USERID + ": " + e.getMessage());
        }

    }

    public String getConfigFileContent() {
        ClusterConfig config = new ClusterConfig();
        config.setClusterName("Change Cluster Name in " + ClusterConfig.DEFAULT_CONFIG_FILE + " file");

        config.setMaster(new MesosMasterConfig());
        config.setZookeeper(new ZooKeeperConfig());
        config.getAgents().add(new MesosAgentConfig());
        config.setConsul(new ConsulConfig());
        config.setRegistrator(new RegistratorConfig());

        AppConfig weaveConfig = new AppConfig();
        weaveConfig.setMarathonJson("https://raw.githubusercontent.com/ContainerSolutions/minimesos/e2a43362f4581122762c80d8780d09b567783f1a/apps/weave-scope.json");

        MarathonConfig marathonConfig = new MarathonConfig();
        marathonConfig.getApps().add(weaveConfig);
        config.setMarathon(marathonConfig);

        ConfigParser parser = new ConfigParser();
        return parser.toString(config);
    }

}

@@ -35,16 +35,288 @@
 .main;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0Aimport java.nio.file.FileSystems;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.attribute.UserPrincipal;%0Aimport java.nio.file.attribute.UserPrincipalLookupService;%0A%0A
 import c
@@ -347,16 +347,16 @@
 meters;%0A
-
 import c
@@ -965,24 +965,25 @@
 eperConfig;%0A
+%0A
 import org.s
@@ -1032,280 +1032,8 @@
 y;%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0Aimport java.nio.file.FileSystems;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.attribute.UserPrincipal;%0Aimport java.nio.file.attribute.UserPrincipalLookupService;%0A%0A
 impo
@@ -1646,32 +1646,32 @@
 %0A%0A    @Override%0A
+
     public void 
@@ -1683,34 +1683,8 @@
 te()
- throws MinimesosException
  %7B%0A 

Remove declaration of unchecked exception throwing