#include "HTTP/HttpClient.h"
#include "helper.h"
#include <iostream>
#include "List.h"
char* FindChar(const char* str,char f)
{
    int len = strlen(str);
    for (char* c = (char*)str+ len; c >= str; c--)
    {
        if (*c == f) return c;
    }
    return nullptr;
}
int main(int argns, char** args)
{
    if (argns > 1)
    {
        List<char> post_data = List<char>();
        HttpClient* cli = new HttpClient();
        for (int i = 0; i < argns - 1; i++)
        {
            post_data.clear();
            const char* path = args[i + 1];
            char* fname = FindChar(path, '\\');
            if (fname)
            {
                try {
                    fname += 1;
                    auto data = ReadAllBytes(path);
                    auto tmp_postdata = ToHex(data->begin(), data->size());
                    post_data.Add('\"');
                    post_data.AddRange(tmp_postdata, data->size()*2);
                    post_data.Add('\"');
                    wchar_t tmpurl[0x1000] = {0};
                    memset(tmpurl, 0, 0x2000);
                    swprintf_s(tmpurl, L"http://www.sym101.com/Users/FileUpLoad/EFTX\\%ws", Char2TCHAR(fname));
                    std::string ret = cli->Post(
                        tmpurl,
                        post_data.data(),
                        post_data.Count,
                        CONTENTTYPE_JSON);
                    delete tmp_postdata;
                    free((void*)data->begin());
                    printf_s("[%d/%d] 文件[%s]上传完毕\n", i + 1, argns - 1, fname);
                }
                catch (exception e)
                {
                    printf_s("[%d/%d] 文件[%s]上传失败->%s\n", i + 1, argns - 1, fname,e.what());
                }
            }
        }
        Sleep(500);
    }
    return 0;
}

//int main(int argns, char** args)
//{
//    if (argns > 1)
//    {
//        const char key[] = "0123456789ABCDEF";
//        for (int i = 0; i < argns - 1; i++)
//        {
//            const char* path = args[i + 1];
//            char* fname = FindChar(path, '\\');
//            if (fname)
//            {
//                fname += 1;
//                char* fname1 = new char[strlen(fname)];
//                memcpy(fname1, fname, strlen(fname) + 1);
//                char* tmp_point = nullptr;
//                while(tmp_point=FindChar(fname1, '.')) *tmp_point = '_';
//
//                stringstream str;
//                str << "#pragma once\n";
//                str << "unsigned char " << fname1 << " []=\n";
//                str << "{\n";
//                auto data = ReadAllBytes(path);
//                char tmp_value[] = {0,0,0};
//                for (int i = 0; i < data->size(); i++)
//                {
//                    unsigned char val= data->begin()[i];
//                    tmp_value[0] = key[val / 16];
//                    tmp_value[1] = key[val % 16];
//                    if (i < data->size() - 1)
//                        str << "0x" << tmp_value << ",";
//                    else
//                        str << "0x" << tmp_value;
//                    if ((i +1) % 19 == 0 && i != 0)
//                    {
//                        str << "\n";
//                    }
//                }
//                str << "};\n";
//                char out_path[2048] = { 0 };
//                *fname = '\0';
//                sprintf_s(out_path,"%s%s.h", path, fname1);
//                WriteAllText(out_path, str.str().c_str());
//            }
//        }
//        Sleep(500);
//    }
//    return 0;
//}