package com.seer.teach.teacher.stateMachine;

import com.seer.teach.common.enums.teacher.LessonStateEnum;
import com.seer.teach.common.enums.teacher.LessonStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LessonStateMachineListener extends StateMachineListenerAdapter<LessonStateEnum, LessonStateEvent> {

    /**
     * 状态转换
     */
    @Override
    public void transition(Transition<LessonStateEnum, LessonStateEvent> transition) {
        if (transition.getSource() != null && transition.getTarget() != null) {
            log.info("状态转换: {} --({})--> {}",
                    transition.getSource().getId(),
                    transition.getTrigger() != null ? transition.getTrigger().getEvent() : "NO_EVENT",
                    transition.getTarget().getId());
        }
    }

    /**
     * 异常处理
     */
    @Override
    public void stateMachineError(org.springframework.statemachine.StateMachine<LessonStateEnum, LessonStateEvent> sm,
                                  Exception exception) {
        log.error("状态机异常: userId={}, error={}", sm.getId(), exception.getMessage(), exception);
    }

}
