package com.alterdekim.hearthhack.component;

import com.alterdekim.hearthhack.config.DBFConfig;
import com.alterdekim.hearthhack.config.ServerConfig;
import com.alterdekim.hearthhack.parser.DBFParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;

@Slf4j
@Component
public class StartupListener {

    @Autowired
    private DBFConfig dbfConfig;

    @Autowired
    private ServerConfig serverConfig;

    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            dbfConfig.setCards(DBFParser.parseCards(serverConfig.getDbfPath()));
            dbfConfig.setClientConfig(
                    new String(
                            Files.readAllBytes(
                                    Path.of(
                                            serverConfig.getClientConfigPath()
                                    )
                            )
                    )
            );
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}