package com.lanyuanxiaoyao.leopard.core.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.leopard.core.entity.Task;
import com.lanyuanxiaoyao.leopard.core.repository.TaskRepository;
import com.lanyuanxiaoyao.leopard.core.task.PyramidSelect;
import com.lanyuanxiaoyao.leopard.core.task.TaskRunner;
import com.lanyuanxiaoyao.leopard.core.task.UpdateDailyTask;
import com.lanyuanxiaoyao.leopard.core.task.UpdateFinanceIndicatorTask;
import com.lanyuanxiaoyao.leopard.core.task.UpdateStockTask;
import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * @author lanyuanxiaoyao
 * @version 20250829
 */
@Slf4j
@Service
public class TaskService extends SimpleServiceSupport<Task> {
  private final ExecutorService executor = Executors.newFixedThreadPool(50);
  private final ApplicationContext context;

  @Getter
  private final Set<TaskTemplate> templates = Stream.of(
    new TaskTemplate("b29f76a5-b07d-4182-85f8-2641c2a975c1", "更新股票信息", "更新股票信息", UpdateStockTask.class),
    new TaskTemplate("b9df25ce-aa55-4f73-8265-d8a724614177", "更新日线数据", "更新日线数据", UpdateDailyTask.class),
    new TaskTemplate("8ab30478-c81f-4bbf-94dd-7e05fa537b50", "更新财务指标", "更新财务指标", UpdateFinanceIndicatorTask.class),
    new TaskTemplate("a6a7b569-a171-481b-9184-716925571639", "金字塔选股", "金字塔选股", PyramidSelect.class)
  ).collect(Collectors.toSet());
  private final Map<String, TaskTemplate> templateMap = templates.stream()
    .collect(Collectors.toMap(TaskTemplate::id, template -> template));

  public TaskService(TaskRepository repository, ApplicationContext context) {
    super(repository);
    this.context = context;
  }

  public TaskTemplate getTemplate(String templateId) {
    return templateMap.get(templateId);
  }

  public void execute(String templateId, Map<String, Object> params) {
    execute(templateId, params, true);
  }

  public void execute(String templateId, Map<String, Object> params, boolean async) {
    var template = templateMap.get(templateId);
    if (ObjectUtil.isNull(template)) {
      throw new RuntimeException("任务模板不存在");
    }
    var instance = context.getBean(template.runnerClass());
    if (async) {
      executor.submit(() -> instance.run(template, params));
    } else {
      instance.run(template, params);
    }
  }

  public record TaskTemplate(
    String id,
    String name,
    String description,
    Class<? extends TaskRunner> runnerClass
  ) {
    public TaskTemplate(String name, String description, Class<? extends TaskRunner> runnerClass) {
      this(IdUtil.fastUUID(), name, description, runnerClass);
    }
  }
}
