/*
 * Copyright © 2015 John H Uckele. All rights reserved.
 */

package io.vivarium.client.task;

import java.io.IOException;
import java.nio.channels.NotYetConnectedException;
import java.util.concurrent.ExecutionException;

import org.java_websocket.handshake.ServerHandshake;

import com.fasterxml.jackson.core.JsonProcessingException;

import io.vivarium.client.TaskClient;
import io.vivarium.net.messages.Message;
import io.vivarium.net.messages.RequestResourceMessage;
import io.vivarium.net.messages.ResourceFormat;
import io.vivarium.net.messages.SendResourceMessage;
import io.vivarium.serialization.JSONConverter;
import io.vivarium.serialization.VivariumObjectCollection;
import io.vivarium.util.SimpleFuture;
import io.vivarium.util.UUID;

public class DownloadResourceTask extends Task
{
    private final UUID _uuid;
    private SimpleFuture<VivariumObjectCollection> _object;

    public DownloadResourceTask(UUID uuid)
    {
        _uuid = uuid;
        _object = new SimpleFuture<>();
    }

    @Override
    public void onOpen(TaskClient client, ServerHandshake handshakedata)
    {
        try
        {
            RequestResourceMessage request = new RequestResourceMessage(_uuid, ResourceFormat.JSON);
            client.send(client.getMapper().writeValueAsString(request));
        }
        catch (NotYetConnectedException | JsonProcessingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onMessage(TaskClient client, String message)
    {
        try
        {
            Message untypedMessage = client.getMapper().readValue(message, Message.class);
            if (untypedMessage instanceof SendResourceMessage)
            {
                SendResourceMessage sendResource = (SendResourceMessage) untypedMessage;
                String jsonDataString = sendResource.dataString;
                VivariumObjectCollection collection = JSONConverter.jsonStringToSerializerCollection(jsonDataString);
            }
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onClose(TaskClient client, int code, String reason, boolean remote)
    {
    }

    @Override
    public void onError(TaskClient client, Exception ex)
    {
    }

    public VivariumObjectCollection waitForResource() throws InterruptedException, ExecutionException
    {
        return _object.get();
    }
}

@@ -882,16 +882,17 @@
  _object
+s
 ;%0A%0A    p
@@ -972,16 +972,17 @@
  _object
+s
  = new S
@@ -2023,16 +2023,58 @@
 tring);%0A
+                _objects.put(collection);%0A
         
@@ -2551,16 +2551,17 @@
  _object
+s
 .get();%0A

Fix to future behavior of DownloadResourceTask