package org.springblade.flow.engine.custom.life;


import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.ProcessEngineLifecycleListener;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;


/**
 * @Author:LoveMyOrange
 * @Description:
 * @Date:Created in 2021/12/17 14:11
 */
@Component
@Slf4j
public class CustomProcessEngineLifecycleListener implements ProcessEngineLifecycleListener {

    /*
  扩展Activiti
  流程引擎启动之前  的监听器
  可以判断 系统是否有  生成流程图的可用字体
  一定必须要有 中文字体, 不然 字体直接乱码
  使其系统有更强的一致性
   */
    @Override
    public void onProcessEngineBuilt(ProcessEngine processEngine) {
        ProcessEngineConfiguration processEngineConfiguration = processEngine.getProcessEngineConfiguration();
        //尽量建议将此开开 这样后期流程设计器更换是可以兼容的
        boolean createDiagramOnDeploy = processEngineConfiguration.isCreateDiagramOnDeploy();
        if(createDiagramOnDeploy){
            String activityFontName = processEngineConfiguration.getActivityFontName();
            String annotationFontName = processEngineConfiguration.getAnnotationFontName();
            String labelFontName = processEngineConfiguration.getLabelFontName();

            //获取系统可用字体
//            GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
//            //
//            String[] availableFontFamilyNames = localGraphicsEnvironment.getAvailableFontFamilyNames();
//            List<String> fontNames = Arrays.asList(availableFontFamilyNames);
//            if( !fontNames.contains(activityFontName) || !fontNames.contains(annotationFontName) ||!fontNames.contains(labelFontName)){
//                StringBuffer sb = new StringBuffer();
//                sb.append("缺少可用的字体,需要的字体为[")
//                        .append(activityFontName)
//                        .append(",")
//                        .append(annotationFontName)
//                        .append(",")
//                        .append(labelFontName)
//                        .append("]")
//                        .append("请给系统/虚拟机/Docker镜像加入相关的字体,然后重启本系统");
////                throw   new RuntimeException(sb.toString());
//            }
        }
        log.info("流程引擎成功启动");
    }
    //工作流引擎销毁的时候执行
    @Override
    public void onProcessEngineClosed(ProcessEngine processEngine) {
        log.info("流程引擎关闭");
    }
}
