/*
 *  Copyright 2014~2015 Dan Haywood
 *
 *  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 org.isisaddons.wicket.pdfjs.fixture.scripts.data;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.ByteSource;
import com.google.common.io.Resources;

import org.apache.isis.applib.fixturescripts.FixtureScript;
import org.apache.isis.applib.value.Blob;

import org.isisaddons.module.fakedata.dom.FakeDataService;
import org.isisaddons.wicket.pdfjs.fixture.dom.demo.DemoObject;
import org.isisaddons.wicket.pdfjs.fixture.dom.demo.DemoObjectMenu;

import lombok.Getter;

public class DemoObjectsFixture extends FixtureScript {

    @javax.inject.Inject
    DemoObjectMenu demoObjectMenu;

    @javax.inject.Inject
    FakeDataService fakeDataService;

    @Getter
    private Integer number ;
    public DemoObjectsFixture setNumber(final Integer number) {
        this.number = number;
        return this;
    }

    @Getter
    private List<DemoObject> demoObjects = Lists.newArrayList();

    @Override
    protected void execute(final ExecutionContext ec) {

        defaultParam("number", ec, 3);
        if(getNumber() < 1 || getNumber() > 5) {
            // there are 5 sample PDFs
            throw new IllegalArgumentException("number of demo objects to create must be within [1,5]");
        }

        for (int i = 0; i < getNumber(); i++) {
            final DemoObject demoObject = create(i, ec);
            getDemoObjects().add(demoObject);
        }
    }

    private DemoObject create(final int n, final ExecutionContext ec) {
        final String name = fakeDataService.name().firstName();

        final String documentName = "Sample" + (n + 1) + ".pdf";
        final String urlStr = "http://www.pdfpdf.com/samples/" + documentName + ".PDF";

        Blob blob = asBlob(documentName, urlStr);

        final DemoObject demoObject = wrap(demoObjectMenu).create(name);
        wrap(demoObject).setUrl(urlStr);
        wrap(demoObject).setBlob(blob);

        return ec.addResult(this, demoObject);
    }

    private Blob asBlob(final String documentName, final String urlStr) {
        try {
            final URL url = new URL(urlStr);

            final HttpURLConnection httpConn = openConnection(url);
            final String contentType = httpConn.getContentType();
            final MimeType mimeType = determineMimeType(contentType);

            httpConn.disconnect();

            final ByteSource byteSource = Resources.asByteSource(url);
            final byte[] bytes = byteSource.read();

            return new Blob(documentName, mimeType.getBaseType(), bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private HttpURLConnection openConnection(final URL url) throws IOException {
        final HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        final int responseCode = httpConn.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Response code: " + responseCode);
        }
        return httpConn;
    }

    private MimeType determineMimeType(final String contentType) throws MimeTypeParseException {
        final String mimeType = parseMimeType(contentType);
        if(mimeType == null) {
            return null;
        }
        return new MimeType(mimeType);
    }

    // text/plain; charset=UTF-8
    private String parseMimeType(final String contentType) {
        final Iterable<String> values = Splitter.on(";").split(contentType);
        for (String value : values) {
            // is simply the first part
            return value.trim();
        }

        return null;
    }

}

@@ -2439,19 +2439,19 @@
  1) + %22.
-pdf
+PDF
 %22;%0A     
@@ -2522,25 +2522,16 @@
 mentName
- + %22.PDF%22
 ;%0A%0A     

Fix the generation of the names for the sample pdfs