#include "logging.hpp"
#include "nfd.h"
#include <pch.hpp>

#include <core/file_dialogs.hpp>

#include <nfd.hpp>

namespace OpenEngine {

    std::string FileDialogs::OpenFile(const char* filters)
    {
        NFD_Init();
        std::string path;

        nfdu8char_t *outPath;
        nfdu8filteritem_t nfd_filters[1] = { { "Open Engine save file", "oes" } };
        nfdopendialogu8args_t args = {0};
        args.filterList = nfd_filters;
        args.filterCount = 1;
        args.defaultPath = ".";
        nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
        if (result == NFD_OKAY)
        {
            path = outPath;
            NFD_FreePathU8(outPath);
            NFD_Quit();
            return path;
        }
        else if (result == NFD_CANCEL)
        {
            OE_CORE_TRACE("User canceled.");
            NFD_Quit();
            return std::string();
        }
        else 
        {
            OE_CORE_ERROR("{}", NFD_GetError());
            NFD_Quit();
            return std::string();
        }
    }

    std::string FileDialogs::SaveFile(const char* filters)
    {
        NFD_Init();
        std::string path;

        nfdchar_t* savePath;

        // prepare filters for the dialog
        nfdfilteritem_t filterItem[1] = {{"Open Engine save file", "oes"}};

        // show the dialog
        nfdresult_t result = NFD_SaveDialog(&savePath, filterItem, 1, NULL, "untitled.oes");
        if (result == NFD_OKAY) {
            path = savePath;
            NFD_FreePath(savePath);
            NFD_Quit();
            return path;
        NFD_Quit();
        } else if (result == NFD_CANCEL) {
            puts("User pressed cancel.");
            NFD_Quit();
            return std::string();
        } else {
            printf("Error: %s\n", NFD_GetError());
            NFD_Quit();
            return std::string();
        }
    }

}
