package com.hibegin.http.server.web;

import com.hibegin.common.util.LoggerUtil;
import com.hibegin.http.server.api.HttpRequest;
import com.hibegin.http.server.api.HttpResponse;
import com.hibegin.http.server.api.Interceptor;
import com.hibegin.http.server.config.StaticResourceLoader;
import com.hibegin.http.server.util.MimeTypeUtil;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MethodInterceptor implements Interceptor {

    private static final Logger LOGGER = LoggerUtil.getLogger(MethodInterceptor.class);

    private boolean handleByStaticResource(HttpRequest request, HttpResponse response) {
        for (Map.Entry<String, Map.Entry<String, StaticResourceLoader>> entry :
                request.getServerConfig().getStaticResourceMapper().entrySet()) {
            if (request.getUri().startsWith(entry.getKey())) {
                String path = request.getUri().substring(entry.getKey().length());
                if (request.getUri().endsWith("/")) {
                    path += request.getServerConfig().getWelcomeFile();
                }
                InputStream inputStream = entry.getValue().getValue().getInputStream(entry.getValue().getKey() + path);
                if (inputStream != null) {
                    if (path.contains(".")) {
                        response.addHeader("Content-Type",
                                MimeTypeUtil.getMimeStrByExt(path.substring(path.lastIndexOf(".") + 1)));
                    }
                    response.write(inputStream);
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean doInterceptor(HttpRequest request, HttpResponse response) throws Exception {
        Router router = request.getRequestConfig().getRouter();
        Method method = router.getMethod(request.getUri());
        if (method == null) {
            return !handleByStaticResource(request, response);
        }
        Controller controller = null;
        Constructor[] constructors = method.getDeclaringClass().getConstructors();
        boolean haveDefaultConstructor = false;
        for (Constructor constructor : constructors) {
            if (constructor.getParameterTypes().length == 2) {
                if (constructor.getParameterTypes()[0].getName().equals(HttpRequest.class.getName()) && constructor.getParameterTypes()[1].getName().equals(HttpResponse.class.getName())) {
                    controller = (Controller) constructor.newInstance(request, response);
                }
            }
            if (constructor.getParameterTypes().length == 0) {
                haveDefaultConstructor = true;
            }
        }
        if (controller == null) {
            if (haveDefaultConstructor) {
                controller = (Controller) method.getDeclaringClass().getDeclaredConstructor().newInstance();
                controller.request = request;
                controller.response = response;
            } else {
                LOGGER.log(Level.WARNING, method.getDeclaringClass().getSimpleName() + " not find default " +
                        "constructor");
                return false;
            }
        }
        method.invoke(controller);
        return true;
    }
}

@@ -675,23 +675,20 @@
 private 
-boolean
+void
  handleB
@@ -1668,29 +1668,24 @@
       return
- true
 ;%0A          
@@ -1722,34 +1722,46 @@
 %7D%0A        re
-turn false
+sponse.renderCode(404)
 ;%0A    %7D%0A%0A   
@@ -2037,16 +2037,8 @@
     
-return !
 hand
@@ -2068,32 +2068,58 @@
 est, response);%0A
+            return false;%0A
         %7D%0A      

Fix response error