/*
 *  This file is part of Player Analytics (Plan).
 *
 *  Plan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License v3 as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Plan 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 Plan. If not, see <https://www.gnu.org/licenses/>.
 */
package com.djrapitops.plan.settings.network;

import com.djrapitops.plan.SubSystem;
import com.djrapitops.plan.exceptions.EnableException;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.settings.config.*;
import com.djrapitops.plan.settings.config.paths.PluginSettings;
import com.djrapitops.plan.settings.config.paths.TimeSettings;
import com.djrapitops.plan.settings.upkeep.FileWatcher;
import com.djrapitops.plan.settings.upkeep.WatchedFile;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.objects.NewerConfigQuery;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.storage.database.transactions.StoreConfigTransaction;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import net.playeranalytics.plugin.scheduling.RunnableFactory;
import net.playeranalytics.plugin.scheduling.TimeAmount;
import net.playeranalytics.plugin.server.PluginLogger;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * In charge of updating network-server configs.
 * <p>
 * Performs the following tasks related to network configs:
 * - File modification watching related to server configs
 * - Database update operations related to server configs
 * - File update operations from database related to server configs
 *
 * @author AuroraLS3
 */
@Singleton
public class NetworkSettingManager implements SubSystem {

    private final PlanFiles files;
    private final DBSystem dbSystem;
    private final ServerInfo serverInfo;
    private final RunnableFactory runnableFactory;
    private final PlanConfig config;
    private final PluginLogger logger;
    private final ErrorLogger errorLogger;

    private File serverSettingsFolder;

    private FileWatcher watcher;

    @Inject
    public NetworkSettingManager(
            PlanFiles files,
            PlanConfig config,
            DBSystem dbSystem,
            ServerInfo serverInfo,
            RunnableFactory runnableFactory,
            PluginLogger logger,
            ErrorLogger errorLogger
    ) {
        this.files = files;
        this.config = config;
        this.dbSystem = dbSystem;
        this.serverInfo = serverInfo;
        this.runnableFactory = runnableFactory;
        this.logger = logger;

        this.errorLogger = errorLogger;
    }

    @Override
    public void enable() {
        serverSettingsFolder = createServerSettingsFolder();

        watcher = prepareFileWatcher();
        watcher.start();

        scheduleDBCheckTask();
    }

    @Override
    public void disable() {
        if (watcher != null) {
            watcher.interrupt();
        }
    }

    public static ServerUUID getServerUUIDFromFilename(File file) {
        String fileName = file.getName();
        String uuidString = fileName.substring(0, fileName.length() - 4);
        return ServerUUID.fromString(uuidString);
    }

    private FileWatcher prepareFileWatcher() {
        FileWatcher fileWatcher = new FileWatcher(serverSettingsFolder, errorLogger);

        File[] configFiles = getConfigFiles();
        if (configFiles != null) {
            for (File file : configFiles) {
                addFileToWatchList(fileWatcher, file);
            }
        }

        return fileWatcher;
    }

    public File[] getConfigFiles() {
        return serverSettingsFolder.listFiles((dir, name) -> name.endsWith(".yml"));
    }

    private void addFileToWatchList(FileWatcher fileWatcher, File file) {
        try {
            ServerUUID serverUUID = getServerUUIDFromFilename(file);

            fileWatcher.addToWatchlist(new WatchedFile(file, () -> updateConfigInDB(file, serverUUID)));
        } catch (IndexOutOfBoundsException | IllegalArgumentException ignore) {
            /* Invalid file-name, ignored */
        }
    }

    private void scheduleDBCheckTask() {
        long checkPeriod = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS);
        runnableFactory.create(this::updateConfigFromDBIfUpdated)
                .runTaskTimerAsynchronously(checkPeriod, checkPeriod);
    }

    private File createServerSettingsFolder() {
        try {
            File serverConfigFolder = files.getFileFromPluginFolder("serverConfiguration");
            Path dir = serverConfigFolder.toPath();
            if (!Files.isSymbolicLink(dir)) Files.createDirectories(dir);
            return serverConfigFolder;
        } catch (IOException e) {
            throw new EnableException("Could not initialize NetworkSettingManager: " + e.getMessage(), e);
        }
    }

    private File getServerConfigFile(ServerUUID serverUUID) {
        return new File(serverSettingsFolder, serverUUID + ".yml");
    }

    private void updateConfigFromDBIfUpdated() {
        Database database = dbSystem.getDatabase();
        Set<ServerUUID> serverUUIDs = database.query(ServerQueries.fetchPlanServerInformation()).keySet();

        for (ServerUUID serverUUID : serverUUIDs) {
            // Remove the proxy server on the list
            if (serverUUID.equals(serverInfo.getServerUUID())) {
                continue;
            }
            updateConfigFromDBIfUpdated(database, serverUUID);
        }
    }

    private void updateConfigFromDBIfUpdated(Database database, ServerUUID serverUUID) {
        File configFile = getServerConfigFile(serverUUID);
        long lastModified = configFile.exists() ? configFile.lastModified() : -1;

        Optional<Config> foundConfig = database.query(new NewerConfigQuery(serverUUID, lastModified));
        if (foundConfig.isPresent()) {
            try {
                Config writing = foundConfig.get();
                String serverName = writing.getNode(PluginSettings.SERVER_NAME.getPath()).map(ConfigNode::getString).orElse("Unknown");

                new ConfigWriter(configFile.toPath()).write(writing);
                logger.info("Config file for server '" + serverName + "' updated in /Plan/serverConfiguration");
                addFileToWatchList(watcher, configFile);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    }

    public void updateConfigInDB(File file, ServerUUID serverUUID) {
        if (!file.exists()) {
            return;
        }

        Database database = dbSystem.getDatabase();

        try (ConfigReader reader = new ConfigReader(file.toPath())) {
            Config serverConfig = reader.read();
            database.executeTransaction(new StoreConfigTransaction(serverUUID, serverConfig, file.lastModified()));
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

}
