#include "imgui_internal.h"
#include <cstdint>
#include <cstring>
#include <panels/content_browser.hpp>

#include <open_engine/renderer/texture.hpp>
#include <open_engine/logging.hpp>
#include <filesystem>
#include <imgui.h>

namespace OpenEngine {

    const std::filesystem::path assets_directory = "assets";

    ContentBrowserPanel::ContentBrowserPanel()
        : current_directory(assets_directory)
    {
        // TODO: Add license
        folder_icon = Texture2D::Create("resources/textures/icons/folder2.png");
        file_icon = Texture2D::Create("resources/textures/icons/file.png");
    }

    void ContentBrowserPanel::OnImGuiRender()
    {
        ImGui::Begin("Assets");

        ImGui::Text("%s", current_directory.c_str());

        if (current_directory != assets_directory)
            if (ImGui::Button(".."))
                current_directory = current_directory.parent_path();

        auto directory_it = std::filesystem::directory_iterator(current_directory);
        std::vector<std::filesystem::directory_entry> entries;

        for (auto entry : directory_it)
            entries.emplace_back(entry);

        std::sort(entries.begin(), entries.end(),
                [](const std::filesystem::directory_entry& a,
                    const std::filesystem::directory_entry& b) {
                return (a.is_directory() && !b.is_directory());
                });

        ImVec2 button_size = { 100, 100 };
        auto panel_width = ImGui::GetContentRegionAvail().x;
        uint32_t table_columns = (int)(panel_width / button_size.x) - 1;

        if (table_columns <= 0)
            table_columns = 1;

        ImVec2 padding( 9, 9 );

        ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, padding);

        float margin = 20.0f;
        ImGui::Indent(margin);

        if (ImGui::BeginTable("table1", table_columns, ImGuiTableFlags_SizingFixedSame))
        {
            for (auto& entry : entries) {
                auto file_name = entry.path().filename().string();

                ImGui::PushID(file_name.c_str());

                ImGui::TableNextColumn();

                ImGui::PushStyleColor(ImGuiCol_Button, { 0.0f, 0.0f, 0.0f, 0.0f });
                ImGui::PushStyleColor(ImGuiCol_ButtonHovered, { 0.192f, 0.196f, 0.266f, 1.0f });
                ImGui::PushStyleColor(ImGuiCol_ButtonActive, { 0.270f, 0.278f, 0.352f, 1.0f });

                ImGui::BeginGroup();
                Ref<Texture2D> icon = entry.is_directory() ? folder_icon : file_icon;
                ImGui::ImageButton("##X", (ImTextureID)icon->GetID(), button_size, { 0, 1 }, { 1, 0 });
                if (entry.is_regular_file() && ImGui::BeginDragDropSource()) {
                    const char* source = entry.path().c_str();
                    ImGui::SetDragDropPayload("CONTENT_BROWSER_PAYLOAD", source, (strlen(source) + 1) * sizeof(char), ImGuiCond_Once);

                    ImGui::EndDragDropSource();
                }
                ImGui::TextWrapped("%s", file_name.c_str());

                ImGui::EndGroup();

                if (entry.is_directory()) {
                    if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
                        current_directory = entry.path().c_str();
                }

                ImGui::PopStyleColor(3);
                ImGui::PopID();
            }
            ImGui::EndTable();
        }
        ImGui::Unindent();
        ImGui::PopStyleVar();

        ImGui::End();
    }
}
