#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.5 on Tue Aug 27 21:43:54 2013

import wx
import datetime
import time

# begin wxGlade: extracode
# end wxGlade


class DatePickerDialog(wx.Dialog):
    def __init__(self, images, *args, **kwds):
        self.images = images
        self.currentImage = 0
        # begin wxGlade: DatePickerDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER | wx.THICK_FRAME
        wx.Dialog.__init__(self, *args, **kwds)
        self.bitmap_1 = wx.StaticBitmap(self, -1, wx.NullBitmap)
        self.label_1 = wx.StaticText(self, -1, "Date:")
        self.dateCtrl = wx.DatePickerCtrl(self, -1)
        self.pathLabel = wx.StaticText(self, -1, "path")
        self.button_1 = wx.Button(self, -1, "Save")
        self.button_2 = wx.Button(self, -1, "Skip")
        self.button_3 = wx.Button(self, -1, "Skip All")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.onSave, self.button_1)
        self.Bind(wx.EVT_BUTTON, self.onSkip, self.button_2)
        self.Bind(wx.EVT_BUTTON, self.onSkipAll, self.button_3)
        # end wxGlade
        self.Bind(wx.EVT_CLOSE, self.onClose)

    def __set_properties(self):
        # begin wxGlade: DatePickerDialog.__set_properties
        self.SetTitle("Choose date for an image...")
        self.SetSize((654, 471))
        self.bitmap_1.SetMinSize((600, 400))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: DatePickerDialog.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(self.bitmap_1, 0, 0, 0)
        sizer_3.Add(self.label_1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        sizer_3.Add(self.dateCtrl, 0, wx.ALL, 5)
        sizer_3.Add(self.pathLabel, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
        sizer_1.Add(sizer_3, 0, wx.EXPAND, 0)
        sizer_2.Add(self.button_1, 0, 0, 0)
        sizer_2.Add(self.button_2, 0, 0, 0)
        sizer_2.Add(self.button_3, 0, 0, 0)
        sizer_1.Add(sizer_2, 0, 0, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade
        self.showImage(0)

    def onSave(self, event):  # wxGlade: DatePickerDialog.<event_handler>
        self.images[self.currentImage] = (self.images[self.currentImage][0], datetime.datetime.fromtimestamp(self.dateCtrl.GetValue().GetTicks()))
        self.nextImage()       
    
    def onSkip(self, event):  # wxGlade: DatePickerDialog.<event_handler>
        self.nextImage()

    def onSkipAll(self, event):  # wxGlade: DatePickerDialog.<event_handler>
        self.Destroy()
        
    def onClose(self, event):
        self.Destroy()
        
    def nextImage(self):
        self.currentImage += 1
        if self.currentImage < len(self.images):
            self.showImage(self.currentImage)
        else:
            self.Destroy()

    def showImage(self, imageId):
        suggested_date = self.images[self.currentImage][1]
        if suggested_date is not None:
            wx_time = wx.DateTimeFromTimeT(time.mktime(suggested_date.timetuple())) 
            self.dateCtrl.SetValue(wx_time)
        path = self.images[self.currentImage][0]
        bitmap = wx.Bitmap(path)    
        image = self.scaleImage(self.images[self.currentImage][0], 600, 400)
        self.bitmap_1.SetBitmap(wx.BitmapFromImage(image))
        self.pathLabel.SetLabel(path)
        #self.Layout()
         
    def scaleImage(self, filename, maxW, maxH):
        img = wx.Image(filename, wx.BITMAP_TYPE_ANY)
        W = img.GetWidth()
        H = img.GetHeight()
        if H == 0 or W == 0:
            raise Exception('Could not read size of an image: %s', filename)
        if maxW/maxH > W/H:
            NewH = maxH
            NewW = maxH * W / H
        else:
            NewW = maxW
            NewH = maxW * H / W
        return img.Scale(NewW, NewH, wx.IMAGE_QUALITY_HIGH)

# end of class DatePickerDialog
if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    dialog_1 = DatePickerDialog(None, -1, "")
    app.SetTopWindow(dialog_1)
    dialog_1.Show()
    app.MainLoop()
