package de.kopis.timeclicker;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import de.kopis.timeclicker.api.TimeclickerAPI;
import de.kopis.timeclicker.exceptions.NotAuthenticatedException;
import de.kopis.timeclicker.model.TimeEntry;
import de.kopis.timeclicker.model.TimeSum;
import org.apache.wicket.request.resource.AbstractResource;
import org.joda.time.DateTime;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;

public class ListEntriesChartProducerResource extends AbstractResource {
    private static final Logger LOGGER = Logger.getLogger(ListEntriesChartProducerResource.class.getName());

    private static final TimeclickerAPI api = new TimeclickerAPI();

    @Override
    protected ResourceResponse newResourceResponse(Attributes attributes) {
        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType("application/json");
        resourceResponse.setTextEncoding("utf-8");

        resourceResponse.setWriteCallback(new WriteCallback() {
            @Override
            public void writeData(Attributes attributes) throws IOException {
                final UserService userService = UserServiceFactory.getUserService();
                final User currentUser = userService.getCurrentUser();

                final OutputStream outputStream = attributes.getResponse().getOutputStream();
                final Writer writer = new OutputStreamWriter(outputStream);

                writer.write("{\"cols\":[" +
                        "{\"label\":\"Start\",\"type\":\"string\"}," +
                        "{\"label\":\"Tracked\",\"type\":\"number\"}" +
                        "],\"rows\":[");

                try {
                    final List<TimeEntry> entries = api.list(currentUser);
                    // sort ascending
                    Collections.sort(entries, new Comparator<TimeEntry>() {
                        @Override
                        public int compare(TimeEntry o1, TimeEntry o2) {
                            // sort ASC by start date
                            return o1.getStart().compareTo(o2.getStart());
                        }
                    });

                    for (int i = 0; i < entries.size(); i++) {
                        final TimeEntry entry = entries.get(i);
                        if (entry.getStart() != null && entry.getStop() != null) {
                            // need to convert to ISO8601 for javascript
                            writer.write("{\"c\":[" +
                                    "{\"v\":\"" + new DateTime(entry.getStart()) + "\"}," +
                                    "{\"v\":" + new TimeSum(entry).getDuration() + "}" +
                                    "]}");
                        } else {
                            LOGGER.fine("Skipping entry " + entry);
                        }
                        // only write , if NOT last entry
                        if (i < entries.size() - 1) {
                            writer.write(",");
                        }
                    }
                } catch (NotAuthenticatedException e) {
                    LOGGER.severe("Can not load entries: " + e.getMessage());
                }

                writer.write("]}");
                writer.flush();
            }
        });

        return resourceResponse;
    }
}

@@ -2576,95 +2576,8 @@
 i);%0A
-                        if (entry.getStart() != null && entry.getStop() != null) %7B%0A    
     
@@ -2657,36 +2657,32 @@
                 
-    
 writer.write(%22%7B%5C
@@ -2683,36 +2683,32 @@
 te(%22%7B%5C%22c%5C%22:%5B%22 +%0A
-    
                 
@@ -2803,36 +2803,32 @@
                 
-    
 %22%7B%5C%22v%5C%22:%22 + new 
@@ -2900,146 +2900,15 @@
     
-   
- 
 %22%5D%7D%22);%0A
-                        %7D else %7B%0A                            LOGGER.fine(%22Skipping entry %22 + entry);%0A                        %7D%0A
     
@@ -2941,17 +2941,19 @@
 y write 
-,
+%22,%22
  if NOT 

Remove unneccessary check if start and stop are set.