#include <AssetManager/AssetManager.hpp>
#include <Display/WidgetDisplay/CreditsDisplay/CreditsDisplay.hpp>
#include <Page/Page.hpp>
#include <Widget/Button/BackButton/BackButton.hpp>
#include <Widget/Label/Label.hpp>
#include <Widget/Layout/Center/Center.hpp>
#include <Widget/Layout/Column/Column.hpp>
#include <Widget/Layout/CompoundWidget/CategoryView/CategoryView.hpp>
#include <Widget/Layout/CompoundWidget/Form/Form.hpp>
#include <Widget/Layout/CompoundWidget/IconText/IconText.hpp>
#include <Widget/Layout/Margin/Margin.hpp>
#include <Widget/Layout/ScrollPanel/ScrollPanel.hpp>
#include <fstream>

Widget* CreditsDisplay::makeFilePage(std::string filePath)
{
    auto fullPath = assets.resolve(filePath);
    
    std::ifstream ifs(fullPath);
    if (!ifs.is_open())
    {
        return new Label("Could not open file: " + fullPath, Color::error);
    }

    std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
    ifs.close();

    return new ScrollPanel(
        1,
        new Label(content)
    );
}

CreditsDisplay::CreditsDisplay()
{
    setWidget(
        new Margin<10>(
            new Form(
                new IconText(Material::heart, "Credits & Acknowledgements"),
                new CategoryView(
                    "/credits/category-view",
                    new Page(
                        Material::heart,
                        "Credits",
                        makeFilePage("credits/credits.txt")
                    ),

                    new Page(
                        Material::heart,
                        "GLSL-FXAA License",
                        makeFilePage("credits/glsl-fxaa-license.txt")
                    ),

                    new Page(
                        Material::heart,
                        "Crypto++ License",
                        makeFilePage("credits/cryptopp-license.txt")
                    ),

                    new Page(
                        Material::heart,
                        "Lua License",
                        makeFilePage("credits/lua-license.txt")
                    ),

                    new Page(
                        Material::heart,
                        "SDL3 License",
                        makeFilePage("credits/sdl3-license.txt")
                    ),

                    new Page(
                        Material::heart,
                        "OpenAL Soft License",
                        makeFilePage("credits/openal-license.txt")
                    ),

                    new Page(
                        Material::heart,
                        "zlib License",
                        makeFilePage("credits/zlib-license.txt")
                    ),

                    new Page(
                        Material::heart,
                        "fmt License",
                        makeFilePage("credits/fmt-license.txt")
                    )
                ),
                Layout::WithProportion<1>(),
                new Center(new BackButton())
            )
        )
    );
}