/**
 * Copyright (c) 2012 EBM WebSourcing, 2012-2013 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.admin;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.ow2.petals.admin.api.ContainerAdministration;
import org.ow2.petals.admin.api.artifact.Logger;
import org.ow2.petals.admin.api.exception.ContainerAdministrationException;
import org.ow2.petals.admin.api.exception.InvalidLogLevelException;
import org.ow2.petals.admin.api.exception.LoggerNotFoundException;
import org.ow2.petals.admin.api.exception.NoConnectionException;
import org.ow2.petals.admin.topology.Domain;

/**
 * <p>
 * A mock of the {@link ContainerAdministration} API.
 * </p>
 * <p>
 * This mock provides only one method requiring a connection:
 * {@link #getSystemInfo()}.
 * </p>
 * 
 * @author "Mathieu CARROLLE - EBM WebSourcing"
 * 
 */
public class ContainerAdministrationMock implements ContainerAdministration {

    public static final String SYSTEM_INFO = "Petals JBI Container 3.3-SNAPSHOT"
            + "Java(TM) SE Runtime Environment 20.4-b02 Sun Microsystems Inc."
            + "Linux 3.3.1-1-ARCH amd64";

    public static final String INVALID_LOGGER_NAME = "invalid.logger";

    public static final String INVALID_LOG_LEVEL = "INVALID_LEVEL";

    public static final String ROOT_LOGGER_NAME = "";

    public static final String ROOT_LEVEL = "FINEST";

    public static final String LOGGER1_LOGGER_NAME = "logger.1";

    public static final String LOGGER1_LEVEL = "FINEST";

    public static final String LOGGER2_LOGGER_NAME = "logger.2";

    public static final String LOGGER2_LEVEL = "INFO";

    /**
     * Name of an host generating an error as 'host unknown'.
     */
    public static final String UNKNOWN_HOST = "unknown";

    private final Map<String, String> loggers = new HashMap<String, String>();

    /**
     * Flag to simulate an established connection
     */
    private static boolean IS_CONNECTED = false;

    public ContainerAdministrationMock() {
        this.loggers.put(ROOT_LOGGER_NAME, ROOT_LEVEL);
        this.loggers.put(LOGGER1_LOGGER_NAME, LOGGER1_LEVEL);
        this.loggers.put(LOGGER2_LOGGER_NAME, LOGGER2_LEVEL);
    }

    @Override
    public void connect(String host, int port, String user, String password)
            throws ContainerAdministrationException {
        if (host.equals(UNKNOWN_HOST)) {
            throw new ContainerAdministrationException("Unknown Host");
        }
        IS_CONNECTED = true;
    }

    @Override
    public boolean isConnected() throws ContainerAdministrationException {
        return IS_CONNECTED;
    }

    @Override
    public void disconnect() throws NoConnectionException, ContainerAdministrationException {
        IS_CONNECTED = false;
    }

    @Override
    public String getSystemInfo() throws ContainerAdministrationException {
        if (!IS_CONNECTED) {
            throw new ContainerAdministrationException("No connection establised");
        }
        return SYSTEM_INFO;
    }

    @Override
    public void stopContainer() throws ContainerAdministrationException {
        // TODO Auto-generated method stub

    }

    @Override
    public void shutdownContainer() throws ContainerAdministrationException {
        // TODO Auto-generated method stub

    }

    @Override
    public Domain getTopology(final String securityPassPhrase)
            throws ContainerAdministrationException {
        if ("no-domain-found-passphrase".equals(securityPassPhrase)) {
            return null;
        } else {
            return new Domain("my-domain");
        }
    }

    @Override
    public Properties getServerProperties() throws ContainerAdministrationException {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * <p>
     * The logger name 'invalid.logger' is associated to a logger that does not
     * exist.
     * </p>
     * <p>
     * The level 'INVALID_LEVEL' is associated to an invalid log level.
     * </p>
     */
    @Override
    public void changeLoggerLevel(String loggerName, String loggerLevel)
            throws LoggerNotFoundException, InvalidLogLevelException,
            ContainerAdministrationException {
        if (ContainerAdministrationMock.INVALID_LOGGER_NAME.equals(loggerName)) {
            throw new LoggerNotFoundException(loggerName);
        }

        if (ContainerAdministrationMock.INVALID_LOG_LEVEL.equals(loggerLevel)) {
            throw new InvalidLogLevelException(loggerLevel);
        }

        this.loggers.put(loggerName, loggerLevel);
    }

    @Override
    public List<Logger> getLoggers() throws ContainerAdministrationException {
        final List<Logger> loggers = new LinkedList<Logger>();
        for (final Entry<String, String> logger : this.loggers.entrySet()) {
            loggers.add(new Logger(logger.getKey(), logger.getValue()));
        }
        return loggers;
    }

}
