/*
 * Copyright 2015 Angus.ai (http://www.angus.ai).
 *
 * 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.
 *
 * Contributors:
 *     Aurélien Moreau
 */

package ai.angus.sdk;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;

import ai.angus.sdk.impl.ConfigurationImpl;
import ai.angus.sdk.impl.File;

public class SessionTest {

    private static Root root;
    private static Service service;
    private static CheckResult callback1, callback3, callback43;
    private static Resource image;
    private static String IMG_1 = "data/Angus-6.jpg";

    @BeforeClass
    public static void setUp() {
        try {
            Configuration conf = new ConfigurationImpl();
            root = conf.connect();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        assertNotNull(root);
        try {
            service = root.getServices().getService("face_detection", 1);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }
        callback1 = new CheckResult(1);
        callback3 = new CheckResult(3);
        callback43 = new CheckResult(43);

        image = root.getBlobs().create(new File(IMG_1));
    }

    private static class CheckResult implements ResultCallBack {
        private int howmany;

        public CheckResult(int howmany) {
            this.howmany = howmany;
        }

        @Override
        public void callback(Job result) {
            SessionTest.checkResult(result, howmany);
        }
    }

    private static void checkResult(Job result, int howmany) {
        assertEquals(result.getStatus(), Resource.CREATED);
        assertEquals(result.getRepresentation(), result.getResult());
        assertTrue(result.getRepresentation().containsKey("faces"));

        double min = Math.ceil(0.5 * howmany);
        double max = Math.floor(1.5 * howmany);

        int size = ((JSONArray) result.getRepresentation().get("faces")).size();
        assertTrue(size >= min);
        assertTrue(size <= max);

    }

    private static void checkResultEventually(Job result, int howmany) {
        if (result.getStatus() == Resource.ACCEPTED) {
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                result.fetch();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
        }
        checkResult(result, howmany);
    }

    @Test
    public void connect() {
        Root root;
        try {
            Configuration conf = new ConfigurationImpl();
            root = conf.connect();
            Service service = root.getServices()
                    .getService("face_detection", 1);
            assertNotNull(service);
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        }

    }

    @SuppressWarnings("unchecked")
    @Test
    public void embededSync() throws Exception {
        service.enableSession();
        FakeCamera camera = new FakeCamera("data/video1");
        while (camera.hasNext()) {
            JSONObject parameters = new JSONObject();
            parameters.put("image", new File(camera.next()));
            Job job = service.process(parameters, false, callback1);
            checkResultEventually(job, 1);
        }
        service.disableSession();

    }


}

@@ -1238,43 +1238,8 @@
 43;%0A
-    private static Resource image;%0A
     
@@ -1765,363 +1765,106 @@
     
-    callback1 = new CheckResult(1);%0A        callback3 = new CheckResult(3);%0A        callback43 = new CheckResult(43);%0A%0A        image = root.getBlobs().create(new File(IMG_1));%0A    %7D%0A%0A    private static class CheckResult implements ResultCallBack %7B%0A        private int howmany;%0A%0A        public CheckResult(int howmany) %7B%0A            this.howmany = howmany;
+%7D%0A%0A    private static class CheckResult implements ResultCallBack %7B%0A        public CheckResult() %7B
 %0A   
@@ -1970,33 +1970,24 @@
 esult(result
-, howmany
 );%0A        %7D
@@ -2036,37 +2036,24 @@
 t(Job result
-, int howmany
 ) %7B%0A        
@@ -2248,103 +2248,8 @@
 );%0A%0A
-        double min = Math.ceil(0.5 * howmany);%0A        double max = Math.floor(1.5 * howmany);%0A
 %0A   
@@ -2357,11 +2357,9 @@
  %3E= 
-min
+0
 );%0A 
@@ -2388,15 +2388,12 @@
  %3C= 
-max
+1
 );%0A
-%0A
     
@@ -2455,21 +2455,8 @@
 sult
-, int howmany
 ) %7B%0A
@@ -2870,17 +2870,8 @@
 sult
-, howmany
 );%0A 
@@ -3727,19 +3727,16 @@
 ally(job
-, 1
 );%0A     
@@ -3778,15 +3778,12 @@
 ();%0A
-%0A
     %7D%0A
-%0A%0A
 %7D%0A

Remove useless argument howmany in session test suite.