#include "net/http_client.h"
#include "error.h"
#include "str.h"

#include <string.h>
#include <windows.h>
#include <wininet.h>

static HINTERNET internet = 0;

static void init_internet();

struct sw_http_request_handle sw_http_request_async(char *url) {
	HINTERNET session, http;
	u16 port;
	char *proto, *rest;
	char *server, *rest2;

	sw_str_split2(url, "://", &proto, &rest);
	if(proto == 0) {
		sw_error("url requires ://");
	}

	if(strcmp(proto, "https") == 0) {
		port = INTERNET_DEFAULT_HTTPS_PORT;
	} else if(strcmp(proto, "http") == 0) {
		port = INTERNET_DEFAULT_HTTP_PORT;
	} else {
		sw_error("unknown protocol '%s'", proto);
	}

	sw_str_split2(url, "/", &server, &rest2);
	/* TODO: handle urls without / */

	init_internet();

	session = InternetConnect(
	    internet, server, port, 0, 0, INTERNET_SERVICE_HTTP, 0, 0
	);

	if(session == 0) {
		sw_win32_error();
	}

	http = HttpOpenRequest(session, "GET", rest2, "HTTP/1.1", 0, 0, 0, 0);

	if(http == 0) {
		sw_win32_error();
	}

	if(!HttpSendRequest(http, 0, 0, 0, 0)) {
		sw_win32_error();
	}

	return (struct sw_http_request_handle){0};
}

static void init_internet() {
	if(internet == 0) {
		internet = InternetOpen(
		    "Skunkworks", INTERNET_OPEN_TYPE_DIRECT, 0, 0, INTERNET_FLAG_ASYNC
		);
		if(internet == 0) {
			sw_win32_error();
		}
	}
}
