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

import click.kamil.examples.statemachine.layered.document.DocumentState;
import click.kamil.examples.statemachine.layered.document.DocumentTransitionEvent;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

public abstract class AbstractDocumentStateMachineConfiguration
        extends EnumStateMachineConfigurerAdapter<DocumentState, DocumentTransitionEvent> {

    @Override
    public void configure(StateMachineStateConfigurer<DocumentState, DocumentTransitionEvent> states)
            throws Exception {
        states
                .withStates()
                .initial(DocumentState.DRAFT)
                .state(DocumentState.SUBMITTED)
                .state(DocumentState.APPROVED);
        configureAdditionalStates(states);
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<DocumentState, DocumentTransitionEvent> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(DocumentState.DRAFT)
                .target(DocumentState.SUBMITTED)
                .event(DocumentTransitionEvent.SUBMIT)
                .and()
                .withExternal()
                .source(DocumentState.SUBMITTED)
                .target(DocumentState.APPROVED)
                .event(DocumentTransitionEvent.APPROVE);
        configureAdditionalTransitions(transitions);
    }

    protected void configureAdditionalStates(StateMachineStateConfigurer<DocumentState, DocumentTransitionEvent> states)
            throws Exception {
    }

    protected void configureAdditionalTransitions(
            StateMachineTransitionConfigurer<DocumentState, DocumentTransitionEvent> transitions) throws Exception {
    }
}
