package co.phoenixlab.discord.api;

import co.phoenixlab.discord.VahrhedralBot;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.http.HttpHeaders;
import org.apache.http.entity.ContentType;
import org.json.simple.JSONObject;
import org.slf4j.Logger;

import java.io.IOException;
import java.net.HttpURLConnection;

public class DiscordApiClient {

    private static final Logger LOGGER = VahrhedralBot.LOGGER;

    private String email;
    private String password;

    private String token;

    public DiscordApiClient() {

    }

    public void logIn(String email, String password) throws IOException {
        //  TODO
        LOGGER.info("Attempting to log in as {}...", email);
        this.email = email;
        this.password = password;

        JSONObject auth = new JSONObject();
        auth.put("email", email);
        auth.put("password", password);

        HttpResponse<JsonNode> response;
        try {
             response = Unirest.post(ApiConst.LOGIN_ENDPOINT).
                    header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()).
                    body(auth.toJSONString()).
                    asJson();
        } catch (UnirestException e) {
            throw new IOException("Unable to log in", e);
        }
        int status = response.getStatus();

        if (status != HttpURLConnection.HTTP_OK) {
            if (status == HttpURLConnection.HTTP_FORBIDDEN) {
                //  Servers return FORBIDDEN for bad credentials
                LOGGER.warn("Unable to log in with given credentials: ", response.getStatusText());
            } else {
                LOGGER.warn("Unable to log in, Discord may be having issues");
            }
            throw new IOException("Unable to log in: HTTP " + response.getStatus() + ": " + response.getStatusText());
        }
        token = response.getBody().getObject().getString("token");
        LOGGER.info("Successfully logged in, token is {}", token);
        openWebSocket();
    }

    private void openWebSocket() {
        //  TODO
    }

    public void sendMessage(String body, String channelId) throws IOException {
        //  TODO
    }

    public void deleteMessage(String messageId, String channelId) throws IOException {
        //  TODO
    }

    public boolean isOpen() {
        //  TODO
        return false;
    }




}

@@ -2185,34 +2185,1039 @@
 ocket() 
-%7B%0A        //  TODO
+throws IOException %7B%0A        final String gateway = getWebSocketGateway();%0A        //  TODO%0A    %7D%0A%0A    private String getWebSocketGateway() throws IOException %7B%0A        HttpResponse%3CJsonNode%3E response;%0A        try %7B%0A            response = Unirest.get(ApiConst.WEBSOCKET_GATEWAY).%0A                    header(%22authorization%22, token).%0A                    asJson();%0A        %7D catch (UnirestException e) %7B%0A            throw new IOException(%22Unable to retrieve websocket gateway%22, e);%0A        %7D%0A        int status = response.getStatus();%0A        if (status != HttpURLConnection.HTTP_OK) %7B%0A            LOGGER.warn(%22Unable to retrieve websocket gateway: HTTP %7B%7D: %7B%7D%22, status, response.getStatusText());%0A            throw new IOException(%22Unable to retrieve websocket : HTTP %22 + status + %22: %22 + response.getStatusText());%0A        %7D%0A        String gateway = response.getBody().getObject().getString(%22url%22);%0A        gateway = gateway.substring(1);%0A        LOGGER.info(%22Found WebSocket gateway at %7B%7D%22, gateway);%0A        return gateway;
 %0A    %7D%0A%0A

Implement get websocket gateway