package click.kamil.enterprise.machines.order;

import click.kamil.enterprise.core.AbstractStateMachineConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachineFactory;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

@Configuration
@EnableStateMachineFactory(name = "orderStateMachineFactory")
public class OrderStateMachineConfiguration extends AbstractStateMachineConfiguration<OrderState, OrderEvent> {

    @Override
    protected void configureStates(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
        states
            .withStates()
            .initial(OrderState.NEW)
            .state(OrderState.PENDING)
            .end(OrderState.SHIPPED);
    }

    @Override
    protected void configureTransitions(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {
        commonProtectedMethod();
        transitions
            .withExternal()
                .source(OrderState.NEW).target(OrderState.PENDING).event(OrderEvent.PAY)
                .action(context -> System.out.println("Payment processed."))
                .and()
            .withExternal()
                .source(OrderState.PENDING).target(OrderState.SHIPPED).event(OrderEvent.SHIP);
    }
}
