/*
 * This file is part of Nucleus, licensed under the MIT License (MIT). See the LICENSE.txt file
 * at the root of this project for more details.
 */
package io.github.nucleuspowered.nucleus.modules.chatlogger.services;

import io.github.nucleuspowered.nucleus.core.logging.AbstractLoggingHandler;
import io.github.nucleuspowered.nucleus.modules.chatlogger.config.ChatLoggingConfig;
import io.github.nucleuspowered.nucleus.core.scaffold.service.ServiceBase;
import io.github.nucleuspowered.nucleus.core.services.INucleusServiceCollection;

import java.io.IOException;

import com.google.inject.Inject;

public class ChatLoggerHandler extends AbstractLoggingHandler implements ServiceBase {

    private boolean enabled = false;

    @Inject
    public ChatLoggerHandler(final INucleusServiceCollection serviceCollection) {
        super("chat", "chat", serviceCollection.messageProvider(), serviceCollection.logger());
    }

    @Override
    public void onReload(final INucleusServiceCollection serviceCollection) {
        final ChatLoggingConfig clca = serviceCollection.configProvider().getModuleConfig(ChatLoggingConfig.class);
        this.enabled = clca.isEnableLog();
        try {
            if (this.enabled && this.logger == null) {
                this.createLogger();
            } else if (!this.enabled && this.logger != null) {
                this.onShutdown();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected boolean enabledLog() {
        return this.enabled;
    }
}
