#include "KMBuilderScreenshots.h"

KMBuilderScreenshots::KMBuilderScreenshots(shared_ptr<KMBuilderCache> cache)
{
    m_pConfigData = builderContext.getBuilderConfig()->getConfigData();
    m_pKMBuilderCache = cache;
    m_builderCache = m_pKMBuilderCache->getBuilderCache();
}

KMBuilderScreenshots::~KMBuilderScreenshots()
{
}

bool KMBuilderScreenshots::saveScreenshots()
{
    if (!builderContext.getIsRequireChanges() && !builderContext.isExportOnly())
    {
        m_pKMBuilderCache->ensureCheckout();
    }

    if (!builderContext.getMirrorScreenshotsUrl().empty() && !builderContext.isExportOnly())
    {
        string configId = std::any_cast<string>(m_pConfigData->attributeMap["id"]);
        string configBranch = std::any_cast<string>(m_pConfigData->attributeMap["branch"]);
        string appDir = builderContext.getAppDir();

        // 获取当前路径
        string currentPath = fs::current_path().string();
        string stateDir = currentPath + "/" + builderContext.getStateDir();

        string screenshotSubdir = KMBuilderUtils::argSplice(configId, "-", configBranch);

        string url = "";
        if (!builderContext.getMirrorScreenshotsUrl().empty())
        {
            url = builderContext.getMirrorScreenshotsUrl() + "/" + screenshotSubdir;
        }

        string xmlRelpath = KMBuilderUtils::argSplice("files/share/app-info/xmls/", configId, ".xml.gz");
        string xml = appDir + "/" + xmlRelpath;
        string cache = stateDir + "/" + "screenshots-cache";
        cache = "/home/yu/work/kaiming/kaiming-builder/.vscode/test/.kaiming-builder/screenshots-cache";
        string screenshots = appDir + "/" + "screenshots";
        string screenshots_sub = screenshots + "/" + screenshotSubdir;
        string fs_app_dir = "--filesystem=" + appDir;
        string fs_cache = "--filesystem=" + cache;

        vector<string> args;
        args.push_back("kaiming");
        args.push_back("build");
        args.push_back("--die-with-parent");
        args.push_back("--nofilesystem=host:reset");
        args.push_back(fs_app_dir);
        args.push_back(fs_cache);
        args.push_back("--share=network");
        args.push_back(appDir);
        args.push_back("appstream-util");
        args.push_back("mirror-screenshots");
        args.push_back(xml);
        args.push_back(url);
        args.push_back(cache);
        args.push_back(screenshots_sub);

        km_logger.info("Mirroring screenshots from appdata");
        //builder_set_term_title (_("Mirroring screenshots"));

        if (!fs::exists(screenshots))
        {
            if (fs::is_directory(screenshots))
            {
                km_logger.info("Path screenshots exists but is not a directory.");
                return false;
            }
            else
            {
                fs::create_directories(screenshots);
            }
        }

        if (fs::exists(xml))
        {
            if (!fs::is_directory(cache))
            {
                fs::create_directories(cache);
            }

            fs::path filePathXml = xml;
            if (!KMBuilderUtils::breakHardlink(filePathXml))
            {
                km_logger.info("Error mirroring screenshots");
                return false;
            }
            if (KMBuilderUtils::isInSanbox())
            {
                if (!KMBuilderUtils::hostSpawn("", 0, args))
                {
                    km_logger.info("Error mirroring screenshots");
                    return false;
                }
            }
            else
            {
                if (!KMBuilderUtils::spawn("", 0, args))
                {
                    km_logger.info("Error mirroring screenshots");
                    return false;
                }
            }
        }
        km_logger.info("Saved screenshots in %s", screenshots.c_str());
        return true;
    }
    km_logger.info("Don't Saved screenshots");
    return true;
}
