#ifndef _JFXMAP_QGIS_H_
#define _JFXMAP_QGIS_H_

#include <fstream>
#include <iostream>
#include <string>

#include "MapConfig.h"
#include "Utils.h"

namespace jf {

class Qgis {
   public:
    inline static std::string qgis_dir = "";
    // 用于初始化文件夹
    static void InitOnce(const std::string& qgis_relative_dir = "qgis/") {
#ifndef ANDROID
        static int done_once = false;
        if (done_once == true) {
            return;
        }
        done_once = true;
        qgis_dir = MapConfig::GetProjectDir() + qgis_relative_dir;
        FileSys::MkDir(qgis_dir);
        FileSys::Remove(std::string(qgis_dir) + std::string("*"));
#endif
    }
    // 用于打印csv表头
    template <typename... args>
    static void PrintCsvRowOnce(const std::string& f, args&... a) {
#ifndef ANDROID
        static int done_once = false;
        if (done_once == true) {
            return;
        }
        done_once = true;
        PrintCsvRow(f, a...);
#endif
    }
    // 用于打印csv一行
    template <typename... args>
    static void PrintCsvRow(const std::string& f, const args&... a) {
#ifndef ANDROID
        std::string content;
        PrintCsvRowImpl(content, a...);
        content += "\n";
        PrintSlow(qgis_dir + f, content);
#endif
    }

   private:
    static void PrintSlow(const std::string& f, const std::string& content) {
        // jf_log_info("[qgis::print_slow] Save file {}", f);
        std::ofstream of;
        of.open(f.c_str(), std::fstream::app | std::fstream::out);
        of << content;
        of.close();
    }
    template <typename headType, typename... args>
    static void PrintCsvRowImpl(std::string& inout, const headType& head, const args&... a) {
        inout = inout + jf::to_string(head) + ",";
        PrintCsvRowImpl(inout, a...);
    }
    template <typename headType>
    static void PrintCsvRowImpl(std::string& inout, const headType& head) {
        inout = inout + jf::to_string(head);
    }
};

}  // namespace jf

#endif