#ifndef __BUILDER_FINISH_H__
#define __BUILDER_FINISH_H__

#include "KMBuilderCache.h"
#include "KMBuilderConfigTypes.h"
#include "KMBuilderContext.h"
#include "KMBuilderGlobal.h"
#include "KMBuilderUtils.h"
#include "common/KMLogger.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;

/*
类名: KMBuilderFinish
功能: 用于处理finish过程
成员变量:
   - 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 KMBuilderFinish
{
public:
    KMBuilderFinish(shared_ptr<KMBuilderCache> cache);
    virtual ~KMBuilderFinish();

public:
    /**
     * @brief : 开始执行finish阶段
     * @return: true-成功; false-失败
     */
    bool builderContextFinish();
    /**
     * @brief : 执行finish阶段的checksum
     */
    void builderConfigChecksumForFinish();

    /**
     * @brief : 添加--extension=相关参数
     */
    void addArg(shared_ptr<BuilderExtension> self, vector<string> &args, string key, string value);
    void addArgb(shared_ptr<BuilderExtension> self, vector<string> &args, string key, bool value);
    void addArgl(shared_ptr<BuilderExtension> self, vector<string> &args, string key, list<string> value);

    /**
     * @brief : 序列化configData数据
     */
    string builderConfigSerialize(shared_ptr<ConfigData> self);

    /**
     * @brief : 序列化BuildOptions数据
     */
    void builderBuildOptionsToJson(shared_ptr<KMBuildOptions> pBuildOptions, Json::Value &keyJson);

    /**
     * @brief : 序列化builderExtension数据
     */
    void builderExtensionToJson(shared_ptr<BuilderExtension> pBuilderExtension, Json::Value &keyJson);
    /**
     * @brief : 序列化builderModule数据
     */
    void builderModuleToJson(shared_ptr<KMBuilderModule> pBuilderModule, Json::Value &keyJson);

    /**
     * @brief : 针对空字符串数据不加入JSON
     */
    void noEmptyAddJson(string key, any value, Json::Value &keyJson);
    /**
     * @brief : 针对空JsonArr数据不加入JSON
     */
    void noEmptyArrAddJson(string key, Json::Value value, Json::Value &keyJson);

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_FINISH_H__ */
