"""Define filter for photo indices by command line arguments.
"""

import re
import datetime
from photo.geo import GeoPosition


class IdxFilter(object):

    def __init__(self, args):
        if args.tags is not None:
            self.taglist = set()
            self.negtaglist = set()
            if args.tags:
                for t in args.tags.split(","):
                    if t.startswith("!"):
                        self.negtaglist.add(t[1:])
                    else:
                        self.taglist.add(t)
        else:
            self.taglist = None
            self.negtaglist = None
        self.date = args.date
        if args.gpspos:
            self.gpspos = GeoPosition(args.gpspos)
            self.gpsradius = args.gpsradius
        else:
            self.gpspos = None
        self.filelist = set(args.files) if args.files else None

    def __call__(self, item):
        if self.filelist and not item.filename in self.filelist:
            return False
        if self.taglist is not None:
            if not self.taglist <= item.tags:
                return False
            if self.negtaglist & item.tags:
                return False
            if not self.taglist and not self.negtaglist and item.tags:
                return False
        if self.date and (item.createDate < self.date[0] or 
                          item.createDate >= self.date[1]):
            return False
        if self.gpspos:
            if (not item.gpsPosition or
                (item.gpsPosition - self.gpspos) > self.gpsradius):
                return False
        return True


_datere = re.compile(r'''^
    (?P<y0>\d{1,})-(?P<m0>\d{1,2})-(?P<d0>\d{1,2})
    $''', re.X)

def _strpdate(s):
    match = re.match(_datere, s)
    if match:
        y, m, d = match.group('y0', 'm0', 'd0')
        try:
            startdate = datetime.datetime(int(y), int(m), int(d))
            enddate = startdate + datetime.timedelta(days=1)
            return (startdate, enddate)
        except ValueError:
            pass
    raise argparse.ArgumentTypeError("Invalid date value '%s'" % s)

def addFilterArguments(argparser):
    argparser.add_argument('--tags', 
                           help="select images by comma separated list of tags")
    argparser.add_argument('--date', type=_strpdate, 
                           help="select images by date")
    argparser.add_argument('--gpspos', 
                           help="select images by GPS position")
    argparser.add_argument('--gpsradius', type=float, default=3.0, 
                           help="radius around GPS position in km", 
                           metavar='DISTANCE')
    argparser.add_argument('files', nargs='*', 
                           help="select images by file name")
    return argparser

@@ -1632,18 +1632,17 @@
 P%3Cy0%3E%5Cd%7B
-1,
+4
 %7D)-(?P%3Cm
@@ -1650,34 +1650,359 @@
 %3E%5Cd%7B
-1,2%7D)-(?P%3Cd0%3E%5Cd%7B1,2%7D)
+2%7D)-(?P%3Cd0%3E%5Cd%7B2%7D)              # start date%0A    (?:%5BT %5D(?P%3Ch0%3E%5Cd%7B2%7D):(?P%3Cmin0%3E%5Cd%7B2%7D):(?P%3Cs0%3E%5Cd%7B2%7D))?   # optional start time%0A    (?:%5Cs*(?:--%7C/)%5Cs*                                      # delimeter%0A      (?P%3Cy1%3E%5Cd%7B4%7D)-(?P%3Cm1%3E%5Cd%7B2%7D)-(?P%3Cd1%3E%5Cd%7B2%7D)            # end date%0A      (?:%5BT %5D(?P%3Ch1%3E%5Cd%7B2%7D):(?P%3Cmin1%3E%5Cd%7B2%7D):(?P%3Cs1%3E%5Cd%7B2%7D))? # optional end time
 %0A    
+)?
 $'''
@@ -2024,24 +2024,351 @@
 trpdate(s):%0A
+    def _dt(args):%0A        year, month, day, hour, minute, second = args%0A        if hour is None:%0A            return datetime.datetime(int(year), int(month), int(day))%0A        else:%0A            return datetime.datetime(int(year), int(month), int(day), %0A                                     int(hour), int(minute), int(second))%0A
     match = 
@@ -2414,18 +2414,41 @@
     
-y, m, d = 
+try:%0A            startdate = _dt(
 matc
@@ -2471,16 +2471,37 @@
 0', 'd0'
+, 'h0', 'min0', 's0')
 )%0A      
@@ -2502,32 +2502,125 @@
         
+ 
-try:%0A           
+   if match.group('d1') is None:%0A                if match.group('h0') is None:%0A                    enddate =
  startda
@@ -2622,17 +2622,17 @@
 artdate 
-=
++
  datetim
@@ -2637,41 +2637,267 @@
 ime.
-datetime(int(y), int(m), int(d))%0A
+timedelta(days=1)%0A                else:%0A                    enddate = startdate + datetime.timedelta(seconds=1)%0A            else:%0A                enddate = _dt(match.group('y1', 'm1', 'd1', 'h1', 'min1', 's1'))%0A                if match.group('h1') is None:%0A  
     
@@ -2900,32 +2900,38 @@
             
+      
 enddate 
 = startdate 
@@ -2914,37 +2914,106 @@
     enddate 
++
 = 
-start
+datetime.timedelta(days=1)%0A                else:%0A                    end
 date +
+=
  datetime.ti
@@ -3012,35 +3012,38 @@
 etime.timedelta(
-day
+second
 s=1)%0A           
@@ -3184,16 +3184,17 @@
 %22 %25 s)%0A%0A
+%0A
 def addF

Implement parsing date arguments including intervalls and time.