#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2002-2006  Donald N. Allingham
# Copyright (C) 2011       Tim G L Lyons
#
# 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$

#-------------------------------------------------------------------------
#
# gtk
#
#-------------------------------------------------------------------------
from gi.repository import Gtk

#-------------------------------------------------------------------------
#
# GRAMPS modules
#
#-------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext

from gramps.gen.constfunc import cuni

from ...widgets import MonitoredMenu, DateEntry, BasicEntry
from gramps.gen.lib import Citation
from .. import build_filter_model
from . import SidebarFilter
from gramps.gen.constfunc import cuni
from gramps.gen.filters import GenericFilterFactory, rules
from gramps.gen.filters.rules.citation import (RegExpIdOf, HasCitation,
                                               HasNoteRegexp, MatchesFilter,
                                               HasSource, RegExpSourceIdOf,
                                               HasSourceNoteRegexp)
from gramps.gen.utils.string import conf_strings
GenericCitationFilter = GenericFilterFactory('Citation')
#-------------------------------------------------------------------------
#
# CitationSidebarFilter class
#
#-------------------------------------------------------------------------
class CitationSidebarFilter(SidebarFilter):

    def __init__(self, dbstate, uistate, clicked):
        self.clicked_func = clicked
        self.filter_src_id = BasicEntry()
        self.filter_src_title = BasicEntry()
        self.filter_src_author = BasicEntry()
        self.filter_src_abbr = BasicEntry()
        self.filter_src_pub = BasicEntry()
        self.filter_src_note = BasicEntry()
        self.filter_id = Gtk.Entry()
        self.filter_page = Gtk.Entry()       
        self.filter_date = DateEntry(uistate, [])
        
        self.filter_conf = Gtk.ComboBox()
        model = Gtk.ListStore(str)
        for conf_value in sorted(conf_strings.keys()):
            model.append((_(conf_strings[conf_value]),))
        self.filter_conf.set_model(model)
        self.filter_conf.set_active(Citation.CONF_NORMAL)
        
        self.filter_note = Gtk.Entry()

        self.filter_regex = Gtk.CheckButton(_('Use regular expressions'))

        self.generic = Gtk.ComboBox()

        SidebarFilter.__init__(self, dbstate, uistate, "Citation")

    def create_widget(self):
        cell = Gtk.CellRendererText()
        cell.set_property('width', self._FILTER_WIDTH)
        cell.set_property('ellipsize', self._FILTER_ELLIPSIZE)
        self.generic.pack_start(cell, True)
        self.generic.add_attribute(cell, 'text', 0)
        self.on_filters_changed('Citation')

        cell = Gtk.CellRendererText()
        cell.set_property('width', self._FILTER_WIDTH)
        cell.set_property('ellipsize', self._FILTER_ELLIPSIZE)
        self.filter_conf.pack_start(cell, True)
        self.filter_conf.add_attribute(cell, 'text', 0)

        self.add_text_entry(_('Source: ID'), self.filter_src_id)
        self.add_text_entry(_('Source: Title'), self.filter_src_title)
        self.add_text_entry(_('Source: Author'), self.filter_src_author)
        self.add_text_entry(_('Source: Abbreviation'), self.filter_src_abbr)
        self.add_text_entry(_('Source: Publication'), self.filter_src_pub)
        self.add_text_entry(_('Source: Note'), self.filter_src_note)
        self.add_text_entry(_('Citation: ID'), self.filter_id)
        self.add_text_entry(_('Citation: Volume/Page'), self.filter_page)
        self.add_text_entry(_('Citation: Date'), self.filter_date)
        self.add_entry(_('Citation: Minimum Confidence|Min. Conf.'), self.filter_conf)
        self.add_text_entry(_('Citation: Note'), self.filter_note)
        self.add_filter_entry(_('Custom filter'), self.generic)
        self.add_entry(None, self.filter_regex)

    def clear(self, obj):
        self.filter_src_id.set_text('')
        self.filter_src_title.set_text('')
        self.filter_src_author.set_text('')
        self.filter_src_abbr.set_text('')
        self.filter_src_pub.set_text('')
        self.filter_src_note.set_text('')
        self.filter_id.set_text('')
        self.filter_page.set_text('')
        self.filter_date.set_text('')
        self.filter_conf.set_active(Citation.CONF_NORMAL)
        self.filter_note.set_text('')
        self.generic.set_active(0)

    def get_filter(self):
        src_id = cuni(self.filter_src_id.get_text()).strip()
        src_title = cuni(self.filter_src_title.get_text()).strip()
        src_author = cuni(self.filter_src_author.get_text()).strip()
        src_abbr = cuni(self.filter_src_abbr.get_text()).strip()
        src_pub = cuni(self.filter_src_pub.get_text()).strip()
        src_note = cuni(self.filter_src_note.get_text()).strip()
        gid = cuni(self.filter_id.get_text()).strip()
        page = cuni(self.filter_page.get_text()).strip()
        date = cuni(self.filter_date.get_text()).strip()
        model = self.filter_conf.get_model()
        node = self.filter_conf.get_active_iter()
        conf_name = model.get_value(node, 0)  # The value is actually the text
        conf = Citation.CONF_NORMAL
        for i in list(conf_strings.keys()):
            if _(conf_strings[i]) == conf_name:
                conf = i
                break
#        conf = self.citn.get_confidence_level()
        note = cuni(self.filter_note.get_text()).strip()
        regex = self.filter_regex.get_active()
        gen = self.generic.get_active() > 0

        empty = not (src_id or src_title or src_author or src_abbr or src_pub or
                     src_note or
                     gid or page or date or conf or note or regex or gen)
        if empty:
            generic_filter = None
        else:
            generic_filter = GenericCitationFilter()
            if gid:
                rule = RegExpIdOf([gid], use_regex=regex)
                generic_filter.add_rule(rule)

            rule = HasCitation([page, date, conf], use_regex=regex)
            generic_filter.add_rule(rule)
            
            if src_id:
                rule = RegExpSourceIdOf([src_id], use_regex=regex)
                generic_filter.add_rule(rule)
            
            rule = HasSource([src_title, src_author, src_abbr, src_pub],
                             use_regex=regex)
            generic_filter.add_rule(rule)
                
            if note:
                rule = HasNoteRegexp([note], use_regex=regex)
                generic_filter.add_rule(rule)

            if src_note:
                rule = HasSourceNoteRegexp([src_note], use_regex=regex)
                generic_filter.add_rule(rule)

            if self.generic.get_active() != 0:
                model = self.generic.get_model()
                node = self.generic.get_active_iter()
                obj = cuni(model.get_value(node, 0))
                rule = MatchesFilter([obj])
                generic_filter.add_rule(rule)

        return generic_filter

    def on_filters_changed(self, name_space):
        if name_space == 'Citation':
            all_filter = GenericCitationFilter()
            all_filter.set_name(_("None"))
            all_filter.add_rule(rules.citation.AllCitations([]))
            self.generic.set_model(build_filter_model('Citation', [all_filter]))
            self.generic.set_active(0)
