/******************************************************************************
*	File		-	fileBrowser.h
*	Author		-	Joey Pollack
*	Date		-	2021/11/04 (y/m/d)
*	Mod Date	-	2021/11/04 (y/m/d)
*	Description	-	File browser dialog window. Can be used for opening
*                   and saving files.
******************************************************************************/

// Some useful links:
// https://en.cppreference.com/w/cpp/filesystem/is_directory
// https://en.cppreference.com/w/cpp/filesystem/path

#ifndef FILE_SELECT_H_
#define FILE_SELECT_H_

#include <utils/opRes.h>

#include <vector>
#include <filesystem>

namespace lunarium
{
    class Image;
    class FileBrowser
    {
    public:
        enum Result
        {
            OK,
            CANCEL
        };

        enum SelectionMode
        {
            FILES_ONLY,
            DIRECTORIES_ONLY,
            ANY,
        };

    public:

        FileBrowser();
        bool OpenInDirectory(std::filesystem::path path);
        bool DoFrame();
        bool IsOpen() const;

        void SetPrompt(std::string prompt);
        void AddExtensionFilter(std::string ext);
        void SetSelectionMode(SelectionMode mode);
        void WarnOnExistingFileSelection(bool warn);

        // returns nullptr if the dialog has not been opened yet
        const std::filesystem::path* GetSelectedItem() const;
        Result GetResult() const;

    private:

        bool mIsOpen;
        SelectionMode mSelectionMode;
        bool mWarnOnExisting;
        Result mResult;
        std::string mPrompt;
        std::filesystem::path mCurrentDirectory;
        std::vector<std::filesystem::path> mItemsInDir;
        std::vector<std::string> mExtensionsFilter;         // Show only these extensions. If empty show all files.
        std::filesystem::path* mpSelectedItem;
        std::filesystem::path mInputSelection;

        Image* mpFolderIcon;
        Image* mpNewFolderIcon;
        Image* mpUpFolderIcon;
        static const int mBufferSize = 256;
        char mDirNameBuffer[mBufferSize];
        char mInputBuffer[mBufferSize];

    private:
        void ReloadItems();
        void DoListBox();
        bool DoButtons();
    };
}


#endif  // FILE_SELECT_H_