/* 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 ink.wgink.module.activiti.controller.staticfile;

import ink.wgink.util.ResourceUtil;
import ink.wgink.util.request.StaticResourceRequestUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 用于汉化在线编辑器
 *
 * @author Tijs Rademakers
 */
@RestController
@RequestMapping("static/editor-app")
public class ActivitiEditorAppStaticController {

    @GetMapping("{fileName}")
    public void root(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("configuration/{fileName}")
    public void configuration(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/configuration/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("configuration/properties/{fileName}")
    public void diagramViewerImagesDeployer(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/configuration/properties/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("css/{fileName}")
    public void css(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/css/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("editor/{fileName}")
    public void editor(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/editor/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("editor/{folder}/{fileName}")
    public void editor(HttpServletResponse httpServletResponse, @PathVariable("folder") String folder, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/editor/" + folder + "/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("fonts/{fileName}")
    public void fonts(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/fonts/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("i18n/{fileName}")
    public void i18n(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/i18n/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("images/{fileName}")
    public void images(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/images/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("libs/{fileName}")
    public void libs(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/libs/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("libs/{folder}/{fileName}")
    public void libs(HttpServletResponse httpServletResponse, @PathVariable("folder") String folder, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/libs/" + folder + "/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("libs/bootstrap_3.1.1/{folder}/{fileName}")
    public void libsBootstrap(HttpServletResponse httpServletResponse, @PathVariable("folder") String folder, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/libs/bootstrap_3.1.1/" + folder + "/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("libs/es5-shim-15.3.4.5/tests/{fileName}")
    public void libSes5ShimTests(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/libs/es5-shim-15.3.4.5/tests" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("libs/es5-shim-15.3.4.5/tests/{floder}/{fileName}")
    public void libSes5ShimTests(HttpServletResponse httpServletResponse, @PathVariable("folder") String folder, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/libs/es5-shim-15.3.4.5/tests/" + folder + "/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("libs/json3_3.2.6/lib/{fileName}")
    public void libJsonLib(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/libs/json3_3.2.6/lib/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("partials/{fileName}")
    public void partials(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/partials/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("popups/{fileName}")
    public void popups(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/popups/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("stencilsets/bpmn2.0/icons/{fileName}")
    public void stencilsetsBpmnIcons(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/stencilsets/bpmn2.0/icons/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("stencilsets/bpmn2.0/icons/{folder}/{fileName}")
    public void stencilsetsBpmnIcons(HttpServletResponse httpServletResponse, @PathVariable("folder") String folder, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/stencilsets/bpmn2.0/icons/" + folder + "/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

    @GetMapping("stencilsets/bpmn2.0/icons/activity/list/{fileName}")
    public void stencilsetsBpmnIconsActivitiList(HttpServletResponse httpServletResponse, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = ResourceUtil.getJarResourceInputStream("static/editor-app/stencilsets/bpmn2.0/icons/activity/list/" + fileName);
        StaticResourceRequestUtil.download(httpServletResponse, inputStream, fileName);
    }

}
