package com.github.onsdigital.data;

import com.github.onsdigital.content.DirectoryListing;
import com.github.onsdigital.content.page.base.Page;
import com.github.onsdigital.content.service.ContentNotFoundException;
import com.github.onsdigital.content.util.ContentUtil;
import com.github.onsdigital.data.zebedee.ZebedeeClient;
import com.github.onsdigital.data.zebedee.ZebedeeDataService;
import com.github.onsdigital.data.zebedee.ZebedeeRequest;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;

/**
 * Route data requests to zebedee if required, else read from the local filesystem.
 */
public class DataService {

    private static DataService instance = new DataService();
    private DataService() { }
    public static DataService getInstance() {
        return instance;
    }

    public InputStream readData(String uri, boolean resolveReferences, ZebedeeRequest zebedeeRequest) throws ContentNotFoundException, IOException {
        if (zebedeeRequest != null) {
            return ZebedeeDataService.getInstance().readData(uri, zebedeeRequest, resolveReferences);
        }

        if (resolveReferences) {
            try {
                Page page = readAsPage(uri, true, zebedeeRequest);
                return IOUtils.toInputStream(page.toJson());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            return LocalFileDataService.getInstance().readData(uri);
        }

        throw new DataNotFoundException(uri);
    }


    public Page readAsPage(String uri, boolean resolveReferences, ZebedeeRequest zebedeeRequest) throws IOException, ContentNotFoundException {
        if (zebedeeRequest != null) {
            return ContentUtil.deserialisePage(ZebedeeDataService.getInstance().readData(uri, zebedeeRequest, resolveReferences));
        } else {
            Page page = ContentUtil.deserialisePage(LocalFileDataService.getInstance().readData(uri));
            if (resolveReferences) {
                page.loadReferences(LocalFileDataService.getInstance());
            }
            return page;
        }
    }

    public DirectoryListing readDirectory(String uri, ZebedeeRequest zebedeeRequest) throws ContentNotFoundException {

        // make request to browse api
        ZebedeeClient zebedeeClient = new ZebedeeClient(zebedeeRequest);
        DirectoryListing directoryListing;
        try {
            directoryListing = ContentUtil.deserialise(zebedeeClient.get("browse", uri, false), DirectoryListing.class);
        } finally {
            zebedeeClient.closeConnection();
        }

        return directoryListing;
    }
}

@@ -268,65 +268,8 @@
 il;%0A
-import com.github.onsdigital.data.zebedee.ZebedeeClient;%0A
 impo
@@ -2219,389 +2219,200 @@
     
-// make request to browse api%0A        ZebedeeClient zebedeeClient = new ZebedeeClient(zebedeeRequest);%0A        DirectoryListing directoryListing;%0A        try %7B%0A            directoryListing = ContentUtil.deserialise(zebedeeClient.get(%22browse%22, uri, false), DirectoryListing.class);%0A        %7D finally %7B%0A            zebedeeClient.closeConnection();%0A        %7D%0A%0A        return d
+if (zebedeeRequest != null) %7B%0A            return ZebedeeDataService.getInstance().readDirectory(uri, zebedeeRequest);%0A        %7D%0A%0A        return LocalFileDataService.getInstance().readD
 irectory
 List
@@ -2407,23 +2407,21 @@
 irectory
-Listing
+(uri)
 ;%0A    %7D%0A

Fix previous releases page. Read directory contents from zebedee if its a zebedee request else read from local file system.