package xyz.rc24.bot.mangers;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Manages a single Redis instance, available across classes.
 * Now that's intuitive.™
 */
public class LogManager {
    /**
     * Redis for configuration use.
     */
    private JedisPool pool;
    private Gson gson;

    public LogManager(JedisPool pool) {
        this.pool = pool;
        this.gson = new Gson();
    }

    public enum LogType {
        MOD,
        SERVER
    }

    public class StorageFormat {
        @SerializedName("mod")
        public Long modLog;
        @SerializedName("server")
        public Long serverLog;
    }

    /**
     * Checks if a channel is enabled.
     *
     * @param type     Type of log to look for
     * @param serverID Server ID to check with
     * @return Boolean of state
     */
    public Boolean isLogEnabled(LogType type, Long serverID) {
        try (Jedis conn = pool.getResource()) {
            String storedJSON = conn.hget("logs", "" + serverID);
            StorageFormat storedConfig = gson.fromJson(storedJSON, StorageFormat.class);
            switch (type) {
                case MOD:
                    return !(storedConfig.modLog == null);
                case SERVER:
                    return !(storedConfig.serverLog == null);
                default:
                    // Other types we don't (yet) know of
                    return false;
            }
        }
    }

    /**
     * Gets the ID of the channel by type.
     *
     * @param serverID Server ID to look up with
     * @param type     Type of log to look for
     * @return Long with ID of server-log
     */
    public Long getLog(LogType type, Long serverID) {
        try (Jedis conn = pool.getResource()) {
            String storedJSON = conn.hget("logs", "" + serverID);
            StorageFormat storedConfig = gson.fromJson(storedJSON, StorageFormat.class);
            switch (type) {
                case MOD:
                    return storedConfig.modLog;
                case SERVER:
                    return storedConfig.serverLog;
                default:
                    // ????
                    return 0L;
            }
        }
    }

    /**
     * Sets the ID of the channel by type.
     *
     * @param serverID  Server ID to associate with
     * @param type      Type of log to associate
     * @param channelID Channel ID to set
     */
    public void setLog(Long serverID, LogType type, Long channelID) {
        try (Jedis conn = pool.getResource()) {
            String storedJSON = conn.hget("logs", "" + serverID);
            StorageFormat format;
            if (storedJSON == null || storedJSON.isEmpty()) {
                // I guess no config was created previously.
                format = new StorageFormat();
            } else {
                format = gson.fromJson(storedJSON, StorageFormat.class);
            }
            switch (type) {
                case MOD:
                    format.modLog = channelID;
                    break;
                case SERVER:
                    format.serverLog = channelID;
                    break;
            }
            String JSONtoStore = gson.toJson(format);
            conn.hset("logs", "" + serverID, JSONtoStore);
        }
    }

    /**
     * "Disables" a log type for a server.
     *
     * @param serverID  Server ID to associate with
     * @param type      Type of log to associate
     */
    public void disableLog(Long serverID, LogType type) {
        try (Jedis conn = pool.getResource()) {
            String storedJSON = conn.hget("logs", "" + serverID);
            StorageFormat format;
            if (storedJSON == null || storedJSON.isEmpty()) {
                // I guess no config was created previously.
                format = new StorageFormat();
            } else {
                format = gson.fromJson(storedJSON, StorageFormat.class);
            }
            switch (type) {
                case MOD:
                    format.modLog = null;
                    break;
                case SERVER:
                    format.serverLog = null;
                    break;
            }
            String JSONtoStore = gson.toJson(format);
            conn.hset("logs", "" + serverID, JSONtoStore);
        }
    }
}

@@ -1120,36 +1120,244 @@
 orageFormat 
-storedConfig
+format;%0A            if (storedJSON == null %7C%7C storedJSON.isEmpty()) %7B%0A                // I guess no config was created previously.%0A                format = new StorageFormat();%0A            %7D else %7B%0A                format
  = gson.from
@@ -1387,32 +1387,46 @@
 eFormat.class);%0A
+            %7D%0A
             swit
@@ -1492,28 +1492,22 @@
 eturn !(
-storedConfig
+format
 .modLog 
@@ -1574,28 +1574,22 @@
 eturn !(
-storedConfig
+format
 .serverL
@@ -2147,28 +2147,236 @@
 eFormat 
-storedConfig
+format;%0A            if (storedJSON == null %7C%7C storedJSON.isEmpty()) %7B%0A                // I guess no config was created previously.%0A                format = new StorageFormat();%0A            %7D else %7B%0A                format
  = gson.
@@ -2410,32 +2410,46 @@
 eFormat.class);%0A
+            %7D%0A
             swit
@@ -2513,28 +2513,22 @@
  return 
-storedConfig
+format
 .modLog;
@@ -2588,20 +2588,14 @@
 urn 
-storedConfig
+format
 .ser

Apply earlier logic going forward