package io.gisbi.listener;

import io.gisbi.datasource.dao.auto.entity.CoreDatasourceTask;
import io.gisbi.datasource.manage.DataSourceManage;
import io.gisbi.datasource.manage.DatasourceSyncManage;
import io.gisbi.datasource.manage.EngineManage;
import io.gisbi.datasource.provider.CalciteProvider;
import io.gisbi.datasource.server.DatasourceServer;
import io.gisbi.datasource.server.DatasourceTaskServer;
import io.gisbi.system.dao.auto.entity.CoreSysSetting;
import io.gisbi.system.manage.SysParameterManage;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@Order(value = 2)
public class DataSourceInitStartListener implements ApplicationListener<ApplicationReadyEvent> {
    @Resource
    private DatasourceSyncManage datasourceSyncManage;
    @Resource
    private DatasourceServer datasourceServer;
    @Resource
    private DataSourceManage dataSourceManage;
    @Resource
    private DatasourceTaskServer datasourceTaskServer;
    @Resource
    private CalciteProvider calciteProvider;
    @Resource
    private EngineManage engineManage;
    @Resource
    private SysParameterManage sysParameterManage;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        try {
            engineManage.initSimpleEngine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            calciteProvider.initConnectionPool();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<CoreDatasourceTask> list = datasourceTaskServer.listAll();
        for (CoreDatasourceTask task : list) {
            try {
                if (!StringUtils.equalsIgnoreCase(task.getSyncRate(), DatasourceTaskServer.ScheduleType.RIGHTNOW.toString())) {
                    if (task.getEndTime() != null && task.getEndTime() > 0) {
                        if (task.getEndTime() > System.currentTimeMillis()) {
                            datasourceSyncManage.addSchedule(task);
                        } else {
                            datasourceSyncManage.deleteSchedule(task);
                        }
                    } else {
                        datasourceSyncManage.addSchedule(task);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            List<CoreSysSetting> coreSysSettings = sysParameterManage.groupList("basic.");
            datasourceServer.addJob(coreSysSettings);
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataSourceManage.encryptDsConfig();
    }


}
