/*-
 * ========================LICENSE_START========================
 * Morphix MongoDB
 * %%
 * Copyright (C) 2017 - 2018 Harry Fox
 * %%
 * This file is part of Morphix, licensed under the MIT License (MIT).
 *
 * Copyright 2018 Harry Fox <https://hfox.uk/>
 * Copyright 2018 contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * ========================LICENSE_END========================
 */
package uk.hfox.morphix.mongo.query;

import com.mongodb.client.MongoCollection;
import org.bson.Document;
import uk.hfox.morphix.mapper.lifecycle.LifecycleAction;
import uk.hfox.morphix.mongo.connection.MorphixMongoConnector;
import uk.hfox.morphix.mongo.mapper.MongoEntity;
import uk.hfox.morphix.mongo.query.raw.input.MongoDeleteQuery;
import uk.hfox.morphix.mongo.query.raw.output.MongoFindQuery;
import uk.hfox.morphix.mongo.query.sort.MongoQuerySortElement;
import uk.hfox.morphix.query.QueryBuilder;
import uk.hfox.morphix.query.result.QueryResult;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MongoQueryBuilder<R> implements QueryBuilder<R> {

    private final Class<R> clazz;

    private final MongoCollection<Document> collection;
    private final MorphixMongoConnector connector;

    private final Map<String, MongoFieldQueryBuilder<R>> fields;

    MongoQuerySortBuilder<R> sort;

    public MongoQueryBuilder(Class<R> clazz, MongoCollection<Document> collection, MorphixMongoConnector connector) {
        this.clazz = clazz;
        this.collection = collection;
        this.connector = connector;

        this.fields = new HashMap<>();
    }

    @Override
    public MorphixMongoConnector getConnector() {
        return connector;
    }

    @Override
    public Class<R> getQueryType() {
        return clazz;
    }

    @Override
    public MongoFieldQueryBuilder<R> where(String field) {
        MongoFieldQueryBuilder<R> query = new MongoFieldQueryBuilder<>(this);
        this.fields.put(field, query);

        return query;
    }

    @Override
    public MongoQuerySortBuilder<R> sort() {
        if (this.sort == null) {
            return new MongoQuerySortBuilder<>(this);
        }

        return this.sort;
    }

    @Override
    public void delete() {
        delete(false);
    }

    @Override
    public void delete(boolean justOne) {
        MongoDeleteQuery query = new MongoDeleteQuery(collection, getFilter(), !justOne, null);
        query.performQuery();
    }

    @Override
    public QueryResult<R> result() {
        MongoFindQuery query = find();

        List<R> results = new ArrayList<>();
        for (Document document : query.getResults()) {
            R result = this.connector.getTransformer().fromGenericDB(document, null, clazz);
            results.add(result);
            this.connector.getEntityManager().getCache().put(document.getObjectId("_id"), result);

            MongoEntity entity = this.connector.getTransformer().getOrMapEntity(result.getClass());
            entity.set(LifecycleAction.ACCESSED_AT, result, LocalDateTime.now());
            entity.call(LifecycleAction.AFTER_ACCESS, result);
        }

        return new MongoQueryResult<>(results);
    }

    private Document getFilter() {
        Document document = new Document();
        for (Map.Entry<String, MongoFieldQueryBuilder<R>> entry : this.fields.entrySet()) {
            MongoFieldQueryBuilder<R> field = entry.getValue();
            document.put(entry.getKey(), field.getBson());
        }

        return document;
    }

    @Override
    public MongoFindQuery find() {
        MongoFindQuery query = new MongoFindQuery(collection, getFilter());
        query.performQuery();

        if (this.sort != null) {
            Document bson = new Document();
            for (MongoQuerySortElement element : this.sort.getElements()) {
                element.append(bson);
            }

            query.getOutput().sort(bson);
        }

        return query;
    }

}

@@ -3650,55 +3650,143 @@
     
-for (Document document : query.getResults()) %7B%0A
+Iterable%3CDocument%3E iterable = query.getResults();%0A        if (iterable != null) %7B%0A            for (Document document : iterable) %7B%0A    
     
@@ -3886,16 +3886,20 @@
         
+    
 results.
@@ -3915,32 +3915,36 @@
 t);%0A            
+   
+ 
 this.connector.g
@@ -4011,24 +4011,28 @@
 , result);%0A%0A
+    
             
@@ -4115,24 +4115,28 @@
 etClass());%0A
+    
             
@@ -4213,24 +4213,28 @@
             
+    
 entity.call(
@@ -4264,32 +4264,46 @@
 CCESS, result);%0A
+            %7D%0A
         %7D%0A%0A     

Fix NPE on non-existent collection, fixes #20