package click.kamil.ultimate.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;

import java.util.EnumSet;

@Configuration
@EnableStateMachine
public class UltimateStateMachineConfig extends StateMachineConfigurerAdapter<String, String> {

    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) throws Exception {
        states
            .withStates()
                .initial("NEW")
                .state("TRIAGE")
                .state("IN_PROGRESS")
                .state("ESCALATED")
                .end("RESOLVED")
                .end("CLOSED");
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception {
        transitions
            .withExternal()
                .source("NEW").target("TRIAGE").event("CREATE")
            .and()
            .withExternal()
                .source("NEW").target("IN_PROGRESS").event("CREATE_REACTIVE")
            .and()
            .withExternal()
                .source("NEW").target("IN_PROGRESS").event("CREATE_JAXRS")
            .and()
            .withExternal()
                .source("TRIAGE").target("IN_PROGRESS").event("ASSIGN")
            .and()
            .withExternal()
                .source("IN_PROGRESS").target("ESCALATED").event("ESCALATE")
            .and()
            .withExternal()
                .source("IN_PROGRESS").target("RESOLVED").event("RESOLVE")
            .and()
            .withExternal()
                .source("ESCALATED").target("RESOLVED").event("RESOLVE")
            .and()
            .withExternal()
                .source("RESOLVED").target("CLOSED").event("CLOSE_TICKET")
            .and()
            .withExternal()
                .source("IN_PROGRESS").target("TRIAGE").event("SNS_UPDATE")
            .and()
            .withExternal()
                .source("TRIAGE").target("ESCALATED").event("SQS_UPDATE");
    }
}
