/*
 * Copyright 2014 Matthew Collins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.thinkofdeath.thinkcraft.bukkit.web;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import org.apache.commons.io.IOUtils;
import uk.co.thinkofdeath.thinkcraft.bukkit.ThinkMapPlugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

public class ResourcesServer extends EndPoint {

    public static final String HTTP_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
    private final static HashMap<String, String> mimeTypes = new HashMap<String, String>();

    static {
        mimeTypes.put("html", "text/html");
        mimeTypes.put("js", "application/javascript");
        mimeTypes.put("css", "text/css");
    }

    private final ThinkMapPlugin plugin;
    private final SimpleDateFormat format = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);

    public ResourcesServer(ThinkMapPlugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public void handle(ChannelHandlerContext context, URI uri, FullHttpRequest request) throws Exception {
        File file = new File(plugin.getResourceDir(), uri.getPath().substring("/resources/".length()));
        if (!file.exists()) {
            sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
            return;
        }

        String modified = request.headers().get(IF_MODIFIED_SINCE);
        if (modified != null && !modified.isEmpty()) {
            Date modifiedDate = format.parse(modified);

            if (modifiedDate.equals(plugin.getStartUpDate())) {
                sendHttpResponse(context, request, new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED));
                return;
            }
        }

        ByteBuf buffer;
        try (InputStream stream = new FileInputStream(file)) {
            ByteBufOutputStream out = new ByteBufOutputStream(context.alloc().buffer());
            IOUtils.copy(stream, out);
            buffer = out.buffer();
            out.close();
        }

        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buffer);

        response.headers().set(DATE, format.format(new Date()));
        response.headers().set(LAST_MODIFIED, format.format(plugin.getStartUpDate()));

        String path = uri.getPath();
        String ext = path.substring(path.lastIndexOf('.') + 1);
        String type = mimeTypes.containsKey(ext) ? mimeTypes.get(ext) : "text/plain";
        if (type.startsWith("text/")) {
            type += "; charset=UTF-8";
        }
        response.headers().set(CONTENT_TYPE, type);
        response.headers().add("Access-Control-Allow-Origin", "*");
        sendHttpResponse(context, request, response);
    }
}

@@ -1168,16 +1168,43 @@
 Format;%0A
+import java.util.Calendar;%0A
 import j
@@ -3379,24 +3379,397 @@
 pDate()));%0A%0A
+        if (uri.getPath().startsWith(%22/resources/assets%22)) %7B%0A            Calendar calendar = Calendar.getInstance();%0A            calendar.setTime(new Date());%0A            calendar.add(Calendar.MONTH, 1);%0A            response.headers().set(EXPIRES, format.format(calendar.getTime()));%0A            response.headers().set(CACHE_CONTROL, %22public, max-age=2592000%22);%0A        %7D%0A%0A
         Stri

Allow textures to be cached for a month