//
// Created by Иван_Архипов on 23.11.2017.
//
#include <iostream>
#include <ctime>
#include <algorithm>

#ifdef WIN32
#include <direct.h>
#define mkdir(dir, mode) _mkdir(dir)
#endif

#include "LotroDat.h"
using namespace LOTRO_DAT;
using namespace std;

void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
    if (info.status == DatStatus::DAT_STATUS::E_FREE) {
        std::cout << "DatStatus: operation finished" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
        std::cout << "DatStatus: creating backup " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
        std::cout << "DatStatus: removing backup " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
        std::cout << "DatStatus: restoring backup " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
        std::cout << "DatStatus: applying locales " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
        std::cout << "DatStatus: extracting data " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
        std::cout << "DatStatus: gathering info " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
        std::cout << "DatStatus: initialising " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
    if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
        std::cout << "DatStatus: applying patch " << info.percentage << "% " 
        << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
        return;
    }
}

int main() {
    std::cout.precision(1);
    std::cout << fixed;
    std::cout << "Gi1dor's LotRO .dat patcher ver. " << LOTRO_DAT_VERSION << std::endl;
    freopen("patcher_errors.log", "w", stderr);

    setbuf(stdout, nullptr);
    setbuf(stderr, nullptr);

    setvbuf (stdout, nullptr, _IONBF, BUFSIZ);
    setvbuf (stderr, nullptr, _IONBF, BUFSIZ);

    std::cout << "Hello! I'm a basic shell version of .dat file patcher. I can open .dat file directly, "
            "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
    DatFile file;

    ifstream in("dat_file_path.txt");
    if (!in.fail()) {
        std::string filename;
        getline(in, filename);

        std::cout << "Using .dat file from dat_file_path.txt...\n";
        std::cout << "Opening file " << filename << std::endl;
        auto operation = file.Initialise(filename, 0);
        if (operation.result == ERROR)
            std::cout << "Cannot initialise dat file " << filename << " \n";
    }

    while (!file.Initialized()) {
        std::cout << "Please, tell, where the .dat file is\n";
        std::cout << "Enter path to file (including filename): ";
        std::string filename;
        std::getline(std::cin, filename);

        std::cout << "Opening file " << filename << std::endl;

        auto operation = file.Initialise(filename, 0);
        if (operation.result == ERROR)
            std::cout << "Cannot initialise dat file " << filename << ", please, try again\n";
    }

    std::cout << "Great! File initialised successfully!\n";
    file.GetStatusModule().AddStatusChangedCallbackFunction(&DatStatusChangedHandler);

    while (true) {
        std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1), "
                "change locale (enter 2), print current locale (enter 3), enable category (enter 4), disable category (enter 5), "
                "print disabled categories (enter 6), create backup (enter 7), remove backup (enter 8), "
                "restore .dat file from backup (enter 9), check if backup exists (enter 10) or exit (enter -1)\n";

        int cmd = 0;
        std::cout << "Enter number of command (1-10): ";
        std::cin >> cmd;

        std::string tmp;
        std::getline(std::cin, tmp);

        if (cmd == -1) {
            std::cout << "Exiting. Thanks for using me!\n";
            file.Deinitialize();
            break;
        }

        if (cmd == 1) {
            std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
                    "directory), of enter 0 to return to main dialogue.\n";
            while (true) {
                std::cout << "Enter name of file or 0: ";
                std::string dbname;
                std::getline(std::cin, dbname);

                if (dbname == std::to_string(0)) {
                    std::cout << "Okay, returning back...\n\n";
                    break;
                }

                Database db;

                std::cout << "Opening database... " << dbname << std::endl;

                if (!db.InitDatabase(dbname)) {
                    std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
                    continue;
                };

                if (db.CountRows() == 0) {
                    std::cout << "There are no files in database or database doesn't exist. "
                            "Please, try again!\n";
                    continue;
                }

                std::cout << "There are " << db.CountRows() << " files in database." << std::endl;

                std::cout << "Successfully opened database! Beginning patching...\n";
                
                const clock_t begin_time = clock();

                file.GetPatcher().PatchAllDatabase(&db);

                db.CloseDatabase();

                fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
                        float(clock() - begin_time) / CLOCKS_PER_SEC);

                std::cout << "Great! File was patched successfully!\n\n";
                break;
            }
        }
        if (cmd == 2) {
            std::cout << "Old locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
            std::cout << "Changing locale..." << std::endl;
            file.GetLocaleManager().SetLocale(file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED  ? DatLocaleManager::ORIGINAL : DatLocaleManager::PATCHED);
            std::cout << "New locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
        }

        if (cmd == 3) {
            std::cout << "Current locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
        }

        if (cmd == 4) {
            int category_id = 0;
            std::cout << "Enter category id: ";
            std::cin >> category_id;
            file.GetLocaleManager().EnableCategory(category_id);
            std::cout << "Category successfully enabled!" << std::endl;
        }

        if (cmd == 5) {
            int category_id = 0;
            std::cout << "Enter category id: ";
            std::cin >> category_id;
            file.GetLocaleManager().DisableCategory(category_id);
            std::cout << "Category successfully disabled!" << std::endl;
        }

        if (cmd == 6) {
            std::cout << "Disabled categories: ";
            for (auto i : file.GetLocaleManager().GetInactiveCategories())
                std::cout << i << " ";
            std::cout << endl;
        }

        if (cmd == 7) {
            std::cout << "Creating backup..." << std::endl;
            std::cout << "Create backup function returned " << file.GetBackupManager().CreateBackup("cli_local_En.backup").result << std::endl;
        }

        if (cmd == 8) {
            std::cout << "Removing backup..." << std::endl;
            std::cout << "Remove backup function returned " << file.GetBackupManager().RemoveBackup("cli_local_En.backup").result << std::endl;
        }

        if (cmd == 9) {
            std::cout << "Restoring file from backup..." << std::endl;
            std::cout << "Restore file function returned " << file.GetBackupManager().RestoreFromBackup("cli_local_En.backup").result << std::endl;
        }

        if (cmd == 10) {
            std::cout << "Backup file " << (file.GetBackupManager().CheckIfBackupAvailable("cli_local_En.backup") ? "exists!" : "doesn't exist.") << std::endl;
        }
    }
    //system("pause");
    return 0;
}

