package com.xisc.pm.app.service.autoboard.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.mongodb.core.MongoTemplate;

import com.xisc.pm.app.service.autoboard.dto.PmAutoPdLog;

import io.choerodon.core.convertor.ApplicationContextHelper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CalcLog {

    private static MongoTemplate mongoTemplate;

    private static final ThreadLocal<Integer> VERSION = new ThreadLocal<>();
    
    private static final ThreadLocal<Boolean> LOG_COLLECT_FLAG = new ThreadLocal<>();
    
    private static final ThreadLocal<Integer> LOG_COUNT = new ThreadLocal<>();

    public static final String COLLECTION_AUTO_PD_LOG = "pmAutoPdLog";

    private static final int BATCH_SIZE = 500;
    
    private static ThreadLocal<List<PmAutoPdLog>> LOGS = new ThreadLocal<List<PmAutoPdLog>>();

    public static void setVersion(Integer version) {
        VERSION.set(version);
    }
    
    public static void openLog() {
        LOG_COLLECT_FLAG.set(true);
    }
    
    public static void closeLog() {
        LOG_COLLECT_FLAG.set(false);
    }

    public static void clear() {
        if(LOG_COLLECT_FLAG.get() != null && LOG_COLLECT_FLAG.get()) {
            flush();
            LOG_COUNT.remove();
        }
        VERSION.remove();
        LOGS.remove();
    }

    public static void log(List<String> block1, List<String> block2, String message) {
        StringBuffer sb = new StringBuffer();
        if (block1 != null) {
            sb.append("block1=[").append(String.join(",", block1)).append("]");
        }
        if (block2 != null) {
            sb.append(" block2=[").append(String.join(",", block2)).append("]");
        }
        sb.append(" " + message);
        log(sb.toString());
    }

    public static synchronized void flush() {
        if (LOGS.get() == null || LOGS.get().isEmpty()) {
            return;
        }
        if (mongoTemplate == null) {
            mongoTemplate = ApplicationContextHelper.getContext().getBean(MongoTemplate.class);
        }
        if (!mongoTemplate.collectionExists(COLLECTION_AUTO_PD_LOG)) {
            mongoTemplate.createCollection(COLLECTION_AUTO_PD_LOG);
        }
        if(LOG_COUNT.get() != null && LOG_COUNT.get() > 500000) {
            // 如果单版本最多记录指定条日志，避免日志过大
            return ;
        }
        try {
            mongoTemplate.insert(LOGS.get(), COLLECTION_AUTO_PD_LOG);
            if(LOG_COUNT.get() == null) {
                LOG_COUNT.set(LOGS.get().size());
            } else {
                LOG_COUNT.set(LOG_COUNT.get() + LOGS.get().size());
            }
        } catch (Exception e) {
            log.error(e.getMessage() , e);
        } finally {
            LOGS.get().clear();
            LOGS.set(new ArrayList<>());
        }
        
    }

    public static void log(PmAutoPdLog log) {
        if(LOG_COLLECT_FLAG.get() != null && LOG_COLLECT_FLAG.get()) {
            if (LOGS.get() == null) {
                LOGS.set(new ArrayList<>());
            }
            LOGS.get().add(log);
            if (LOGS.get().size() >= BATCH_SIZE) {
                flush();
            }
        }
    }

    public static void log(String message) {
        String version = VERSION.get() == null ? "" : VERSION.get().toString();
//        log.info(String.format("执行版本 %s ：%s", version , message));
    }

}
