package click.kamil.examples.statemachine.extended.config;

import click.kamil.examples.statemachine.extended.constants.MyEvents;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.beans.factory.annotation.Value;

@Configuration
@EnableStateMachine(name = "extendedStateMachine")
public class ExtendedStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {

    @Value("${app.states.initial:START}")
    private String initialState;

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

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions
            .withExternal()
                .source("START").target("PROCESSING")
                .event(MyEvents.SUBMIT)
            .and()
            .withExternal()
                .source("PROCESSING").target("COMPLETED")
                .event("FINISH")
            .and()
            .withExternal()
                .source("PROCESSING").target("CANCELLED")
                .event(MyEvents.CANCEL)
            .and()
            .withExternal()
                .source("START").target("PROCESSING")
                .event("REACTIVE_EVENT")
            .and()
            .withExternal()
                .source("START").target("START")
                .event("AUDIT_EVENT")
            .and()
            .withExternal()
                .source("START").target("PROCESSING")
                .event("EXTERNAL_TRIGGER");
    }
}
