#include "common.hpp"
#include "InputParser.hpp"
#include <iostream>
#include "AccountStatus.hpp"
#include "AccountUpdate.hpp"
#include "Deals.hpp"
#include "ExecCommand.hpp"

using namespace std;
char** global_argv;
int global_argc;
const char *GetEnv(const char *tag, const char *def = nullptr) noexcept
{
    const char *ret = getenv(tag);
    return ret ? ret : def;
}
bool IsRuLang()
{

    if (GetEnv("LANG"))
    {
        string rulang("ru_RU.UTF-8");
        string curlang(GetEnv("LANG"));
        if (rulang.compare(curlang) == 0)
        {
            return true;
        }
    }
    return false;
}


int main(int argc, char *argv[])
{    
    // TODO: in windows create folder in disk contains source file eg: project folder is d:\prj\src create folder d:\dev and add empty file null
    int r = system("curl > /dev/null 2>&1");
    if (r == 2) {
        //OK
    }
    else if (r == 1) {
        cout << "curl: not found" << endl;
        return 1;
    }
    global_argv = argv;
    global_argc = argc;
    InputParser input(argc, argv);    
    int retcode = 0;
    bool isRuLang = IsRuLang();
    bool isDebug = false;

    string address(DEFAULTADDRESS);

    if (input.cmdOptionExists("--debug"))
    {
        isDebug = true;
    }

    const string &addressarg = input.getCmdOption("--address");
    if (!addressarg.empty())
    {
        address = addressarg;
    }
    if (input.cmdOptionExists("--version"))
    {
        cout << "Version: " << PROGRAM_VERSION << endl;
        return retcode;
    }

    if (input.cmdOptionExists("account") && input.cmdOptionExists("status"))
    {
        if (input.cmdOptionExists("--help"))
        {
            AccountHelp(isRuLang);
            return retcode;
        }
        if (input.cmdOptionExists("-i") || input.cmdOptionExists("--info"))
        {
            return AccountInfo(address, argv[argc - 1], isDebug);
        }
        // -r|--rating               Показать данные рейтинга.
        if (input.cmdOptionExists("-r") || input.cmdOptionExists("--rating"))
        {
            return AccountRaiting(address, argv[argc - 1], isDebug);
        }
        if (input.cmdOptionExists("-s") || input.cmdOptionExists("--status"))
        {
            return AccountStatus(address, argv[argc - 1], isDebug);
        }
        // -f|--feedbacks [p|n]      Показать последнюю 1 тыс. полученных отзывов. Используй p или n для фильтрации только позитивных или негативных
        if (input.cmdOptionExists("-f") || input.cmdOptionExists("--feedbacks"))
        {
            int fb = 0; // all
            const string fbp = input.getCmdOption("-f");
            if (!fbp.empty())
            {
                if (fbp.compare("p") == 0)
                    fb = 1;
                if (fbp.compare("n") == 0)
                    fb = 2;
            }
            const string fbps = input.getCmdOption("--feedbacks");
            if (!fbps.empty())
            {
                if (fbps.compare("p") == 0)
                    fb = 1;
                if (fbps.compare("n") == 0)
                    fb = 2;
            }
            return AccountFeedbacks(address, argv[argc - 1], isDebug, fb);
        }
        return AccountStatus(address, argv[argc - 1], isDebug);
    }

    if (input.cmdOptionExists("account") && input.cmdOptionExists("update")) {
        AccountUpdate acu(input, isRuLang);
        return acu.Process();
    }
    if (input.cmdOptionExists("deal")) {
        Deals deals(input, isRuLang, address,isDebug);
        return deals.Process();
    }

    show_help(isRuLang);

    return retcode;
}