package dev.blunch.blunch;

import com.firebase.client.DataSnapshot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import java.lang.Boolean;
import java.lang.Exception;
import java.lang.Object;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import dev.blunch.blunch.utils.Entity;
import dev.blunch.blunch.utils.Repository;
import dev.blunch.blunch.utils.Utils;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

/**
 * To work on unit tests, switch the Test Artifact in the Build Variants view.
 */
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class UtilsPackageTest {

    private PersonRepository personRepository = new PersonRepository();

    @Test
    public void generate_differents_keys() throws Exception {
        String key1 = Utils.generateId();
        String key2 = Utils.generateId();
        assertFalse(key1.equals(key2));
    }

    @Test
    public void add_correctly_some_object_in_repository() throws Exception {
        Set<String> sons = new LinkedHashSet<>();
        sons.add("Pere");
        sons.add("Victor");
        Person personToAdd = new Person("Albert", 20, sons);
        personRepository.insert(personToAdd);
        Person personToFind = personRepository.findById(personToAdd.getId());
        assertEquals(personToAdd.getId(), personToFind.getId());
        assertEquals(personToAdd.getAge(), personToFind.getAge());
        Set<String> keys = personToAdd.getSons().keySet();
        for (String s : keys) {
            assertTrue(personToFind.getSons().containsKey(s));
        }
    }

}

class PersonRepository extends Repository<Person> {

    public PersonRepository() {
        super();
    }

    @Override
    public Person convert(DataSnapshot data) {
        Person person = new Person();

        person.setId(data.getKey());
        for (DataSnapshot d : data.getChildren()) {

            if (d.getKey().equals("name")) {
                person.setName(d.getValue().toString());
            }
            else if (d.getKey().equals("age")) {
                person.setAge((Integer) d.getValue());
            }

            else {
                Set<String> keys = new LinkedHashSet<>();
                for (DataSnapshot dd : d.getChildren()) {
                    keys.add(dd.getKey());
                }
                person.setSon(keys);
            }
        }

        return person;
    }

    @Override
    public String getObjectReference() {
        return "people";
    }
}

class Person implements Entity {
    private String id;
    private String name;
    private Integer age;
    private Map<String, Object> sons = new LinkedHashMap<>();

    public Person() {

    }

    public Person(String name, Integer age, Set<String> sons) {
        this.name = name;
        this.age = age;
        for (String s : sons) this.sons.put(s, true);
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public Map<String, Object> getSons() {
        return sons;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setSons(Map<String, Object> sons) {
        this.sons = sons;
    }

    public void setSon(Set<String> sonsKeys) {
        for (String s : sonsKeys) sons.put(s, true);
    }
}
@@ -62,17 +62,16 @@
 apshot;%0A
-%0A
 import o
@@ -212,17 +212,16 @@
 Config;%0A
-%0A
 import j
@@ -233,448 +233,63 @@
 ang.
-Boolean;%0Aimport java.lang.Exception;%0Aimport java.lang.Object;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.LinkedHashSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0Aimport dev.blunch.blunch.utils.Entity;%0Aimport dev.blunch.blunch.utils.Repository;%0Aimport dev.blunch.blunch.utils.Utils;%0A%0Aimport static junit.framework.Assert.assertEquals;%0Aimport static junit.framework.Assert.assertFalse;%0Aimport static junit.framework.Assert.assertTrue
+*;%0Aimport java.util.*;%0Aimport dev.blunch.blunch.utils.*
 ;%0Aim
@@ -317,28 +317,17 @@
 .Assert.
-assertEquals
+*
 ;%0A%0A/**%0A 
@@ -641,17 +641,16 @@
 ifferent
-s
 _keys() 
@@ -1457,24 +1457,24 @@
 insKey(s));%0A
-
         %7D%0A  
@@ -1467,24 +1467,78 @@
 ;%0A        %7D%0A
+        personRepository.delete(personToAdd.getId());%0A
     %7D%0A%0A%7D%0A%0Acl

change some lines