package click.kamil.examples.statemachine.layered.order.config;

import click.kamil.examples.statemachine.layered.order.OrderState;
import click.kamil.examples.statemachine.layered.order.OrderTransitionEvent;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

public abstract class AbstractOrderStateMachineConfiguration
        extends EnumStateMachineConfigurerAdapter<OrderState, OrderTransitionEvent> {

    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderTransitionEvent> states) throws Exception {
        states
                .withStates()
                .initial(OrderState.NEW)
                .state(OrderState.PAID)
                .state(OrderState.SHIPPED);
        configureAdditionalStates(states);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderTransitionEvent> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(OrderState.NEW)
                .target(OrderState.PAID)
                .event(OrderTransitionEvent.PAY)
                .and()
                .withExternal()
                .source(OrderState.PAID)
                .target(OrderState.SHIPPED)
                .event(OrderTransitionEvent.SHIP);
        configureAdditionalTransitions(transitions);
    }

    protected void configureAdditionalStates(StateMachineStateConfigurer<OrderState, OrderTransitionEvent> states)
            throws Exception {
    }

    protected void configureAdditionalTransitions(
            StateMachineTransitionConfigurer<OrderState, OrderTransitionEvent> transitions) throws Exception {
    }
}
