package quant.rich.emoney.controller.api;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.reflections.Reflections;

import lombok.extern.slf4j.Slf4j;
import quant.rich.emoney.interfaces.IQueryableEnum;
import quant.rich.emoney.service.postgre.StockStrategyService;

@RestController
@RequestMapping("/api/v1/common")
@Slf4j
public class CommonAbilityControllerV1 {
    
    @Autowired
    Reflections reflections;
    
    @Autowired
    ObjectMapper objectMapper;
    
    @Autowired
    StockStrategyService stockStrategyService;
    
    @GetMapping("/test")
    void test() throws IOException, InterruptedException, ExecutionException {
        String str = Files.readString(Path.of("C:\\Users\\Administrator\\Desktop\\stock_strategy.json"));
        JsonNode node = objectMapper.readTree(str);
        Future<Boolean> future = stockStrategyService.updateByQueryResponseAsync(node);
        future.get();
    }
    
    @GetMapping("/getIQueryableEnum")
    public Map<String, String> getIQueryableEnum(String enumName) {
        Set<Class<? extends IQueryableEnum>> enums = reflections.getSubTypesOf(IQueryableEnum.class);
        Map<String, String> map = new HashMap<>();
        for (Class<? extends IQueryableEnum> clazz : enums) {
            if (clazz.getSimpleName().equals(enumName) && clazz.isEnum()) {
                @SuppressWarnings({ "unchecked", "rawtypes" })
                Class<? extends Enum> enumClass = (Class<? extends Enum<?>>) clazz;
                for (Enum<?> e : enumClass.getEnumConstants()) {
                    if (e instanceof IQueryableEnum iqe) {
                        map.put(e.name(), iqe.getNote());
                    }
                }
                break;
            }
        }
        return map;
    }

}
