package click.kamil.multi.core;

import click.kamil.multi.api.ProcessAction;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

@Configuration
@EnableStateMachineFactory
public class OrderStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {

    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
        states
            .withStates()
            .initial("NEW")
            .state("PROCESSING")
            .end("COMPLETED");
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions
            .withExternal()
                .source("NEW")
                .target("PROCESSING")
                .event("SUBMIT")
                .action(processAction())
            .and()
            .withExternal()
                .source("PROCESSING")
                .target("COMPLETED")
                .event("FINISH");
    }

    @Bean
    public ProcessAction processAction() {
        return new ProcessAction() {};
    }
}
