package com.shuimin.pond.core;

import com.shuimin.common.S;
import com.shuimin.pond.core.db.Record;
import com.shuimin.pond.core.kernel.PKernel;
import com.shuimin.pond.core.spi.JsonService;
import com.shuimin.pond.core.spi.ViewEngine;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import static com.shuimin.pond.core.Pond.CUR;

/**
 * Created by ed on 2014/4/18.
 */
public interface Renderable {

    public static Renderable error(int err_code, String msg) {
        String path = "err" + File.separator + err_code;
        return view(path, new HashMap<String, Object>() {{
            this.put("err_msg", msg);
        }});
    }

    public static Renderable text(String text) {
        return (resp) -> {
            resp.write(text);
            resp.send(200);
        };
    }

    public static Renderable json(Object o) {
        JsonService serv = PKernel.getService(JsonService.class);
        return (resp) -> {
            resp.contentType("application/json;charset=utf-8");
            resp.write(serv.toString(o));
            resp.send(200);
        };
    }

    public static Renderable dump(Object o) {
        return resp ->
                resp.write(S.dump(o));
    }

    public static Renderable stream(InputStream in) {
        return (resp) -> {
            try (InputStream _in = in) {
                S.stream.write(_in, resp.out());
            } catch (IOException e) {
                S._throw(e);
            }
            resp.send(200);
        };
    }

    @SuppressWarnings("unchecked")
    public static Renderable view(String path, Object o) {
        File file = new File((String) Pond.attribute(Global.TEMPLATE_PATH)
                + File.separator + path);
        ViewEngine engine = PKernel.getService(ViewEngine.class);
        if (file.exists()) {
            final Object render;
            Map map = new HashMap(CUR().attrs());
            if(o instanceof Map) {
                map.putAll((Map) o);
                render = map;
            }
            else{
                render = o;
            }
            return (resp) -> engine.render(resp.out(), path, render);
        } else {
            PKernel.getLogger().warn("File" + file + "not found");
            return json(o);
        }
    }

    public static Renderable view(String path) {
        return view(path, null);
    }

    public void render(Response resp);


}

@@ -402,16 +402,64 @@
 ond.CUR;
+%0Aimport static com.shuimin.pond.core.Pond.debug;
 %0A%0A/**%0A *
@@ -2016,16 +2016,135 @@
         
+debug(%22Render:%22 + S.dump(o));%0A            if(o == null) %7B%0A                render = map;%0A            %7D%0A            else 
 if(o ins

Fix Criterion req.params -> req.param