package net.davidvoid.thor.lightning.data.access;

import static org.springframework.util.Assert.isTrue;
import static org.springframework.util.Assert.notNull;

import java.util.Date;
import java.util.List;

import net.davidvoid.thor.lightning.entity.Entity;
import net.davidvoid.thor.lightning.entity.Feed;
import net.davidvoid.thor.lightning.entity.Item;

import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;


/**
 * Created by david on 3/21/16.
 * collection name: item
 * { _id: XX, feed_id: long, id: long, name: string, author: string, content: string, 
 * url: string, is_saved: boolean, is_read: boolean, last_update; Date}
 * index 1: id
 * index 2: feed_id, name
 * index 3: feed_id, last_update
 * index 4: feed_id, is_read, last_update
 * index 4: feed_id, is_read, name 
 */
@Component
public class ItemStore extends AbstractStore {

    private static final String COLLECTION_NAME = "item";
    
    @SuppressWarnings("unchecked")
    public List<Item> getItemsFromFeed(Feed feed) {
        notNull(feed);
        isTrue(feed.has_valid_id());
        
        BasicDBObject query = new BasicDBObject("id", feed.getId());
        return (List<Item>) (List<?>) get(query);
    }

    @Override
    protected DBObject toDBObject(Entity entity) {
        assert entity != null;
        Item item = (Item) entity;
        assert item.has_valid_id();
        
        BasicDBObject object = new BasicDBObject("id", item.getId());
        object.put("name", item.getName());
        object.put("author", item.getAuthor());
        object.put("content", item.getContent());
        object.put("url", item.getUrl());
        object.put("is_saved", item.isSaved());
        object.put("is_read", item.isRead());
        object.put("last_update", item.getLastUpdate());

        return object;
    }

    @Override
    protected Entity toEntity(DBObject object) {
        assert object != null;
        
        Item item = new Item();
        item.setId((Long)object.get("id"));
        item.setName((String)object.get("name"));
        item.setAuthor((String)object.get("author"));
        item.setContent((String)object.get("content"));
        item.setUrl((String)object.get("url"));
        item.setIsSaved((boolean)object.get("is_saved"));
        item.setIsRead((boolean)object.get("is_read"));
        item.setLastUpdate((Date)object.get("last_update"));
        
        return item;
    }

    @Override
    protected DBObject getModifyQuery(Entity entity) {
        assert entity != null;
        
        Item item = (Item) entity;
        assert item.has_valid_id();
        return new BasicDBObject("id", item.getId());
    }

    @Override
    protected String getCollectionName() {
        return COLLECTION_NAME;
    }
}

@@ -803,33 +803,33 @@
 update%0A * index 
-4
+5
 : feed_id, is_re
@@ -836,17 +836,16 @@
 ad, name
- 
 %0A */%0A@Co
@@ -900,16 +900,184 @@
 tore %7B%0A%0A
+    public enum FILTER_OPTION %7B ONLY_READ, ONLY_UNREAD, BOTH %7D%0A    public enum SORT_OPTION %7B BY_UPDATE_DATE_ASC, BY_UPDATE_DATE_DSC,%0A        BY_NAME_ASC, BY_NAME_DSC%7D%0A%0A
     priv
@@ -1368,48 +1368,1514 @@
     
-return (List%3CItem%3E) (List%3C?%3E) get(query)
+BasicDBObject order = new BasicDBObject(%22last_update%22, 1);%0A        return (List%3CItem%3E) (List%3C?%3E) get(query, order);%0A    %7D%0A%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    public List%3CItem%3E getItemsFromFeed(Feed feed, FILTER_OPTION filter_option, SORT_OPTION sort_option) %7B%0A        notNull(feed);%0A        isTrue(feed.has_valid_id());%0A        BasicDBObject query = getQuery(feed, filter_option);%0A        BasicDBObject order = getOrder(sort_option);%0A%0A        return (List%3CItem%3E) (List%3C?%3E) get(query, order);%0A    %7D%0A%0A    private BasicDBObject getOrder(SORT_OPTION sort_option) %7B%0A        switch (sort_option)%7B%0A            case BY_NAME_ASC:%0A                return new BasicDBObject(%22name%22, 1);%0A            case BY_NAME_DSC:%0A                return new BasicDBObject(%22name%22, -1);%0A            case BY_UPDATE_DATE_ASC:%0A                return new BasicDBObject(%22last_update%22, 1);%0A            case BY_UPDATE_DATE_DSC:%0A                return new BasicDBObject(%22last_update%22, -1);%0A            default:%0A                assert false;%0A                return null;%0A        %7D%0A    %7D%0A%0A    private BasicDBObject getQuery(Feed feed, FILTER_OPTION filter_option) %7B%0A        assert feed != null;%0A        assert feed.has_valid_id();%0A%0A        BasicDBObject object = new BasicDBObject(%22id%22, feed.getId());%0A        if (filter_option == FILTER_OPTION.ONLY_READ) %7B%0A            object.put(%22is_read%22, true);%0A        %7D%0A        if (filter_option == FILTER_OPTION.ONLY_UNREAD) %7B%0A            object.put(%22is_read%22, false);%0A        %7D%0A        return object
 ;%0A  

add interface for sorting