#ifndef __BUILDER_CLEANUP_H__
#define __BUILDER_CLEANUP_H__

#include "KMBuilderCache.h"
#include "KMBuilderConfigTypes.h"
#include "KMBuilderContext.h"
#include "KMBuilderGlobal.h"
#include "KMBuilderUtils.h"
#include "common/KMLogger.h"
#include "tinyxml2.h"

#include <gio/gio.h>
#include <json/json.h>
#include <ostree.h>
#include <yaml-cpp/yaml.h>

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <variant>
#include <vector>

using namespace std;

// 回调函数类型，用于处理每个文件
typedef bool (*FileHandler)(string sourceName, string fullDir, string relDir, bool *found, int depth);

/*
类名: KMBuilderCleanup
功能: 用于处理cleanup过程
成员变量:
   - shared_ptr<ConfigData> m_pConfigData;       	        //ConfigData配置文件指针
   - shared_ptr<KMBuilderCache> m_pKMBuilderCache; 	        //KMBuilderCache指针
   - BuilderCache m_builderCache;               	        //BuilderCache结构体
   - KMBuilderModule *m_pBuilderModule; 			        //BuilderModule私有嵌套BuilderModuleData
   - shared_ptr<BuilderModuleData> m_pBuilderModuleData;	//BuilderModuleData指针
成员函数:
   - 省略
*/

class KMBuilderCleanup
{
public:
    KMBuilderCleanup(shared_ptr<KMBuilderCache> cache);
    virtual ~KMBuilderCleanup();

public:
    /**
     * @brief : 开始执行cleanup阶段
     * @return: true-成功; false-失败
     */
    bool configCleanup();

    /**
     * @brief : 计算cleanup阶段的校验和
     */
    void configChecksumForCleanup();

    /**
     * @brief : 通过config配置文件查找appdata的路径
     * @param : appRoot字符串
     * @return: AppdataFile路径
     */
    string configFindAppdataFile(string appRoot);
    /**
     * @brief : 重写Appdata里面的license数据
     * @return: true-成功; false-失败
     */
    bool reWriteAppdata(const std::string &file_path, const std::string &license);
    /**
     * @brief : 重写Appdata里面关于RenameDesktopFile的数据
     * @return: true-成功; false-失败
     */
    bool reWriteAppdataByRenameDesktopFile(const std::string &file_path, const std::string &renameDesktopFile, const std::string &desktopBasename);
    /**
     * @brief : 重命名Icon
     * @return: true-成功; false-失败
     */
    static bool renameIconCb(string sourceName, string fullDir, string relDir, bool *found, int depth);
    /**
     * @brief : 递归遍历处理每个文件夹下的内容，并重命名
     * @return: true-成功; false-失败
     */
    bool foreachFileHelper(string sourceName, string fullDir, string relDir, FileHandler handler, bool *found, int depth = 0);
    /**
     * @brief : 执行appstream-compose
     * @return: true-成功; false-失败
     */
    bool appstreamCompose(string appDir, vector<string> exaArgs);

private:
    shared_ptr<ConfigData> m_pConfigData;               // ConfigData配置文件指针
    shared_ptr<KMBuilderCache> m_pKMBuilderCache;       // KMBuilderCache指针
    BuilderCache m_builderCache;                        // BuilderCache结构体
    KMBuilderModule *m_pBuilderModule;                  // BuilderModule私有嵌套BuilderModuleData
    shared_ptr<BuilderModuleData> m_pBuilderModuleData; // BuilderModuleData指针
};

#endif /* __BUILDER_CLEANUP_H__ */
