package me.chyxion.tigon.webmvc.util;

import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletResponse;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Jan 16, 2014 11:22:50 AM
 */
@Slf4j
public class ResponseTool {

    public static final String PREFIX_DATA = "data:";
    public static final String PREFIX_REDIRECT = "redirect:";
    public static final String PREFIX_FORWARD = "forward:";
    public static final String PREFIX_JSP = "jsp:";
    public static final String PREFIX_FTL = "ftl:";

    public String data(CharSequence data) {
        log.debug("Build Data Response [{}:{}].", PREFIX_DATA, data);
        return PREFIX_DATA + data;
    }

    /**
     * Return JSP View
     * @param jsp
     * @return
     */
    public String jsp(CharSequence jsp) {
        log.debug("Build JSP Response [{}:{}].", PREFIX_JSP, jsp);
        return PREFIX_JSP + jsp;
    }

    /**
     * Return JSP Model And View
     * @param mv
     * @return
     */
    public ModelAndView jsp(ModelAndView mv) {
        mv.setViewName(PREFIX_JSP + mv.getViewName());
        return mv;
    }

    /**
     * Return FreeMarker Model And View
     * @param mv
     * @return
     */
    public ModelAndView ftl(ModelAndView mv) {
        mv.setViewName(PREFIX_FTL + mv.getViewName());
        return mv;
    }

    /**
     * Return FreeMarker View
     * @param ftl
     * @return
     */
    public String ftl(CharSequence ftl) {
        log.debug("Build FreeMarker Response [{}:{}].", PREFIX_FTL, ftl);
        return PREFIX_FTL + ftl;
    }

    /**
     * Redirect To
     * @param redirect
     * @return
     */
    public String redirect(String redirect) {
        log.debug("Build Redirect Response [{}:{}].", PREFIX_REDIRECT, redirect);
        return PREFIX_REDIRECT + redirect;
    }

    /**
     * Forward To
     * @param forward
     * @return
     */
    public String forward(String forward) {
        log.debug("Build Forward Response [{}:{}].", PREFIX_FORWARD, forward);
        return PREFIX_FORWARD + forward;
    }

    public void write(ServletResponse response, String data, String contentType, String encoding) {
        response.setContentType(contentType);
        response.setCharacterEncoding(encoding);
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.write(data);
            writer.flush();
        }
        catch (Exception e) {
            throw new IllegalStateException(
                "Write Response Error Caused", e);
        }
        finally {
            closeQuietly(writer);
        }
    }

    public void writeJSON(ServletResponse response, String data) {
        write(response, data, "application/json;charset=utf-8", "utf-8");
    }

    public void writeJSON(ServletResponse response, Object data) {
        writeJSON(response, JSON.toJSONString(data));
    }

    public void writeText(ServletResponse response, String data) {
        write(response, data, "text/plain;charset=utf-8", "utf-8");
    }

    void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            }
            catch (IOException e) {
                log.warn("IO Close Error Caused.", e);
            }
        }
    }
}
