package click.kamil.examples.statemachine.polymorphic;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PolymorphicController {

    private final OrderService orderService;

    public PolymorphicController(OrderService orderService) {
        this.orderService = orderService;
    }

    @PostMapping("/pay")
    public void pay() {
        orderService.processEvent(new PayEvent());
    }

    @PostMapping("/fulfill")
    public void fulfill() {
        orderService.processEvent(new FulfillEvent());
    }

    @PostMapping("/cancel")
    public void cancel() {
        orderService.processEvent(new CancelEvent());
    }

    @PostMapping("/payload-pay")
    public void payloadPay() {
        orderService.processPayloadEvent(new PayEvent());
    }

    @PostMapping("/pay-variable")
    public void payVariable() {
        PayEvent event = new PayEvent();
        orderService.processEvent(event);
    }

    @PostMapping("/pay-cast")
    public void payCast() {
        orderService.processEvent((BaseEvent) new PayEvent());
    }

    @PostMapping("/pay-ternary")
    public void payTernary(boolean isPay) {
        orderService.processEvent(isPay ? new PayEvent() : new CancelEvent());
    }

    @PostMapping("/pay-list")
    public void payList() {
        java.util.List<BaseEvent> events = java.util.Arrays.asList(new PayEvent(), new FulfillEvent());
        for (BaseEvent event : events) {
            orderService.processEvent(event);
        }
    }

    @PostMapping("/pay-builder-static")
    public void payBuilderStatic() {
        orderService.processEvent(EventBuilder.buildEvent());
    }

    @PostMapping("/pay-builder-instance")
    public void payBuilderInstance() {
        EventBuilder builder = new EventBuilder();
        orderService.processEvent(builder.buildInstanceEvent());
    }

    @PostMapping("/abcd")
    public void abcd() {
        orderService.processCustomEvent(new AbcdEvent());
    }

    @PostMapping("/mystery")
    public void mystery() {
        orderService.processCustomEvent(new MysteryPayload());
    }
}
