/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2026 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "jmmail.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/wait.h>
#include <unistd.h>
#endif
#include <cx/string.h>
#include <cx/printf.h>
#include <cx/hash_map.h>
#include <cx/linked_list.h>
#include <cx/streams.h>

int jmap_main(int argc, char **argv);

#ifdef _WIN32

#define strcasecmp _stricmp

static char* wchar2utf8(const wchar_t *wstr, size_t wlen) {
    size_t maxlen = wlen * 4;
    char *ret = malloc(maxlen + 1);
    int ret_len = WideCharToMultiByte(
        CP_UTF8,
        0,
        wstr,
        wlen,
        ret,
        maxlen,
        NULL,
        NULL);
    ret[ret_len] = 0;
    return ret;
}

int wmain(int argc, wchar_t **argv) {
    char **argv_utf8 = calloc(argc, sizeof(char*));
    for(int i=0;i<argc;i++) {
        argv_utf8[i] = wchar2utf8(argv[i], wcslen(argv[i]));
    }
    
    int ret = jmap_main(argc, argv_utf8);
    

    for(int i=0;i<argc;i++) {
        free(argv_utf8[i]);
    }
    free(argv_utf8);
    
    return ret;
}
#else
int main(int argc, char **argv) {
    return jmap_main(argc, argv);
}
#endif


int jmap_main(int argc, char **argv) {
    if(argc < 4) {
        fprintf(stderr, "Missing args\n");
        //print_usage(argv[0]);
        return -1;
    }
    
    putenv("LC_TIME=C");
    
    const char *session_url = argv[1];
    const char *user = argv[2];
    const char *password = argv[3];
    
    JMAPSession *sn = jmap_session_new_with_user(session_url, user, password);
    
    
    return 0;
}
