package org.dromara.config;

import cn.hutool.core.io.file.FileReader;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.CamelContext;
import org.apache.camel.Route;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dsl.xml.jaxb.CamelJaxbXmlRoutesBuilderLoader;
import org.apache.camel.spi.Resource;
import org.apache.camel.support.ResourceHelper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CamelRouterConfig  {
    @Autowired
    private CamelContext camelContext;

    public boolean camelRouterRun(String xmlContent){
        try {
            CamelContext camelContext = this.camelContext;
            Resource resource = ResourceHelper.fromString("a.xml", xmlContent);
            CamelJaxbXmlRoutesBuilderLoader xmlRoutesBuilderLoader = new CamelJaxbXmlRoutesBuilderLoader();

            RouteBuilder routeBuilder = xmlRoutesBuilderLoader.doLoadRouteBuilder(resource);
            camelContext.addRoutes(routeBuilder);

            camelContext.getRouteController().startAllRoutes();
            return true;
        } catch (Exception e) {
            log.error("加载路由文件运行错误：{}\r\n ----------------------\r\n message:{}",xmlContent,e.getMessage());
        }
        return false;
    }
    public  boolean camelRouterRemove(String routeId){
        try {
            for (Route route : camelContext.getRoutes()) {
                if(route.getId().equals(routeId)||route.getId().split("__")[0].equals(routeId)){
                    camelContext.removeRoute(route.getId());
                    camelContext.getRouteController().stopRoute(route.getId());
                }
            }
            return true;
        }catch (Exception e){
            log.error("停止路由失败：{}",e.getMessage());
        }
        return false;
    }




}
