#include<filesystem>
#include<cstring>
#include "KMBuilderNewProject.h"
#include "KMBuilderConfig.h"


namespace fs = std::filesystem;

REGISTER_SUBCOMMAND_DYNCREATE(new-project,KMBuilderNewProject)

fs::path KMBuilderNewProject::createDir()
{
    fs::path currentPath = fs::current_path();
    fs::path destYamlPath = currentPath / m_appid ;

    if(fs::exists(destYamlPath))
    {
        std::cout << "目录已经存在" << std::endl;
    }
    else
    {
        fs::create_directory(destYamlPath);
        std::cout << "目录创建成功" << std::endl;
    }
    return destYamlPath;

}

//因为读取的yaml文件不包含注释，所以暂时用字符文件格式处理yaml文件
void KMBuilderNewProject::createYaml()
{

    fs::path destDir = createDir();
    std::cout << "yaml模板生成中..." << std::endl;

    //读取/opt/kaiming-builder下面的yaml文件
    std::ifstream yamlOriFile(m_yamlOriPath);
    std::vector<std::string> yamlContens;
    if(yamlOriFile.is_open())
    {
        std::string line;
        while(std::getline(yamlOriFile,line))
        {
            yamlContens.push_back(line); 
        } 
    }

    //修改package中的id
    std::stringstream ssId(yamlContens[5]);   //
    std::string tmpStr;
    std::vector<std::string> tmpIdValue;
    while(std::getline(ssId,tmpStr,':'))
    {
        tmpIdValue.push_back(tmpStr);
    }
        
    tmpIdValue[1] = m_appid;
    yamlContens[5] = tmpIdValue[0] + ": " + m_appid;   

    //写入目标yaml文件
    fs::path destPath = destDir / (m_appid+".yaml");
    std::ofstream yamlTarget(destPath);
    for(auto line : yamlContens)
    {
        yamlTarget << line + "\n"; 
    }
        
    std::cout << "yaml模板创建成功" << std::endl;
    std::cout << "yaml模板生成在：" << destPath << std::endl;

}

void KMBuilderNewProject::showUsage()
{
    std::cout << "Usage:" << std::endl;
    std::cout << "\t\t kaiming-builder new-project [OPTIONS] [APPID]" << std::endl;
    std::cout << "Create an new project, such as ${appid}.yaml" << std::endl;
    std::cout << "\nOPTIONS:" << std::endl;
    std::cout << "\nHelp Options:" << std::endl;
    std::cout << "  -h [--help]\t\tShow help options" << std::endl;
    std::cout << "\nAPPID:" << std::endl;
    std::cout << "\tappid\t\tThe unique identifier of the app. If the appid is left blank, create a default YAML template." << std::endl;
}

int KMBuilderNewProject::dispose(int argc, char **argv)
{
    //初始化参数
    init(argc , argv);
    //生成yaml文件
    return run();

}

// 检查new-project命令输入的参数
bool KMBuilderNewProject::checkOptions(int argc , char **argv)
{
    if(argc == 2)
    {
        if(strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0)
        {
            m_isHelp = true;
            return true;
        }
            
        if(argv[1][0] == '-')
        {
            return false;
        }
        return true;
    }
    if(argc == 1)
    { 
        return true;
    }
    return false;
}

void KMBuilderNewProject::init(int argc , char **argv)
{
    //不添加appid参数默认使用top.openkylin.demo
    if (! checkOptions(argc,argv))
    {
        std::cout << "Wrong Options" << std::endl;
        showUsage();
        exit(EXIT_FAILURE);
    }
    //-h / --help
    if (m_isHelp)
    {
        showUsage();
        exit(EXIT_SUCCESS);
    }
    if (argc != 1)
    {
        //获取appid
        m_appid = argv[1];
    }
    
    
    
}

int KMBuilderNewProject::run()
{
    createYaml();
    return EXIT_SUCCESS;
}
