package com.face.util;


import com.face.global.language.LanguageStore;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;

import java.io.IOException;

public class ResponseUtils {
    public static void writeError(String code, String message, HttpServletResponse response) throws IOException {
        String errorMessage;
        if (StringUtils.isNotBlank(message)) {
            errorMessage = message;
        } else {
            errorMessage = MessageUtils.getMessage(code,
                    null, LanguageStore.getLocale());
        }
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JsonUtils.toJsonString(
                ApiResultBuilder.getRawBuilder().baseInfo(code,  errorMessage).build()));
        response.getWriter().flush();
    }

    public static void writeString(String message, HttpServletResponse response) throws IOException {
        response.setContentType(MediaType.TEXT_PLAIN_VALUE);
        response.getWriter().write(message);
        response.getWriter().flush();
    }
}
