#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2006  Donald N. Allingham
#               2009       Gary Burton
#
# 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 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
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# $Id: _EditEventRef.py 11905 2009-02-07 22:54:16Z gburto01 $

#-------------------------------------------------------------------------
#
# Python modules
#
#-------------------------------------------------------------------------
from gettext import gettext as _

#-------------------------------------------------------------------------
#
# GTK/Gnome modules
#
#-------------------------------------------------------------------------
from gtk import glade

#-------------------------------------------------------------------------
#
# gramps modules
#
#-------------------------------------------------------------------------
import const
import Config
import gen.lib

from DisplayTabs import (SourceEmbedList, NoteTab, GalleryTab, 
                         EventBackRefList, AttrEmbedList)
from widgets import (PrivacyButton, MonitoredEntry,
                     MonitoredDate, MonitoredDataType)
from _EditReference import RefTab, EditReference

from ObjectEntries import PlaceEntry

#-------------------------------------------------------------------------
#
# Constants
#
#-------------------------------------------------------------------------


#-------------------------------------------------------------------------
#
# EditEventRef class
#
#-------------------------------------------------------------------------
class EditEventRef(EditReference):

    def __init__(self, state, uistate, track, event, event_ref, update):
        EditReference.__init__(self, state, uistate, track, event, event_ref,
                               update)
        self._init_event()

    def _local_init(self):
        self.width_key = Config.EVENT_REF_WIDTH
        self.height_key = Config.EVENT_REF_HEIGHT

        self.top = glade.XML(const.GLADE_FILE, "event_eref_edit","gramps")
        self.set_window(self.top.get_widget('event_eref_edit'),
                        self.top.get_widget('eer_title'),
                        _('Event Reference Editor'))
        self.define_warn_box(self.top.get_widget("eer_warning"))
        self.define_expander(self.top.get_widget("eer_expander"))
        self.share_btn = self.top.get_widget('share_place')
        self.add_del_btn = self.top.get_widget('add_del_place')

        tblref =  self.top.get_widget('table64')
        notebook = self.top.get_widget('notebook_ref')
        #recreate start page as GrampsTab
        notebook.remove_page(0)
        self.reftab = RefTab(self.dbstate, self.uistate, self.track, 
                              _('General'), tblref)
        tblref =  self.top.get_widget('table62')
        notebook = self.top.get_widget('notebook')
        #recreate start page as GrampsTab
        notebook.remove_page(0)
        self.primtab = RefTab(self.dbstate, self.uistate, self.track, 
                              _('_General'), tblref)

    def _init_event(self):
        self.commit_event = self.db.commit_personal_event
        self.add_event = self.db.add_person_event

    def get_custom_events(self):
        return self.db.get_person_event_types()

    def _connect_signals(self):
        self.define_ok_button(self.top.get_widget('ok'),self.ok_clicked)
        self.define_cancel_button(self.top.get_widget('cancel'))
        # FIXME: activate when help page is available
        #self.define_help_button(self.top.get_widget('help'))

    def _setup_fields(self):
        
        self.ref_privacy = PrivacyButton(
            self.top.get_widget('eer_ref_priv'),
            self.source_ref, self.db.readonly)

        self.descr_field = MonitoredEntry(
            self.top.get_widget("eer_description"),
            self.source.set_description,
            self.source.get_description,
            self.db.readonly)

        self.gid = MonitoredEntry(
            self.top.get_widget("gid"),
            self.source.set_gramps_id,
            self.source.get_gramps_id,
            self.db.readonly)

        self.place_field = PlaceEntry(
            self.dbstate,
            self.uistate,
            self.track,
            self.top.get_widget("eer_place"),
            self.source.set_place_handle,
            self.source.get_place_handle,
            self.share_btn,
            self.add_del_btn)

        self.ev_privacy = PrivacyButton(
            self.top.get_widget("eer_ev_priv"),
            self.source, self.db.readonly)
                
        self.role_selector = MonitoredDataType(
            self.top.get_widget('eer_role_combo'),
            self.source_ref.set_role,
            self.source_ref.get_role,
            self.db.readonly,
            self.db.get_event_roles()
            )

        self.event_menu = MonitoredDataType(
            self.top.get_widget("eer_type_combo"),
            self.source.set_type,
            self.source.get_type,
            self.db.readonly,
            custom_values=self.get_custom_events())

        self.date_check = MonitoredDate(
            self.top.get_widget("eer_date_entry"),
            self.top.get_widget("eer_date_stat"),
            self.source.get_date_object(),
            self.uistate,
            self.track,
            self.db.readonly)

    def _create_tabbed_pages(self):
        """
        Create the notebook tabs and inserts them into the main
        window.
        """

        notebook = self.top.get_widget('notebook')
        notebook_ref = self.top.get_widget('notebook_ref')

        self._add_tab(notebook, self.primtab)
        self._add_tab(notebook_ref, self.reftab)

        self.srcref_list = self._add_tab(
            notebook,
            SourceEmbedList(self.dbstate,self.uistate,self.track,self.source))

        self.attr_list = self._add_tab(
            notebook,
            AttrEmbedList(self.dbstate, self.uistate, self.track,
                          self.source.get_attribute_list()))

        self.note_tab = self._add_tab(
            notebook,
            NoteTab(self.dbstate, self.uistate, self.track,
                    self.source.get_note_list(),
                    notetype=gen.lib.NoteType.EVENT))
        
        self.note_ref_tab = self._add_tab(
            notebook_ref,
            NoteTab(self.dbstate, self.uistate, self.track,
                    self.source_ref.get_note_list(),
                    notetype=gen.lib.NoteType.EVENTREF))
        
        self.gallery_tab = self._add_tab(
            notebook,
            GalleryTab(self.dbstate, self.uistate, self.track,
                       self.source.get_media_list()))

        self.backref_tab = self._add_tab(
            notebook,
            EventBackRefList(self.dbstate, self.uistate, self.track,
                             self.db.find_backlink_handles(self.source.handle),
                             self.enable_warnbox))

        self.attr_ref_list = self._add_tab(
            notebook_ref,
            AttrEmbedList(self.dbstate, self.uistate, self.track,
                          self.source_ref.get_attribute_list()))

        self._setup_notebook_tabs( notebook)
        self._setup_notebook_tabs( notebook_ref)

    def build_menu_names(self,eventref):
        if self.source:
            event_name = str(self.source.get_type())
            submenu_label = _('Event: %s')  % event_name
        else:
            submenu_label = _('New Event')
        return (_('Event Reference Editor'),submenu_label)
        
    def ok_clicked(self, obj):

        trans = self.db.transaction_begin()
        if self.source.handle:
            self.commit_event(self.source,trans)
            self.db.transaction_commit(trans,_("Modify Event"))
        else:
            self.add_event(self.source,trans)
            self.db.transaction_commit(trans,_("Add Event"))
            self.source_ref.ref = self.source.handle
        
        if self.update:
            self.update(self.source_ref,self.source)

        self.close()

class EditFamilyEventRef(EditEventRef):

    def __init__(self, state, uistate, track, event, event_ref, update):
        
        EditEventRef.__init__(self, state, uistate, track, event,
                              event_ref, update)
        
    def _init_event(self):
        self.commit_event = self.db.commit_family_event
        self.add_event = self.db.add_family_event

    def get_custom_events(self):
        return [ gen.lib.EventType((gen.lib.EventType.CUSTOM,val)) \
                 for val in self.dbstate.db.get_family_event_types()]
