
#include "../log4cplus/logger.h"
#include "../log4cplus/configurator.h"
#include "../log4cplus/fileappender.h"
#include "../log4cplus/layout.h"
#include "../log4cplus/ndc.h"
#include "../log4cplus/log4.h"
#include "../log4cplus/spi/loggingevent.h"
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <ctime>
#include <sstream>
#include <pthread.h>
#include <unistd.h>
#include <cstring>
//目录创建
#include <sys/stat.h>
#include <sys/types.h>
//kafka结构定义
#include "../json/mms_json_inter.h"


#include "../mms/rdb_client.h"
#include "../include/node.h"//lnk20241223


extern unsigned int	g_node_id;
extern int          g_front_seg_index;
extern std::string  FRONT_INST;
extern char			subdir[128];
extern node_t*      g_node;


//mq
extern QMutex kafka_data_list_mutex; //Kafka发送数据锁
extern QList<Ckafka_data_t> kafka_data_list; //kafka发送数据链表

//辅助函数
extern std::string intToString(int number);

//日志主题
extern std::string G_LOG_TOPIC;

//log4命名空间
using namespace log4cplus;
using namespace log4cplus::helpers;

////////////////////////////////////////////////////////辅助函数
std::string get_front_type_from_subdir() {
    if (std::strstr(subdir, "cfg_3s_data") != NULL)
        return "realTime";
    else if (std::strstr(subdir, "cfg_soe_comtrade") != NULL)
        return "comtrade";
    else if (std::strstr(subdir, "cfg_recallhis_data") != NULL)
        return "recall";
    else if (std::strstr(subdir, "cfg_stat_data") != NULL)
        return "stat";
    else
        return "unknown";
}

// 递归创建目录
bool create_directory_recursive(const std::string& path) {
    size_t pos = 0;
    std::string current;
    while (pos != std::string::npos) {
        pos = path.find('/', pos + 1);
        current = path.substr(0, pos);
        if (!current.empty() && access(current.c_str(), F_OK) != 0) {
            if (mkdir(current.c_str(), 0755) != 0) {
                perror(("mkdir failed: " + current).c_str());
                return false;
            }
        }
    }
    return true;
}
//////////////////////////////////////////////////////////////////////
std::string extract_logger_id(const std::string& logger_name) {
    size_t pos = logger_name.find('.');
    if (pos != std::string::npos && pos + 1 < logger_name.size()) {
        return logger_name.substr(pos + 1);
    }
    return ""; // 没有找到 '.' 或 '.' 后为空
}

std::string get_level_str(int level) {
    switch (level) {
        case 10000: return "DEBUG";
        case 20000: return "NORMAL";  // 或 "INFO" 根据你业务定义
        case 30000: return "WARN";
        case 40000: return "ERROR";
        default:    return "UNKNOWN";
    }
}
//////////////////////////////////////////////////////////////////////
TypedLogger::TypedLogger() {}
TypedLogger::TypedLogger(const Logger& l, int t) : logger(l), logtype(t) {}

DebugSwitch::DebugSwitch() : debug_open(false), min_level(WARN_LOG_LEVEL) {}
void DebugSwitch::open() { debug_open = true; }
void DebugSwitch::close() {
    debug_open = false;
    targets.clear();
    type_enable.clear();
}
void DebugSwitch::set_target(const std::string& name) { targets.insert(name); }
void DebugSwitch::set_level(int level) { min_level = level; }
void DebugSwitch::enable_type(int type) { type_enable[type] = true; }
void DebugSwitch::disable_type(int type) { type_enable[type] = false; }

bool DebugSwitch::match(const std::string& logger_name, int level, int logtype) {
    if (!debug_open) return false;
    if (level < min_level) return false;
    if (type_enable.count(logtype) && !type_enable[logtype]) return false;
    std::set<std::string>::iterator it;
    for (it = targets.begin(); it != targets.end(); ++it) {
        if (logger_name.find(*it) != std::string::npos)
            return true;
    }
    return false;
}

std::map<std::string, TypedLogger> logger_map;
DebugSwitch g_debug_switch;

class SendAppender : public Appender {
protected:
    void append(const spi::InternalLoggingEvent& event) {
        std::string logger_name = event.getLoggerName();
        int level = event.getLogLevel();
        std::string msg = event.getMessage();

        int logtype = (logger_name.find(".COM") != std::string::npos) ? LOGTYPE_COM : LOGTYPE_DATA;
        std::string level_str;
        if (logger_name.find("process") == 0)
            level_str = "process";
        else if (logger_name.find("monitor") != std::string::npos)
            level_str = "measurepoint";
        else
            level_str = "terminal";

        if (level == ERROR_LOG_LEVEL || level == WARN_LOG_LEVEL || g_debug_switch.match(logger_name, level, logtype)) {
            std::ostringstream oss;
            oss << "{\"processNo\":\"" << intToString(g_front_seg_index)
                << "\",\"nodeId\":\"" << FRONT_INST
                << "\",\"businessId\":\"" << extract_logger_id(logger_name)
                << "\",\"level\":\"" << level_str
                << "\",\"grade\":\"" << get_level_str(level)
                << "\",\"logtype\":\"" << (logtype == LOGTYPE_COM ? "com" : "data")
                << "\",\"frontType\":\"" << get_front_type_from_subdir()
                << "\",\"log\":\"" << escape_json(msg) << "\"}";

            std::string jsonString = oss.str();

            Ckafka_data_t connect_info;
            connect_info.strTopic = QString::fromStdString(G_LOG_TOPIC);
            connect_info.strText = QString::fromStdString(jsonString);

            kafka_data_list_mutex.lock();
            kafka_data_list.append(connect_info);
            kafka_data_list_mutex.unlock();
        }
    }

    std::string escape_json(const std::string& input) {
        std::ostringstream ss;
        for (unsigned int i = 0; i < input.size(); ++i) {
            switch (input[i]) {
                case '\\': ss << "\\\\"; break;
                case '"': ss << "\\\""; break;
                case '\n': ss << "\\n"; break;
                case '\r': ss << "\\r"; break;
                case '\t': ss << "\\t"; break;
                default: ss << input[i]; break;
            }
        }
        return ss.str();
    }

    virtual void close() {
        // 可空实现
    }

public:
    SendAppender() {}
    virtual ~SendAppender() {
        destructorImpl();  // 重要！释放 log4cplus 基类资源
    }
};

//用来控制日志上送的结构
struct LOGEntry {
    std::string id;
    std::string level; // terminal / measurepoint
    int logtype;       // com / data
    int min_grade;
    int countdown;
};

//日志上送map管理
std::map<std::string, LOGEntry> g_log_entries;
pthread_mutex_t g_log_mutex = PTHREAD_MUTEX_INITIALIZER;

// 生成唯一 key
std::string build_debug_key(const std::string& id, const std::string& level, int logtype) {
    return id + "|" + level + "|" + (logtype == 1 ? "COM" : "DATA");
}

// 外部线程中调用：每秒更新所有倒计时，0 则删除
void update_log_entries_countdown() {
    pthread_mutex_lock(&g_log_mutex);
    std::map<std::string, LOGEntry>::iterator it = g_log_entries.begin();
    while (it != g_log_entries.end()) {
        if (it->second.countdown > 0) {
            it->second.countdown--;
            if (it->second.countdown == 0) {
                std::cout << "[LOG] debug日志上送自动关闭: " << it->first << std::endl;
                it = g_log_entries.erase(it);
                continue;
            }
        }
        ++it;
    }
    pthread_mutex_unlock(&g_log_mutex);
}

void process_log_command(const std::string& id, const std::string& level, const std::string& grade, const std::string& logtype_str) {
    if (level != "terminal" && level != "measurepoint") return;

    int type = (logtype_str == "com") ? LOGTYPE_COM : LOGTYPE_DATA;
    int grade_level = (grade == "DEBUG") ? DEBUG_LOG_LEVEL : INFO_LOG_LEVEL;

    std::string key = build_debug_key(id, level, type);

    pthread_mutex_lock(&g_log_mutex);

    LOGEntry& entry = g_log_entries[key];  // 会自动 insert 或取已有
    entry.id = id;
    entry.level = level;
    entry.logtype = type;
    entry.min_grade = grade_level;
    entry.countdown = 60;  // 重置倒计时

    pthread_mutex_unlock(&g_log_mutex);

}

Logger init_logger(const std::string& full_name, const std::string& file_dir, const std::string& base_file, SharedAppenderPtr fileAppender) {
    create_directory_recursive(file_dir);

    Logger logger = Logger::getInstance(full_name);
    if (!fileAppender) {
        std::string file_path = file_dir + "/" + base_file + ".log";
        fileAppender = SharedAppenderPtr(new RollingFileAppender(file_path, 1 * 1024 * 1024, 2));
        fileAppender->setLayout(std::auto_ptr<Layout>(
            new PatternLayout("%D{%Y-%m-%d %H:%M:%S} [%p] [%c] %m%n")));
    }

    SharedAppenderPtr sendAppender(new SendAppender());
    logger.addAppender(fileAppender);
    logger.addAppender(sendAppender);
    logger.setLogLevel(DEBUG_LOG_LEVEL);
    return logger;
}

// 重载版本：无 Appender 传入时调用上面的实现
log4cplus::Logger init_logger(const std::string& full_name,
                              const std::string& file_dir,
                              const std::string& base_file) {
    return init_logger(full_name, file_dir, base_file,
                       log4cplus::SharedAppenderPtr());  // 空指针
}

//进程的日志
void init_logger_process() {
    std::string base_dir = std::string("/FeProject/") + subdir + "/processNo" + intToString(g_front_seg_index) + "/log";
    logger_map["process"] = TypedLogger(init_logger(std::string("process"), base_dir, std::string("process")), LOGTYPE_DATA);
    std::cout << "process log init ok" << std::endl;
}

//终端的日志
void init_loggers_bydevid(const char* dev_id) 
{
    if (!dev_id) return;

    std::string terminal_id(dev_id);  // 转为 std::string
    std::string base_dir = std::string("/FeProject/") + subdir + "/processNo" + intToString(g_front_seg_index) + "/log";

    ied_t* ied = NULL;
	int			iedno;
	ied_usr_t* ied_usr = NULL;

    for (iedno = 0; iedno < g_node->n_clients; iedno++) {
        ied = g_node->clients[iedno];

        if (!ied || !ied->usr_ext) {
            std::cout << "ied No."<< iedno << " is null" << std::endl;
            continue;
        }

        ied_usr = (ied_usr_t*)ied->usr_ext;

        //跳过不匹配的终端
        if (strcmp(ied_usr->terminal_id, dev_id) != 0) continue;

        const char* ip_cstr = (ied->channel[0].addr_str != NULL) ? ied->channel[0].addr_str : "unknown";

        std::string ip_str(ip_cstr);

        std::string device_dir = base_dir + "/" + ip_str;

        std::string device_key_c = std::string("terminal.") + dev_id + ".COM";
        std::string device_key_d = std::string("terminal.") + dev_id + ".DATA";

        // 添加判断：终端日志 logger 是否已存在
        if (logger_map.find(device_key_c) == logger_map.end() &&
            logger_map.find(device_key_d) == logger_map.end()) {

            // 所有终端日志（com 和 data）写到同一个 device 日志文件中
            std::string file_path_t = device_dir + "/" + dev_id + ".log";

            // 共用一个 appender 实例
            SharedAppenderPtr device_appender = SharedAppenderPtr(new RollingFileAppender(file_path_t, 1 * 1024 * 1024, 2));
            device_appender->setLayout(std::auto_ptr<Layout>(new PatternLayout("%D{%Y-%m-%d %H:%M:%S} [%p] [%c] %m%n")));

            Logger device_logger_c = init_logger(device_key_c, device_dir, dev_id, device_appender); //用终端id作为日志文件名
            Logger device_logger_d = init_logger(device_key_d, device_dir, dev_id, device_appender); //用终端id作为日志文件名
            logger_map[device_key_c]  = TypedLogger(device_logger_c, LOGTYPE_COM);
            logger_map[device_key_d] = TypedLogger(device_logger_d, LOGTYPE_DATA);

            DIY_WARNLOG(device_key_d.c_str(),"【WARN】终端id:%s终端级日志初始化完毕", ied_usr->terminal_id);
        }

        // 初始化监测点
        // 监测点 logger 名称格式：monitor.<mp_id>.COM / .DATA
        for (int i = 0; i < 10; ++i) {
            if (strlen(ied_usr->LD_info[i].mp_id) > 0){
                std::ostringstream mon_key_c, mon_key_d, mon_path, mon_name;
                mon_key_c << "monitor." << ied_usr->LD_info[i].mp_id << ".COM";
                mon_key_d << "monitor." << ied_usr->LD_info[i].mp_id << ".DATA";
                mon_path << device_dir << "/monitor" << i;//终端路径下用monitor+序号作为目录
                mon_name << ied_usr->LD_info[i].mp_id;

                // 添加判断：监测点 logger 是否已存在
                if (logger_map.find(mon_key_c.str()) == logger_map.end() &&
                    logger_map.find(mon_key_d.str()) == logger_map.end()) {

                    // 所有监测点日志（com 和 data）写到同一个 monitor 日志文件中
                    std::string file_path_m = mon_path.str() + "/" + mon_name.str() + ".log";

                    // 共用一个 appender 实例
                    SharedAppenderPtr monitor_appender = SharedAppenderPtr(new RollingFileAppender(file_path_m, 1 * 1024 * 1024, 2));
                    monitor_appender->setLayout(std::auto_ptr<Layout>(new PatternLayout("%D{%Y-%m-%d %H:%M:%S} [%p] [%c] %m%n")));

                    Logger mon_logger_c = init_logger(mon_key_c.str(), mon_path.str(), mon_name.str(),monitor_appender);//用监测点号作为日志文件名
                    Logger mon_logger_d = init_logger(mon_key_d.str(), mon_path.str(), mon_name.str(),monitor_appender);
                    logger_map[mon_key_c.str()] = TypedLogger(mon_logger_c, LOGTYPE_COM);
                    logger_map[mon_key_d.str()] = TypedLogger(mon_logger_d, LOGTYPE_DATA);

                    DIY_WARNLOG(mon_key_d.str().c_str(),"【WARN】监测点:%s - id:%s监测点级日志初始化完毕", ied_usr->LD_info[i].name,ied_usr->LD_info[i].mp_id);
                }
            }
            
        }

        break; // 只匹配一个 terminal_id
    }
}

void init_loggers() {
    std::string base_dir = std::string("/FeProject/") + subdir + "/processNo" + intToString(g_front_seg_index) + "/log";

    ied_t* ied = NULL;
	int			iedno;
	ied_usr_t* ied_usr = NULL;

    for (iedno = 0; iedno < g_node->n_clients; iedno++) {
        ied = g_node->clients[iedno];

        if (!ied || !ied->usr_ext) {
            std::cout << "ied No."<< iedno << " is null" << std::endl;
            continue;
        }

        ied_usr = (ied_usr_t*)ied->usr_ext;

        const char* ip_cstr = (ied->channel[0].addr_str != NULL) ? ied->channel[0].addr_str : "unknown";

        std::string ip_str(ip_cstr);

        std::string device_dir = base_dir + "/" + ip_str;

        std::string device_key_c = std::string("terminal.") + ied_usr->terminal_id + ".COM";
        std::string device_key_d = std::string("terminal.") + ied_usr->terminal_id + ".DATA";

        // 所有终端日志（com 和 data）写到同一个 device 日志文件中
        std::string file_path_t = device_dir + "/" + ied_usr->terminal_id + ".log";

        // 共用一个 appender 实例
        SharedAppenderPtr device_appender = SharedAppenderPtr(new RollingFileAppender(file_path_t, 1 * 1024 * 1024, 2));
        device_appender->setLayout(std::auto_ptr<Layout>(new PatternLayout("%D{%Y-%m-%d %H:%M:%S} [%p] [%c] %m%n")));

        Logger device_logger_c = init_logger(device_key_c, device_dir, ied_usr->terminal_id, device_appender); //用终端id作为日志文件名
        Logger device_logger_d = init_logger(device_key_d, device_dir, ied_usr->terminal_id, device_appender); //用终端id作为日志文件名

        logger_map[device_key_c]  = TypedLogger(device_logger_c, LOGTYPE_COM);
        logger_map[device_key_d] = TypedLogger(device_logger_d, LOGTYPE_DATA);

        DIY_WARNLOG(device_key_d.c_str(),"【WARN】终端id:%s终端级日志初始化完毕", ied_usr->terminal_id);

        // 初始化监测点
        // 监测点 logger 名称格式：monitor.<mp_id>.COM / .DATA
        for (int i = 0; i < 10; ++i) {
            if (strlen(ied_usr->LD_info[i].mp_id) > 0){
                std::ostringstream mon_key_c, mon_key_d, mon_path, mon_name;
                mon_key_c << "monitor." << ied_usr->LD_info[i].mp_id << ".COM";
                mon_key_d << "monitor." << ied_usr->LD_info[i].mp_id << ".DATA";

                mon_path << device_dir << "/monitor" << i;//终端路径下用monitor+序号作为目录
                mon_name << ied_usr->LD_info[i].mp_id;

                std::string file_path_m = mon_path.str() + "/" + mon_name.str() + ".log";

                // 共用一个 appender 实例
                SharedAppenderPtr monitor_appender = SharedAppenderPtr(new RollingFileAppender(file_path_m, 1 * 1024 * 1024, 2));
                monitor_appender->setLayout(std::auto_ptr<Layout>(new PatternLayout("%D{%Y-%m-%d %H:%M:%S} [%p] [%c] %m%n")));

                Logger mon_logger_c = init_logger(mon_key_c.str(), mon_path.str(), mon_name.str(), monitor_appender);
                Logger mon_logger_d = init_logger(mon_key_d.str(), mon_path.str(), mon_name.str(), monitor_appender);

                logger_map[mon_key_c.str()] = TypedLogger(mon_logger_c, LOGTYPE_COM);
                logger_map[mon_key_d.str()] = TypedLogger(mon_logger_d, LOGTYPE_DATA);

                DIY_WARNLOG(mon_key_d.str().c_str(),"【WARN】监测点:%s - id:%s监测点级日志初始化完毕", ied_usr->LD_info[i].name,ied_usr->LD_info[i].mp_id);

            }
            
        }

    }
}

void remove_loggers_by_terminal_id(const char* terminal_id_cstr) {
    if (!g_node || !terminal_id_cstr) return;

    std::string terminal_id(terminal_id_cstr);  // 转为 std::string

    for (int iedno = 0; iedno < g_node->n_clients; ++iedno) {
        ied_t* ied = g_node->clients[iedno];
        if (!ied || !ied->usr_ext) continue;

        ied_usr_t* ied_usr = (ied_usr_t*)ied->usr_ext;
        if (strcmp(ied_usr->terminal_id, terminal_id.c_str()) != 0) continue;

        // 删除终端日志 logger
        std::string com_key = "terminal." + terminal_id + ".COM";
        std::string data_key = "terminal." + terminal_id + ".DATA";

        if (logger_map.count(com_key)) {
            logger_map[com_key].logger.removeAllAppenders();
            logger_map.erase(com_key);
        }

        if (logger_map.count(data_key)) {
            logger_map[data_key].logger.removeAllAppenders();
            logger_map.erase(data_key);
        }

        // 删除监测点日志 logger
        for (int i = 0; i < 10; ++i) {
            const char* mp_id = ied_usr->LD_info[i].mp_id;
            if (strlen(mp_id) > 0) {
                std::string mon_prefix = std::string("monitor.") + mp_id;

                std::string mon_com_key = mon_prefix + ".COM";
                std::string mon_data_key = mon_prefix + ".DATA";

                if (logger_map.count(mon_com_key)) {
                    logger_map[mon_com_key].logger.removeAllAppenders();
                    logger_map.erase(mon_com_key);
                }

                if (logger_map.count(mon_data_key)) {
                    logger_map[mon_data_key].logger.removeAllAppenders();
                    logger_map.erase(mon_data_key);
                }
            }
        }

        std::cout << "[LOG] Logger for terminal_id=" << terminal_id << " removed.\n";
        break; // 找到匹配终端后退出
    }
}



#ifdef __cplusplus
extern "C" {
#endif

    // 公共函数
    void log4_log_with_level(const char* key, const char* msg, int level) {
        std::map<std::string, TypedLogger>::iterator it = logger_map.find(key);
        if (it == logger_map.end()) return;
    
        Logger logger = it->second.logger;
        switch (level) {
            case 0: LOG4CPLUS_DEBUG(logger, msg); break;
            case 1: LOG4CPLUS_INFO(logger, msg);  break;
            case 2: LOG4CPLUS_WARN(logger, msg);  break;
            case 3: LOG4CPLUS_ERROR(logger, msg); break;
            default: break;
        }
    }
    
    // 四个包装函数
    void log_debug(const char* key, const char* msg) { log4_log_with_level(key, msg, 0); }
    void log_info (const char* key, const char* msg) { log4_log_with_level(key, msg, 1); }
    void log_warn (const char* key, const char* msg) { log4_log_with_level(key, msg, 2); }
    void log_error(const char* key, const char* msg) { log4_log_with_level(key, msg, 3); }

    void send_reply_to_kafka_c(const char* guid, const char* step, const char* result) {
        send_reply_to_kafka(std::string(guid), std::string(step), std::string(result));
    }

    //标准化日志接口
    void format_log_msg(char* buf, size_t buf_size, const char* fmt, ...) {
        // 写入时间
        time_t now = time(NULL);
        struct tm tm_info;
        localtime_r(&now, &tm_info);
        strftime(buf, buf_size, "%Y-%m-%d %H:%M:%S ", &tm_info);  // 时间+空格

        // 处理可变参数并写入剩余内容
        va_list args;
        va_start(args, fmt);
        vsnprintf(buf + strlen(buf), buf_size - strlen(buf), fmt, args);
        va_end(args);
    }
    
#ifdef __cplusplus
}
#endif


/*
int main() {
    initialize();
    init_loggers();

    LOG4CPLUS_INFO(logger_map["process.1.data"].logger, "程序启动，PID=" << getpid());

    LOG4CPLUS_WARN(logger_map["terminal.192.168.1.1.com"].logger, "串口异常，尝试重连");
    LOG4CPLUS_INFO(logger_map["terminal.192.168.1.1.data"].logger, "终端运行状态正常");
    LOG4CPLUS_DEBUG(logger_map["measurepoint.192.168.1.1.monitor1.data"].logger, "电压测量值=220.1V");

    process_log_command("192.168.1.1.monitor1", "measurepoint", "DEBUG", "data");
    LOG4CPLUS_DEBUG(logger_map["measurepoint.192.168.1.1.monitor1.data"].logger, "频率测量值=50.0Hz");

    sleep(65);
    LOG4CPLUS_ERROR(logger_map["terminal.192.168.1.2.data"].logger, "终端掉线");

    return 0;
}
*/