/*
 * This file is part of BlockJamCore, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2016, BlockJam <https://blockjam.org/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.blockjam.core;

import com.google.common.io.CharStreams;
import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.blockjam.core.config.ConfigKeys;
import org.blockjam.core.config.ConfigManager;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Plugin;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

@Plugin(id = "blockjamcore", name = "BlockJamCore")
public final class BlockJamCorePlugin {

    public static BlockJamCorePlugin instance;

    @Inject @DefaultConfig(sharedRoot = false) private File configFile;
    @Inject @DefaultConfig(sharedRoot = false) private ConfigurationLoader<CommentedConfigurationNode> configLoader;

    private ConfigManager configManager;

    //TODO: make this extendable?
    @Listener
    public void onPreInitlization(GameInitializationEvent event) {
        instance = this;

        try {
            configManager = new ConfigManager(configFile, configLoader);
            configManager.loadDefaults();
        } catch (IOException ex) {
            throw new RuntimeException("Failed to load config");
        }
    }

    public static BlockJamCorePlugin instance() {
        return instance;
    }

    public static ConfigManager config() {
        return instance().configManager;
    }

    public static byte[] getFromAuthority(String key) {
        Optional<String> url = config().get(ConfigKeys.AUTHORITY_URL);
        assert url.isPresent();
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url.get()).openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            byte[] params = ("key=" + key).getBytes(StandardCharsets.UTF_8);
            connection.setFixedLengthStreamingMode(params.length);
            connection.connect();
            OutputStream os = connection.getOutputStream();
            os.write(params);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int i;
            while ((i = connection.getInputStream().read(buffer)) != -1) {
                baos.write(buffer, 0, i);
            }
            return baos.toByteArray();
        } catch (MalformedURLException ex) {
            throw new RuntimeException("Invalid value for `authority-url` in config", ex);
        } catch (IOException ex) {
            throw new RuntimeException("Failed to connect to authority service", ex);
        }
    }
}

@@ -3049,16 +3049,35 @@
 ing key)
+ throws IOException
  %7B%0A     
@@ -3553,24 +3553,345 @@
 .connect();%0A
+%0A            if (connection.getResponseCode() / 100 != 2) %7B%0A                // this just gets caught down below%0A                throw new IOException(%22Received bad response code from authority server (%22%0A                        + connection.getResponseCode() + %22 %22 + connection.getResponseMessage() + %22)%22);%0A            %7D%0A%0A
             
@@ -4364,16 +4364,23 @@
 Invalid 
+config 
 value fo
@@ -4400,18 +4400,8 @@
 url%60
- in config
 %22, e
@@ -4483,28 +4483,53 @@
 on(%22
-Failed to connect to
+Encountered connection error while contacting
  aut
@@ -4539,19 +4539,18 @@
 ity serv
-ic
 e
+r
 %22, ex);%0A

Handle non-200 response codes from authority server