//
// Created by sfd on 25-5-31.
//

#ifndef WINDOWSPLATFORMUTILS_H
#define WINDOWSPLATFORMUTILS_H

#include <AsyncInfo.h>
#include <commdlg.h>

#include <Hazel/Utils/PlatformUtils.h>
#include <Hazel/Core/Application.h>

namespace Hazel
{
    std::string FileDiaglogs::OpenFile(const char* filter)
    {
        OPENFILENAME ofn;       // common diaglog box structure
        CHAR szFile[260] = { 0 };


        SDL_PropertiesID props = SDL_GetWindowProperties((SDL_Window*)Application::Get().GetWindow().GetNativeWindow());
        void * hwnd_ptr = SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);

        //   Init OPENFILENAME
        ZeroMemory(&ofn, sizeof(OPENFILENAME));
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = (HWND)hwnd_ptr;
        ofn.lpstrFile = szFile;
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = filter;
        ofn.nFilterIndex = 1;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
        if (GetOpenFileName(&ofn) == TRUE)
        {
            return ofn.lpstrFile;
        }

        return std::string();

    }

    std::string FileDiaglogs::SaveFile(const char* filter)
    {
        OPENFILENAME ofn;       // common diaglog box structure
        CHAR szFile[260] = { 0 };


        SDL_PropertiesID props = SDL_GetWindowProperties((SDL_Window*)Application::Get().GetWindow().GetNativeWindow());
        void * hwnd_ptr = SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);

        //   Init OPENFILENAME
        ZeroMemory(&ofn, sizeof(OPENFILENAME));
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = (HWND)hwnd_ptr;
        ofn.lpstrFile = szFile;
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = filter;
        ofn.nFilterIndex = 1;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
        if (GetSaveFileName(&ofn) == TRUE)
        {
            return ofn.lpstrFile;
        }

        return std::string();


    }

}


#endif //WINDOWSPLATFORMUTILS_H
