package com.inspect.gateway.handler;

import com.inspect.base.core.exception.CaptchaException;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.gateway.service.ValidateCodeService;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class ValidateCodeHandler implements HandlerFunction<ServerResponse> {
    private final ValidateCodeService validateCodeService;

    public ValidateCodeHandler(ValidateCodeService validateCodeService) {
        this.validateCodeService = validateCodeService;
    }

    public Mono<ServerResponse> handle(ServerRequest serverRequest) {
        AjaxResult ajax;
        try {
            ajax = this.validateCodeService.createCaptcha();
        } catch (IOException | CaptchaException var4) {
            return Mono.error(var4);
        }

        return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(ajax));
    }
}
