package com.couchbase.devxp.updownvote;

import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Document;
import com.couchbase.lite.Emitter;
import com.couchbase.lite.Mapper;
import com.couchbase.lite.Query;
import com.couchbase.lite.QueryEnumerator;
import com.couchbase.lite.util.Log;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by phil on 03/11/14.
 */
public class Presentation {
    public static final String TYPE = "presentation";

    private Date createdAt;
    private int upVotes;
    private int downVotes;
    private String title;
    private Document sourceDocument;
    private Database database;

    public static Query findAll(Database database) {

        com.couchbase.lite.View view = database.getView("presentations");
        if (view.getMap() == null) {
            Mapper mapper = new Mapper() {
                @Override
                public void map(Map<String, Object> doc, Emitter emitter) {
                    String type = (String) doc.get("type");
                    if (type.equals(Presentation.TYPE)) {
                        emitter.emit(doc.get("created_at"), doc);
                    }
                }
            };
            view.setMap(mapper, "2");
        }
        return view.createQuery();
    }

    public static Presentation from(Document document) {
        Presentation presentation = new Presentation(document.getDatabase());
        presentation.setTitle((String) document.getProperty("title"));
        presentation.setCreatedAt(new Date((Long) document.getProperty("created_at")));
        presentation.setDownVotes((Integer) document.getProperty("down_votes"));
        presentation.setUpVotes((Integer) document.getProperty("up_votes"));
        presentation.setSourceDocument(document);
        return presentation;
    }

    public Presentation(Database database) {
        this.createdAt = new Date();
        this.upVotes = 0;
        this.downVotes = 0;
        this.title = "";
        this.database = database;
    }

    public void save() throws CouchbaseLiteException {
        Map<String, Object> properties = new HashMap<String, Object>();
        Document document;
        if (sourceDocument == null) {
            document = database.createDocument();
        } else {
            document = sourceDocument;
            properties.putAll(sourceDocument.getProperties());
        }
        properties.put("type", TYPE);
        properties.put("created_at", createdAt.getTime());
        properties.put("title", title);
        properties.put("up_votes", upVotes);
        properties.put("down_votes", downVotes);
        try {
            document.putProperties(properties);
        } catch(CouchbaseLiteException e) {
            Log.e("PRESENTATION", "Failed to save");
            throw e;
        }
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public int getUpVotes() {
        return upVotes;
    }

    public void setUpVotes(int upVotes) {
        this.upVotes = upVotes;
    }

    public String getTitle() {
        return title;
    }

    public void setSourceDocument(Document document) {
        this.sourceDocument = document;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getDownVotes() {
        return downVotes;
    }

    public void setDownVotes(int downVotes) {
        this.downVotes = downVotes;
    }
}

@@ -1504,24 +1504,81 @@
 atabase());%0A
+        if (document.getProperty(%22title%22) != null) %7B%0A    
         pres
@@ -1648,88 +1648,528 @@
     
-presentation.setCreatedAt(new Date((Long) document.getProperty(%22created_at%22)));%0A
+%7D%0A        if (document.getProperty(%22created_at%22) != null)%7B%0A            long createdAtL = 0;%0A            Object createdAt = document.getProperty(%22created_at%22);%0A            if (createdAt instanceof Double)%7B%0A                createdAtL = ((Double) createdAt).longValue();%0A            %7D%0A            if (createdAt instanceof Long)%7B%0A                createdAtL = (Long) createdAt;%0A            %7D%0A            presentation.setCreatedAt(new Date(createdAtL));%0A        %7D%0A        if (document.getProperty(%22down_votes%22) != null) %7B%0A    
     
@@ -2237,32 +2237,101 @@
 %22down_votes%22));%0A
+        %7D%0A        if (document.getProperty(%22up_votes%22) != null)%7B%0A    
         presenta
@@ -2383,32 +2383,42 @@
 y(%22up_votes%22));%0A
+        %7D%0A
         presenta

make presentation marshalling safer