# -*- coding: utf-8 -*-
# Phatch - Photo Batch Processor
# Copyright (C) 2007-2008 www.stani.be
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/
#
# Phatch recommends SPE (http://pythonide.stani.be) for editing python files.

# Embedded icon is designed by Alexandre Moore (http://nuovext.pwsp.net/).

# Follows PEP8

import time
from datetime import datetime

from core import ct, models
from lib.metadata import DateTime
from lib.reverse_translation import _t

OPTIONS = [_t('Exif time'), _t('File time'), _t('Exif and file')]


def init():
    global relativedelta
    from other.relativedelta import relativedelta


def get_date(info):
    return datetime(
        year=info[_t('year')],
        month=info[_t('month')],
        day=info[_t('day')],
        hour=info[_t('hour')],
        minute=info[_t('minute')],
        second=info[_t('second')])


class Action(models.Action):
    label = _t('Time Shift')
    author = 'Juho Vepsäläinen'
    email = 'bebraw@gmail.com'
    version = '0.2'
    init = staticmethod(init)
    tags = [_t('metadata')]
    __doc__ = _t('Shift time values of tags')
    metadata = ['Exif_Image_DateTime', 'year', 'month', 'day', 'hour',
        'minute', 'second']  # only specified for reading

    def interface(self, fields):
        fields[_t('Change')] = self.ChoiceField(OPTIONS[2], choices=OPTIONS)
        fields[_t('Use exif datetime')] = self.BooleanField(True)
        fields[_t('Seconds')] = self.IntegerField(0)
        fields[_t('Minutes')] = self.IntegerField(0)
        fields[_t('Hours')] = self.IntegerField(0)
        fields[_t('Days')] = self.IntegerField(0)
        fields[_t('Months')] = self.IntegerField(0)
        fields[_t('Years')] = self.IntegerField(0)

    def apply(self, photo, setting, cache):
        info = photo.info
        method = self.get_field('Change', info)
        date_delta = self._construct_date_delta(photo)
        if method == OPTIONS[1] or not self.get_field('Use exif datetime'):
            new_date = get_date(info) + date_delta
        else:
            try:
                new_date = info['Exif_Image_DateTime'].datetime + date_delta
            except KeyError:
                new_date = get_date(info) + date_delta
        if method in (OPTIONS[1], OPTIONS[2]):
            try:
                photo.modify_date = time.mktime(new_date.timetuple())
            except OverflowError:
                raise Exception(
                    _('Sorry, time shift can only handle '\
                      'dates before 2038-1-19'))
        new_date = DateTime(new_date)
        if method in (OPTIONS[0], OPTIONS[2]):
            info['Exif_Image_DateTime'] = new_date
            info['Exif_Photo_DateTimeOriginal'] = new_date
            info['Exif_Photo_DateTimeDigitized'] = new_date
        return photo

    def get_relevant_field_labels(self):
        relevant = ['Change', 'Seconds', 'Minutes', 'Hours', 'Days', 'Months',
        'Years']
        if self.get_field_string('Change') == OPTIONS[2]:
            relevant.append('Use exif datetime')
        return relevant

    def _construct_date_delta(self, photo):
        info = photo.info

        second_delta = self.get_field('Seconds', info)
        minute_delta = self.get_field('Minutes', info)
        hour_delta = self.get_field('Hours', info)
        day_delta = self.get_field('Days', info)
        month_delta = self.get_field('Months', info)
        year_delta = self.get_field('Years', info)

        return relativedelta(
            years=year_delta, months=month_delta, days=day_delta,
            hours=hour_delta, minutes=minute_delta, seconds=second_delta)

    icon = \
'x\xda\x01^\x0b\xa1\xf4\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x000\x00\
\x00\x000\x08\x06\x00\x00\x00W\x02\xf9\x87\x00\x00\x00\x04sBIT\x08\x08\x08\
\x08|\x08d\x88\x00\x00\x0b\x15IDATh\x81\xed\x9a{lS\xd7\x1d\xc7\xbf\xf7\xdc\
\x87_q\xeck;\xb1\xd3<\xec\xbc\x80\x8e\x04\x92\n\xe8\\\xa0*[!\xb4T\n\xd3\xd6\
\x0eU\xac\xd2\xaan\xeb\xfaO\xa9*\xb5\xaa\x86\xda?\x8a\xd4\x7f\xdaj\xffN\xea(\
\x9b\xa0\x12\x12]\xa7\t\xba\xc2DT\xda\x82(\nKKy\xd5$NB\x9c\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