#!/usr/bin/python

# debtags - Implement package tags support for Debian
#
# Copyright (C) 2003--2006  Enrico Zini <enrico@debian.org>
#
# 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/>.

from __future__ import print_function

import wx
import wx.html
import wx.lib.dialogs
import sys
import re
import subprocess
from debian import debtags
import apt
from optparse import OptionParser

VERSION="0.1"

class Model(wx.EvtHandler):
    class ModelEvent(wx.PyCommandEvent):
        def __init__(self, event_type):
            wx.PyCommandEvent.__init__(self, event_type)

    # Create custom events
    wxEVT_CHANGED = wx.NewEventType()
    EVT_CHANGED = wx.PyEventBinder(wxEVT_CHANGED, 0)

    def __init__(self, fullcoll):
        wx.EvtHandler.__init__(self)
        self.apt_cache = apt.Cache()

        self.wanted = set()
        self.unwanted = set()
        self.ignored = set()
        self.interesting = []
        self.discriminant = []
        self.uninteresting = []

        self.fullcoll = fullcoll
        self.subcoll = fullcoll

        self.refilter()

    def tag_match(self, pkg):
        tags = self.fullcoll.tags_of_package(pkg)
        if len(self.wanted) > 0 and not self.wanted.issubset(tags):
            return False
        if len(self.unwanted) > 0 and len(tags.intersection(self.unwanted)) > 0:
            return False
        return True

    def refilter(self):
        # Regenerate subcoll
        if len(self.wanted) == 0 and len(self.unwanted) == 0:
            self.subcoll = self.fullcoll
        else:
            self.subcoll = self.fullcoll.filter_packages(self.tag_match)

        # Compute the most interesting tags by discriminance
        self.discriminant = sorted(self.subcoll.iter_tags(), \
                  lambda b, a: cmp(self.subcoll.discriminance(a), self.subcoll.discriminance(b)))
        #print("-----------------------------")
        #for d in self.discriminant:
        #    print(d, self.subcoll.discriminance(d))

        # Notify the change
        e = Model.ModelEvent(Model.wxEVT_CHANGED)
        self.ProcessEvent(e)

    def add_wanted(self, tag):
        self.wanted.add(tag)
        if tag in self.unwanted: self.unwanted.remove(tag)
        if tag in self.ignored: self.ignored.remove(tag)
        self.refilter()
        
    def add_unwanted(self, tag):
        self.unwanted.add(tag)
        if tag in self.wanted: self.wanted.remove(tag)
        if tag in self.ignored: self.ignored.remove(tag)
        self.refilter()

    def add_ignored(self, tag):
        self.ignored.add(tag)
        if tag in self.wanted: self.wanted.remove(tag)
        if tag in self.unwanted: self.unwanted.remove(tag)
        self.refilter()

    def remove_tag_from_filter(self, tag):
        if tag in self.wanted: self.wanted.remove(tag)
        if tag in self.unwanted: self.unwanted.remove(tag)
        if tag in self.ignored: self.ignored.remove(tag)
        self.refilter()

    def set_query(self, query):
        query = query.split()

        def match(pkg):
            for q in query:
                try:
                    if pkg.name.find(q) == -1 and \
                       pkg.raw_description.find(q) == -1:
                        return False
                except UnicodeDecodeError:
                    desc = pkg.raw_description.decode("ascii", "replace")
                    if pkg.name.find(q) == -1 and \
                       desc.find(q) == -1:
                        return False
            return True

        subcoll = self.fullcoll.choose_packages(map(lambda x: x.name, filter(match, self.apt_cache)))

        rel_index = debtags.relevance_index_function(self.fullcoll, subcoll)

        # Get all the tags sorted by increasing relevance
        self.interesting = sorted(subcoll.iter_tags(), lambda b, a: cmp(rel_index(a), rel_index(b)))

        # Get the list of the uninteresting tags, sorted by cardinality (the
        # ones that you are less likely to want, and first the ones that will
        # weed out most results)
        self.uninteresting = set(self.fullcoll.iter_tags())
        self.uninteresting -= set(subcoll.iter_tags())

        if len(self.wanted) == 0:
            self.wanted = set(fullcoll.ideal_tagset(self.interesting))
            self.refilter()

        # Notify the change
        e = Model.ModelEvent(Model.wxEVT_CHANGED)
        self.ProcessEvent(e)

class TagList(wx.html.HtmlWindow):
    class TagListEvent(wx.PyCommandEvent):
        def __init__(self, event_type):
            wx.PyCommandEvent.__init__(self, event_type)
            self.action = None
            self.tag = None

    # Create custom events
    wxEVT_ACTION = wx.NewEventType()
    EVT_ACTION = wx.PyEventBinder(wxEVT_ACTION, 0)

    def __init__(self, parent, model):
        wx.html.HtmlWindow.__init__(self, parent)
        self.model = model
        self.model.Bind(Model.EVT_CHANGED, self.model_changed)
        self.SetBorders(0)
        self.SetFonts("", "", [4, 6, 8, 10, 11, 12, 13])

    def OnLinkClicked(self, link_info):
        # Notify the change
        action, tag = link_info.GetHref().split(':',1)
        e = TagList.TagListEvent(TagList.wxEVT_ACTION)
        e.action, e.tag = action, tag
        self.ProcessEvent(e)

    def model_changed(self, event):
        #print("TLMC")
        self.SetPage("<html><body>")
        first = True

        if len(self.model.wanted):
            if first: first = False;
            else: self.AppendToPage("<br>") 
            self.AppendToPage("Wanted:<br>")
            for tag in self.model.wanted:
                self.AppendToPage("&nbsp;&nbsp;&nbsp;<a href='del:%s'>%s</a><br>" % (tag, tag))

        if len(self.model.unwanted):
            if first: first = False;
            else: self.AppendToPage("<br>") 
            self.AppendToPage("Not wanted:<br>")
            for tag in self.model.unwanted:
                self.AppendToPage("&nbsp;&nbsp;&nbsp;<a href='del:%s'>%s</a><br>" % (tag, tag))

        def filter_candidate(tag):
            if self.model.subcoll.card(tag) == 0: return False
            if tag in self.model.wanted: return False
            if tag in self.model.unwanted: return False
            if tag in self.model.ignored: return False
            return True

        interesting = filter(filter_candidate, self.model.interesting)[:7]
        if len(interesting):
            if first: first = False;
            else: self.AppendToPage("<br>") 
            self.AppendToPage("Candidate tags:<br>")
            for tag in interesting:
                self.AppendToPage("&nbsp;&nbsp;&nbsp;<a href='add:%s'>%s</a> <a href='addnot:%s'>[no]</a> (%d pkgs)<br>" % (tag, tag, tag, self.model.subcoll.card(tag)))

        discr = filter(filter_candidate, self.model.discriminant)[:7]
        if len(discr):
            if first: first = False;
            else: self.AppendToPage("<br>") 
            for tag in discr:
                self.AppendToPage("&nbsp;&nbsp;&nbsp;<a href='add:%s'>%s</a> <a href='addnot:%s'>[no]</a> (%d pkgs)<br>" % (tag, tag, tag, self.model.subcoll.card(tag)))

        unint = filter(filter_candidate,
                sorted(self.model.uninteresting, lambda a,b: cmp(self.model.subcoll.card(b), self.model.subcoll.card(a))))[:7]
        if len(unint):
            if first: first = False;
            else: self.AppendToPage("<br>") 
            self.AppendToPage("Candidate unwanted tags:<br>")
            for tag in unint:
                self.AppendToPage("&nbsp;&nbsp;&nbsp;<a href='addnot:%s'>%s</a> <a href='add:%s'>[yes]</a> (%d pkgs)<br>" % (tag, tag, tag, self.model.subcoll.card(tag)))

        self.AppendToPage("</body></html>")
        event.Skip()

class Results(wx.ListCtrl):
    def __init__(self, parent, model):
        wx.ListCtrl.__init__(self, parent, style=wx.LC_REPORT|wx.LC_VIRTUAL)
        self.model = model
        self.model.Bind(Model.EVT_CHANGED, self.model_changed)

        self.packages = []

        self.InsertColumn(0, "Name")
        self.InsertColumn(1, "Description")

        self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
        self.SetColumnWidth(1, wx.LIST_AUTOSIZE)

        self.Bind(wx.EVT_SIZE, self.OnResize)

    def resize_columns(self):
        """
        Ugly hack to have some decent size for the columns, since the ListCtrl
        appearently won't autosize itself.
        """
        w, h = self.GetClientSizeTuple()
        self.SetColumnWidth(0, w * 0.3)
        # -20 to hope to account for the vertical scrollbar, when it's present
        self.SetColumnWidth(1, w * 0.7 - 20)

    def model_changed(self, event):
        self.packages = sorted(self.model.subcoll.iter_packages())
        self.SetItemCount(len(self.packages))
        self.resize_columns()
        event.Skip()

    def OnResize(self, event):
        self.resize_columns()
        event.Skip()

    def OnGetItemText(self, row, col):
        if col == 0:
            return self.packages[row]
        else:
            aptpkg = self.model.apt_cache[self.packages[row]]
            return aptpkg.raw_description.split("\n")[0]


class SearchWindow(wx.Frame):
    ACTION_QUIT = wx.NewId()
    ACTION_CONTEXT_HELP = wx.NewId()
    ACTION_ABOUT = wx.NewId()

    def __init__(self, parent, model, title):
        wx.Frame.__init__(self, parent, -1, title,
                style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)

        self.status_bar = self.CreateStatusBar()

        # Create menu bar
        self.menu_bar = wx.MenuBar()
        m_file = wx.Menu()
        m_file.Append(SearchWindow.ACTION_QUIT, "&Quit", "Quit wxdballe")
        self.menu_bar.Append(m_file, "&File")
        m_file = wx.Menu()
        m_file.Append(SearchWindow.ACTION_CONTEXT_HELP, "&What is...", "Show context information about interface elements")
        m_file.Append(SearchWindow.ACTION_ABOUT, "&About...", "Show information about this application")
        self.menu_bar.Append(m_file, "&Help")
        self.SetMenuBar(self.menu_bar)
        self.Bind(wx.EVT_MENU, self.on_action)

        self.model = model
        self.model.Bind(Model.EVT_CHANGED, self.model_changed)

        self.SetSizeHints(500, 500)

        query_panel = wx.Panel(self, style=wx.SUNKEN_BORDER)

        query_field = wx.Panel(query_panel)
        self.query = wx.TextCtrl(query_field)
        self.query.SetHelpText("Enter here some keyword about what you are looking for.  They will lead you to a selection of categories that you can use to browse the packages")
        self.query.Bind(wx.EVT_CHAR, self.query_changed)
        # self.query.Bind(wx.EVT_TEXT, self.query_changed)
        self.query_button = wx.Button(query_field, -1, "Go", style=wx.BU_EXACTFIT)
        self.query_button.Bind(wx.EVT_BUTTON, self.go_button_pressed)
        self.query_button.SetHelpText("Look for keyword corresponding to the text entered in the Search field.")

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(wx.StaticText(query_field, -1, "Search: "), 0, wx.ALIGN_CENTER_VERTICAL)
        box.Add(self.query, 1, wx.EXPAND)
        box.Add(self.query_button, 0, wx.ALIGN_CENTER_VERTICAL)
        query_field.SetSizerAndFit(box)

        self.tag_list = TagList(query_panel, model)
        self.tag_list.Bind(TagList.EVT_ACTION, self.wanted_event)
        self.tag_list.SetHelpText("Tags used for searching.  Candidate tags are up for selection: click on a tag to say that you want it, and click on 'no' next to it to say that you do not want it.  To remove a tag from the 'wanted' or 'unwanted' list, just click on it")

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(query_field, 0, wx.EXPAND)
        box.Add(self.tag_list, 3, wx.EXPAND)
        query_panel.SetSizerAndFit(box)

        self.results = Results(self, model)
        self.results.SetHelpText("List of packages matching the current selecion of tags")

        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(query_panel, 2, wx.EXPAND)
        box.Add(self.results, 3, wx.EXPAND)
        self.SetSizerAndFit(box)

        self.timed_updater = None

        self.Bind(wx.EVT_CHAR, self.on_char)
        self.query.SetFocus()

    def on_action(self, event):
        id = event.GetId()
        if id == SearchWindow.ACTION_QUIT:
            self.Destroy()
        elif id == SearchWindow.ACTION_ABOUT:
            msg = """
                  Prototype for a new concept of package search
                  =============================================

                  Introduction
                  ------------

                  Debtags Smart Search is an attempt to use the Debtags
                  category data to create a powerful and intuitive search
                  metaphore.  It combines the ease of use of a keyword search
                  text box with the unambiguiry and accuracy of categories.

                  The keywords entered are not used to filter packages, but to
                  create a selection of relevant tags for the user to choose
                  from.
                  
                  The actual package filtering is done with the tags only.  The
                  way the user interact with tags is through simple "I want
                  this"/"I don't want this" decisions.

                  Tips
                  ----
                   * you can be vague while giving keywords, since you can
                     always refine later using the tags
                   * you can change the search keywords in the middle of a
                     search, to get a different selection of candidate tags.
                  """
            dia = wx.lib.dialogs.ScrolledMessageDialog(self, msg, "About Debtags Smart Search")
            dia.ShowModal() 
        elif id == SearchWindow.ACTION_CONTEXT_HELP:
            context_help = wx.ContextHelp()
            context_help.BeginContextHelp()

    def on_char(self, event):
        c = event.GetKeyCode()
        # Quit on ^Q
        if c == 17:
            self.Destroy()

    def wanted_event(self, event):
        action, tag = event.action, event.tag

        if action == 'add':
            self.model.add_wanted(tag)
        elif action == 'addnot':
            print("wanted_event -> addnot")
            self.model.add_unwanted(tag)
        elif action == 'del':
            self.model.remove_tag_from_filter(tag)
        else:
            print("Unknown action", action)

    def go_button_pressed(self, event):
        self.model.set_query(self.query.GetValue())

    def query_changed(self, event):
        "Delayed update of the filter from the value of the input fields"
        c = event.GetKeyCode()
        if c == 13:
            self.model.set_query(self.query.GetValue())
        event.Skip()

    def model_changed(self, event):
        self.status_bar.SetStatusText("%d packages" % (self.model.subcoll.package_count()))
        event.Skip()


##
### Main
###

class Parser(OptionParser):
    def __init__(self, *args, **kwargs):
        OptionParser.__init__(self, *args, **kwargs)

    def error(self, msg):
        sys.stderr.write("%s: error: %s\n\n" % (self._get_prog_name(), msg))
        self.print_help(sys.stderr)
        sys.exit(2)

if __name__ == "__main__":
    parser = Parser(usage="usage: %prog [options]",
            version="%prog "+ VERSION,
            description="smart search of Debian packages")
    parser.add_option("--tagdb", default="/var/lib/debtags/package-tags", help="Tag database to use (default: %default)")

    (options, args) = parser.parse_args()

    app = wx.PySimpleApp()

    provider = wx.SimpleHelpProvider()
    wx.HelpProvider_Set(provider)

    # Read full database 
    fullcoll = debtags.DB()
    tag_filter = re.compile(r"^special::.+$|^.+::TODO$")
    fullcoll.read(open(options.tagdb, "r"), lambda x: not tag_filter.match(x))

    model = Model(fullcoll)
    sw = SearchWindow(None, model, "Debtags Smart Search")
    sw.Show()
    app.MainLoop()

# vim:set ts=4 sw=4 expandtab:
