package uk.gov.register.resources;

import io.dropwizard.views.View;
import uk.gov.register.configuration.RegisterNameConfiguration;
import uk.gov.register.core.Entry;
import uk.gov.register.core.Item;
import uk.gov.register.db.EntryQueryDAO;
import uk.gov.register.db.ItemQueryDAO;
import uk.gov.register.db.RecordQueryDAO;
import uk.gov.register.presentation.ArchiveCreator;
import uk.gov.register.core.RegisterDetail;
import uk.gov.register.api.representations.ExtraMediaType;
import uk.gov.register.views.ViewFactory;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collection;

@Path("/")
public class DataResource {
    protected final ViewFactory viewFactory;
    private final ItemQueryDAO itemDAO;
    private RecordQueryDAO recordDAO;
    private final EntryQueryDAO entryDAO;
    private String registerPrimaryKey;

    @Inject
    public DataResource(ViewFactory viewFactory, RecordQueryDAO recordDAO, EntryQueryDAO entryDAO, ItemQueryDAO itemDAO, RegisterNameConfiguration registerNameConfiguration) {
        this.viewFactory = viewFactory;
        this.recordDAO = recordDAO;
        this.entryDAO = entryDAO;
        this.itemDAO = itemDAO;
        this.registerPrimaryKey = registerNameConfiguration.getRegister();
    }

    @GET
    @Path("/download-register")
    @Produces({MediaType.APPLICATION_OCTET_STREAM, ExtraMediaType.TEXT_HTML})
    @DownloadNotAvailable
    public Response downloadRegister() {
        Collection<Entry> entries = entryDAO.getAllEntriesNoPagination();
        Collection<Item> items = itemDAO.getAllItemsNoPagination();

        int totalEntries = entryDAO.getTotalEntries();
        int totalRecords = recordDAO.getTotalRecords();

        RegisterDetail registerDetail = viewFactory.registerDetailView(
                totalRecords,
                totalEntries,
                totalEntries,
                entryDAO.getLastUpdatedTime()
        ).getRegisterDetail();

        return Response
                .ok(new ArchiveCreator().create(registerDetail, entries, items))
                .header("Content-Disposition", String.format("attachment; filename=%s-%d.zip", registerPrimaryKey, System.currentTimeMillis()))
                .header("Content-Transfer-Encoding", "binary")
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM)
                .build();
    }

    @GET
    @Path("/download")
    @Produces(ExtraMediaType.TEXT_HTML)
    @DownloadNotAvailable
    public View download() {
        return viewFactory.thymeleafView("download.html");
    }

    @GET
    @Path("/download.torrent")
    @Produces(ExtraMediaType.TEXT_HTML)
    @DownloadNotAvailable
    public Response downloadTorrent() {
        return Response
                .status(Response.Status.NOT_IMPLEMENTED)
                .entity(viewFactory.thymeleafView("not-implemented.html"))
                .build();
    }
}


@@ -40,32 +40,253 @@
 ort 
-io.dropwizard.views.View
+com.fasterxml.jackson.databind.JsonNode;%0Aimport com.google.common.base.Throwables;%0Aimport io.dropwizard.views.View;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0Aimport uk.gov.mint.ItemValidator;%0Aimport uk.gov.mint.ObjectReconstructor
 ;%0Aim
@@ -449,32 +449,70 @@
 .EntryQueryDAO;%0A
+import uk.gov.register.db.EntryStore;%0A
 import uk.gov.re
@@ -792,45 +792,111 @@
 vax.
-inject.Inject;%0Aimport javax.ws.rs.GET
+annotation.security.PermitAll;%0Aimport javax.inject.Inject;%0Aimport javax.servlet.http.HttpServletRequest
 ;%0Aim
@@ -912,20 +912,17 @@
 x.ws.rs.
-Path
+*
 ;%0Aimport
@@ -934,24 +934,28 @@
 x.ws.rs.
-Produces
+core.Context
 ;%0Aimport
@@ -1123,16 +1123,93 @@
 ource %7B%0A
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());%0A%0A
     prot
@@ -1297,16 +1297,22 @@
 private 
+final 
 RecordQu
@@ -1341,22 +1341,16 @@
 private 
-final 
 EntryQue
@@ -1403,16 +1403,145 @@
 maryKey;
+%0A    private ObjectReconstructor objectReconstructor;%0A    private ItemValidator itemValidator;%0A    private EntryStore entryStore;
 %0A%0A    @I
@@ -1718,16 +1718,109 @@
 guration
+, ObjectReconstructor objectReconstructor, ItemValidator itemValidator, EntryStore entryStore
 ) %7B%0A    
@@ -1974,69 +1974,796 @@
 his.
-registerPrimaryKey = registerNameConfiguration.getRegister();
+objectReconstructor = objectReconstructor;%0A        this.itemValidator = itemValidator;%0A        this.entryStore = entryStore;%0A        this.registerPrimaryKey = registerNameConfiguration.getRegister();%0A    %7D%0A%0A    @Context%0A    HttpServletRequest httpServletRequest;%0A%0A    @POST%0A    @PermitAll%0A    @Path(%22/load%22)%0A    @Consumes(MediaType.APPLICATION_JSON)%0A    public void load(String payload) %7B%0A        try %7B%0A            Iterable%3CJsonNode%3E objects = objectReconstructor.reconstruct(payload.split(%22%5Cn%22));%0A            objects.forEach(singleObject -%3E itemValidator.validateItem(registerPrimaryKey, singleObject));%0A            entryStore.load(registerPrimaryKey, objects);%0A        %7D catch (Throwable t) %7B%0A            logger.error(Throwables.getStackTraceAsString(t));%0A            throw t;%0A        %7D
 %0A   

Move '/load' resource from Mint to Register.resources package