# -*- coding: UTF-8 -*-

__revision__ = '$Id$'

# Copyright (c) 2009 Michael Jahn
#
# 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 2 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

# You may use and distribute this software under the terms of the
# GNU General Public License, version 2 or later

import datetime
from plugins.export import XmlExportBase
from gettext import gettext as _


class ExportPlugin(XmlExportBase):
    plugin_name         = "Ant Movie Catalog Database XML Export"
    plugin_description  = _("Full Ant Movie Catalog database xml export plugin")
    plugin_author       = "Michael Jahn"
    plugin_author_email = "griffith@griffith.cc"
    plugin_version      = "1.0"

    def __init__(self, database, locations, parent_window, search_conditions, config):
        XmlExportBase.__init__(self, database, locations, parent_window, search_conditions, config)
        self.config_section = 'export-antmovie'
        self.export_name    = 'Ant Movie Catalog database export'
        self.filename       = 'ant-movie-catalog.xml'
        self.encoding       = 'iso-8859-1'

    def export_to_document(self, document, mainelement):
        collelement = document.createElement('AntMovieCatalog')
        collelement.setAttribute('Format', '35')
        collelement.setAttribute('Version', '3.5.1 (2007-09-22)')
        collelement.setAttribute('Date', datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S'))
        document.appendChild(collelement)
        comment = document.createComment('Griffith Export in Ant Movie Catalog database format')
        document.insertBefore(comment, collelement)
        catalogelement = document.createElement('Catalog')
        collelement.appendChild(catalogelement)
        propertieselement = document.createElement('Properties')
        catalogelement.appendChild(propertieselement)
        contentselement = document.createElement('Contents')
        catalogelement.appendChild(contentselement)
        XmlExportBase.export_to_document(self, document, contentselement)

    def process_movie(self, document, mainelement, movie, keys):
        # create node
        itemelement = document.createElement('Movie')
        itemelement.setAttribute('Checked', 'False')
        mainelement.appendChild(itemelement)
        XmlExportBase.process_movie(self, document, itemelement, movie, keys)

    def process_movie_value(self, document, dvdelement, key, value):
        if key == 'number':
            dvdelement.setAttribute('Number', value)
        elif key == 'loaned':
            pass
        elif key == 'seen':
            dvdelement.setAttribute('Checked', value)
        elif key == 'rating':
            dvdelement.setAttribute('Rating', value)
        elif key == 'movies_color':             # SmallInteger
            pass
        elif key == 'movies_cond':              # SmallInteger
            pass
        elif key == 'movies_layers':            # SmallInteger
            pass
        elif key == 'media_num':         # SmallInteger
            dvdelement.setAttribute('Disks', value)
        elif key == 'runtime':           # Integer
            dvdelement.setAttribute('Length', value)
        elif key == 'year':              # year
            dvdelement.setAttribute('Year', value)
        elif key == 'o_title':           # VARCHAR
            dvdelement.setAttribute('OriginalTitle', value)
            dvdelement.setAttribute('FormattedTitle', value)
        elif key == 'title':             # VARCHAR
            dvdelement.setAttribute('TranslatedTitle', value)
        elif key == 'director':          # VARCHAR
            dvdelement.setAttribute('Director', value)
        elif key == 'o_site':            # VARCHAR
            pass
        elif key == 'site':              # VARCHAR
            dvdelement.setAttribute('URL', value)
        elif key == 'trailer':           # VARCHAR
            pass
        elif key == 'country':           # VARCHAR
            dvdelement.setAttribute('Country', value)
        elif key == 'genre':             # VARCHAR
            dvdelement.setAttribute('Category', value.replace('\n', ''))
        elif key == 'region':            # SmallInteger
            pass
        elif key == 'studio':            # VARCHAR
            pass
        elif key == 'classification':    # VARCHAR
            pass
        elif key == 'cast':              # VARCHAR
            if len(value.strip()) > 0:
                actors = value.split('\n')
                actorsstring = ''
                delim = ''
                for actor in actors:
                    actorsplit = actor.split(_(' as '))
                    actornames = actorsplit[0]
                    if len(actorsplit) > 1:
                        actorsstring += delim + actornames.strip() + ' (' + actorsplit[1].strip() + ')'
                    else:
                        actorsstring += delim + actornames.strip()
                    delim = ', '
                dvdelement.setAttribute('Actors', actorsstring)
        elif key == 'plot':              # VARCHAR
            dvdelement.setAttribute('Description', value.replace('\n', '|'))
        elif key == 'notes':             # VARCHAR
            dvdelement.setAttribute('Comments', value.replace('\n', '|'))
        elif key == 'vcodecs_name':             # VARCHAR
            dvdelement.setAttribute('VideoFormat', value)

    def process_movie_image(self, document, dvdelement, imagedata, imagemd5sum):
        if imagemd5sum and imagedata:
            dvdelement.setAttribute('Picture', self.filename + '_pictures/' + imagemd5sum + '.jpg')
            self.save_image_to_file(imagedata, imagemd5sum, self.filename + '_pictures/')

    def process_movie_tags(self, document, movieelement, movie):
        pass

    def process_acodecs(self, document, movieelement, acodecsquery):
        acodecsstring = ''
        delim = ''
        codecsused = []
        for acodec in self.db.session.execute(acodecsquery).fetchall():
            if acodec['name'] not in codecsused:
                acodecsstring += delim + self.convert_value(acodec['name'])
                delim = ', '
                codecsused.append(acodec['name'])
        movieelement.setAttribute('AudioFormat', acodecsstring)

    def process_languages(self, document, movieelement, languagesquery):
        languagesstring = ''
        delim = ''
        languagesused = []
        for language in self.db.session.execute(languagesquery).fetchall():
            if language['languages_name'] not in languagesused:
                languagesstring += delim + self.convert_value(language['languages_name'])
                delim = ', '
                languagesused.append(language['languages_name'])
        movieelement.setAttribute('Languages', languagesstring)

    def process_subtitles(self, document, movieelement, subtitlesquery):
        subtitlesstring = ''
        delim = ''
        for subtitle in self.db.session.execute(subtitlesquery).fetchall():
            subtitlesstring += delim + self.convert_value(subtitle['name'])
            delim = ', '
        movieelement.setAttribute('Subtitles', subtitlesstring)
