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

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;

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

    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
        states
            .withStates()
                .initial("NEW")
                .state("AUTHORIZED")
                .state("CAPTURED")
                .state("DECLINED")
                .state("QUIRK1")
                .state("QUIRK2")
                .state("QUIRK3")
                .state("QUIRK4");
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions
            .withExternal()
                .source("NEW").target("AUTHORIZED")
                .event("AUTHORIZE")
            .and()
            .withExternal()
                .source("AUTHORIZED").target("CAPTURED")
                .event("CAPTURE")
            .and()
            .withExternal()
                .source("NEW").target("DECLINED")
                .event("DECLINE")
            .and()
            .withExternal()
                .source("NEW").target("QUIRK1")
                .event("PRIMARY_EVENT")
            .and()
            .withExternal()
                .source("NEW").target("QUIRK2")
                .event("NAMED_EVENT")
            .and()
            .withExternal()
                .source("NEW").target("QUIRK3")
                .event("QUALIFIER_EVENT")
            .and()
            .withExternal()
                .source("NEW").target("QUIRK4")
                .event("FALLBACK_EVENT");
    }
}
