package edu.deanza.calendar.dal;

import android.content.Context;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;

import edu.deanza.calendar.domain.EventRepository;
import edu.deanza.calendar.domain.OrganizationRepository;
import edu.deanza.calendar.domain.models.Event;
import edu.deanza.calendar.util.Callback;

/**
 * Created by karinaantonio on 8/11/16.
 */

public final class FirebaseEventRepository extends FirebaseRepository<Event> implements EventRepository, Serializable {

    private final DataMapper<Event> mapper;

    public FirebaseEventRepository(Context context) {
        super(context);
        mapper = new EventMapper(new FirebaseOrganizationRepository(context, this));
    }

    public FirebaseEventRepository(Context context, OrganizationRepository repository) {
        super(context);
        mapper = new EventMapper(repository);
    }

    @Override
    String getRootName() {
        return "events";
    }

    @Override
    DataMapper<Event> getMapper() {
        return mapper;
    }

    @Override
    public void all(Callback<Event> callback) {
        currentQuery = root.orderByKey();
        listenToQuery(callback);
    }

    @Override
    public void findByOrganization(String organizationName, Callback<Event> callback) {
        currentQuery = root
                .orderByChild(organizationName)
                .equalTo(true);
        listenToQuery(callback);
    }

    @Override
    public void on(LocalDate date, Callback<Event> callback) {
        DateTimeFormatter formatter = ISODateTimeFormat.date();
        currentQuery = root
                .orderByKey()
                .startAt(formatter.print(date))
                .endAt(formatter.print(date.plusDays(1)));
        listenToQuery(callback);
    }

    @Override
    public void before(LocalDate date, Callback<Event> callback) {
        DateTimeFormatter formatter = ISODateTimeFormat.date();
        currentQuery = root
                .orderByKey()
                .endAt(formatter.print(date));
        listenToQuery(callback);
    }

    @Override
    public void after(LocalDate date, Callback<Event> callback) {
        DateTimeFormatter formatter = ISODateTimeFormat.date();
        currentQuery = root
                .orderByKey()
                .startAt(formatter.print(date));
        listenToQuery(callback);
    }

    @Override
    public void between(LocalDate start, LocalDate end, Callback<Event> callback) {
        DateTimeFormatter formatter = ISODateTimeFormat.date();
        currentQuery = root
                .orderByKey()
                .startAt(formatter.print(start))
                .endAt(formatter.print(end));
        listenToQuery(callback);
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        initialize();
    }

}

@@ -1280,20 +1280,29 @@
 .orderBy
-Key(
+Child(%22start%22
 );%0A     
@@ -1771,36 +1771,45 @@
         .orderBy
-Key(
+Child(%22start%22
 )%0A              
@@ -2130,36 +2130,45 @@
         .orderBy
-Key(
+Child(%22start%22
 )%0A              
@@ -2428,36 +2428,45 @@
         .orderBy
-Key(
+Child(%22start%22
 )%0A              
@@ -2758,12 +2758,21 @@
 erBy
-Key(
+Child(%22start%22
 )%0A  

Order events on the same day chronologically correctly