//
// Created by sfd on 25-6-10.
//

#include "ContentBroswerPanel.h"

#include <filesystem>
#include <../../../../Hazel/vendor/imgui/imgui.h>
#include <string>

namespace Hazel
{

    extern const std::filesystem::path g_AssetPath = "assets";

    ContentBroswerPanel::ContentBroswerPanel()
        : m_CurrentDirectory(g_AssetPath)
    {
        m_DirectoryIcon = Texture2D::Create("Resources/Icons/ContentBrowser/DirectoryIcon.png");
        m_FileIcon = Texture2D::Create("Resources/Icons/ContentBrowser/FileIcon.png");
    }


    void ContentBroswerPanel::OnImGuiRender()
    {
        ImGui::Begin("ContentBroswerPanel");

        ImGui::Text("%s", m_CurrentDirectory.string().c_str());
        ImGui::Separator();

        if (m_CurrentDirectory != std::filesystem::path(g_AssetPath))
        {
            if (ImGui::Button(".."))
            {
                m_CurrentDirectory = m_CurrentDirectory.parent_path();
            }
        }

        static float folderSize = 90.0f;
        static float padding = 16.0f;
        float cellSize = folderSize + padding;

        float panelWidth = ImGui::GetContentRegionAvail().x;
        int columnsCount = (int)panelWidth / cellSize;
        if (columnsCount <= 1)
            columnsCount = 1;

        ImGui::Columns(columnsCount, 0, false);

        for (auto& directory : std::filesystem::directory_iterator(m_CurrentDirectory))
        {
            auto& path = directory.path();
            auto relativePath = std::filesystem::relative(directory.path(), g_AssetPath);
            std::string filePathString = relativePath.filename().string();


            ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
            Ref<Texture2D> icon = directory.is_directory() ? m_DirectoryIcon : m_FileIcon;
            ImGui::ImageButton(std::to_string(hash_value(path)).c_str(),icon->GetRendererID(), {folderSize, folderSize}, {0, 1}, {1, 0});
            ImGui::PopStyleColor();

            if (ImGui::BeginDragDropSource())
            {
                const wchar_t* itemPath = relativePath.c_str();
                ImGui::SetDragDropPayload("CONTENT_BROSWER_ITEM", itemPath, (wcslen(itemPath) + 1) * sizeof(wchar_t), ImGuiCond_Once);
                ImGui::EndDragDropSource();
            }

            if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Left))
            {
                if (directory.is_directory())
                    m_CurrentDirectory /= path.filename();
            }
            ImGui::TextWrapped(filePathString.c_str());
            ImGui::NextColumn();

            /*
            if (directory.is_directory())
            {
                if (ImGui::Button(filePathString.c_str()))
                {
                    m_CurrentDirectory /= path.filename();
                }
            }else
            {
                if (ImGui::Button(filePathString.c_str()))
                {
                }
            }
            */

        }

        ImGui::Columns(1);

        ImGui::SliderFloat("folder size", &folderSize, 16, 512);
        ImGui::SliderFloat("padding", &padding, 0, 32);

        ImGui::End();
    }
}
