package org.puller;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.puller.comixology.Comixology;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class Puller {
    public static void main(String[] args) {
        final PullerOptions options = new PullerOptions();
        final CmdLineParser parser = new CmdLineParser(options);
        try {
            parser.parseArgument(args);

            if (options.getUsername() == null) {
                throw new CmdLineException(parser, new LocalizedString("The username is missing"));
            }

            if (options.getPassword() == null) {
                throw new CmdLineException(parser, new LocalizedString("The password is missing"));
            }

            pull(options);
        } catch (CmdLineException e) {
            System.err.println(e.getMessage());
            System.err.println("usage: puller [options]");
            new CmdLineParser(new PullerOptions()).printUsage(System.err);
        }
    }

    private static void pull(final PullerOptions options) {
        final LocalDate from = LocalDate.of(2015, 6, 10);
        final LocalDate to = LocalDate.now();
        final List<LocalDate> wednesdays = Dates.getWednesdays(from, to);
        final Comixology comixology = new Comixology(options.getUsername(), options.getPassword());
        final List<Comic> comics = wednesdays.stream()
                .map(comixology::getWeeklyPullList)
                .flatMap(Collection::stream)
                .collect(Collectors.toList());

        comics.stream().sorted().forEach(System.out::println);
        System.out.println("-------------------------------");
        System.out.println(comics.stream().map(Comic::getRawPrice).collect(Collectors.summingDouble(Double::valueOf)));
    }
}

@@ -99,16 +99,65 @@
 Parser;%0A
+import org.kohsuke.args4j.OptionHandlerRegistry;%0A
 import o
@@ -189,16 +189,16 @@
 xology;%0A
-
 %0Aimport 
@@ -370,24 +370,127 @@
 g%5B%5D args) %7B%0A
+        OptionHandlerRegistry.getRegistry().registerHandler(LocalDate.class, DateOptionHandler.class);%0A
         fina
@@ -654,16 +654,67 @@
 t(args);
+%0A            System.out.println(options.getFrom());
 %0A%0A      
@@ -1378,95 +1378,91 @@
 nal 
-LocalDate from = LocalDate.of(2015, 6, 10);%0A        final LocalDate to = LocalDate.now(
+Comixology comixology = new Comixology(options.getUsername(), options.getPassword()
 );%0A 
@@ -1527,72 +1527,8 @@
 ays(
-from, to);%0A        final Comixology comixology = new Comixology(
 opti
@@ -1530,32 +1530,28 @@
 (options.get
-Username
+From
 (), options.
@@ -1549,32 +1549,26 @@
  options.get
-Password
+To
 ());%0A       

Use dates provided by command line