package org.atmospheregames.network.redis;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import org.atmospheregames.network.util.Style;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import redis.clients.jedis.JedisPubSub;

import java.util.HashMap;
import java.util.Map;

public class RedisListener extends JedisPubSub {

    @Getter private final Map<String, JsonObject> cachedMessages = new HashMap<>();
    private final JsonParser parser = new JsonParser();

    @Override
    public void onMessage(String channel, String message) {
        JsonObject object = parser.parse(message).getAsJsonObject();
        String serverName = object.get("server").getAsString();

        cachedMessages.put(serverName, object);



        // do shit with recieved message.

        /*
        proposed logic: if message states is offline, print offline message
        if method states is online, print online message.

        problem being: we only want to do each one of these once.

         */

        if(object.get("server").getAsString().equals("hub")) {
            // run blacklist commands remotely
            
        }

        if(!object.get("online").getAsBoolean()) {
            for(Player player : Bukkit.getOnlinePlayers()) {
                if(!player.hasPermission("network.servermonitor"))
                    continue;

                player.sendMessage(Style.translate("&6[Server Monitor] &fThe server &e" + serverName + " &fhas gone &coffline&f."));
            }
        }


    }
}
