package io.github.proxyprint.kitchen.controllers.consumer;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import io.github.proxyprint.kitchen.models.User;
import io.github.proxyprint.kitchen.models.consumer.Consumer;
import io.github.proxyprint.kitchen.models.repositories.ConsumerDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.io.IOException;

/**
 * Created by daniel on 04-04-2016.
 */
@RestController
public class BaseController {

    @Autowired
    private ConsumerDAO consumers;
    private final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    @RequestMapping(value = "/consumer/login", method = RequestMethod.POST)
    public String login(WebRequest request) throws IOException {
        boolean auth = false;
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        JsonObject response = new JsonObject();
        if (username != null && password != null && !username.isEmpty() && !password.isEmpty()) {
            Consumer consumer = consumers.findByUsername(username);
            if (consumer != null) {
                auth = consumer.getPassword().equals(password);
                if (auth) {
                    response.add("consumer", GSON.toJsonTree(consumer));
                }
            }
        }

        response.addProperty("success", auth);
        return GSON.toJson(response);
    }

    @RequestMapping(value = "/consumer/register", method = RequestMethod.POST)
    public String addUser(WebRequest request) {
        boolean success = false;

        JsonObject response = new JsonObject();
        String username = request.getParameter("username");
        Consumer c = consumers.findByUsername(username);

        if (c == null) {
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String name = request.getParameter("name");
            String lat = request.getParameter("latitude");
            String lon = request.getParameter("longitude");
            c = new Consumer(name, username, password, email, lat, lon);
            consumers.save(c);
            response.add("consumer", GSON.toJsonTree(c));
            success = true;
        } else {
            response.addProperty("error", "O username já se encontra em uso.");
            success = false;
        }

        response.addProperty("success", success);
        return GSON.toJson(response);
    }
}

@@ -333,16 +333,107 @@
 merDAO;%0A
+import java.io.BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileOutputStream;%0A
 import o
@@ -762,16 +762,279 @@
 ception;
+%0Aimport org.apache.commons.io.FilenameUtils;%0Aimport org.apache.pdfbox.pdmodel.PDDocument;%0Aimport org.springframework.security.core.Authentication;%0Aimport org.springframework.web.bind.annotation.RequestParam;%0Aimport org.springframework.web.multipart.MultipartFile;
 %0A%0A/**%0A *
@@ -3164,11 +3164,1333 @@
 );%0A    %7D
+%0A%0A    @RequestMapping(value = %22/consumer/upload%22, method = RequestMethod.POST)%0A    public String handleFileUpload(@RequestParam(%22file%22) MultipartFile file, WebRequest req) %7B%0A        JsonObject response = new JsonObject();%0A%0A        String filetype = FilenameUtils.getExtension(file.getOriginalFilename());%0A        if (!filetype.equals(%22pdf%22)) %7B%0A            throw new RuntimeException(%22Error! Not a pdf%22);%0A        %7D%0A        String name = FilenameUtils.removeExtension(file.getOriginalFilename());%0A%0A        if (!file.isEmpty()) %7B%0A            try %7B%0A                PDDocument pdf = PDDocument.load(file.getInputStream());%0A                int count = pdf.getNumberOfPages();%0A%0A                System.out.println(%22Este pdf tem %22 + count + %22 p%C3%A1ginas!!%22);%0A                response.addProperty(%22success%22, true);%0A                response.addProperty(%22message%22, %22Este pdf tem %22 + count + %22 p%C3%A1ginas!!%22);%0A            %7D catch (Exception e) %7B%0A                response.addProperty(%22success%22, false);%0A                response.addProperty(%22message%22, %22You failed to upload %22 + name + %22 =%3E %22 + e.getMessage());%0A            %7D%0A        %7D else %7B%0A            response.addProperty(%22success%22, false);%0A            response.addProperty(%22message%22, %22You failed to upload because the file was empty%22);%0A        %7D%0A        return GSON.toJson(response);%0A    %7D
 %0A%7D%0A

handle file uploads!!