# -*- coding: utf-8 -*-

#  Copyright © 2011-2012  B. Clausius <barcc@gmx.de>
#
#  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, division

from xml.sax.saxutils import escape

from gi.repository import GObject
from gi.repository import Gtk


class FilesModel (Gtk.TreeStore):
    class FilesRow (object):
        def __init__(self, tree_model_row):
            self.tree_model_row = tree_model_row
        #def __getitem__(self, key):
        #    return self.tree_model_row[key]
        #def __setitem__(self, key, value):
        #    self.tree_model_row[key] = value
        def iterchildren(self):
            for row in self.tree_model_row.iterchildren():
                yield self.__class__(row)
        _itemgetter = lambda index: lambda self: self.tree_model_row[index]
        _itemsetter = lambda index: lambda self, value: self.tree_model_row.__setitem__(index, value)
        display_string = property(_itemgetter(0), _itemsetter(0))
        abspath = property(_itemgetter(1), _itemsetter(1))
        checked = property(_itemgetter(2), _itemsetter(2))
        group = property(_itemgetter(3), _itemsetter(3))
        relpath = property(_itemgetter(4), _itemsetter(4))
        file_id = property(_itemgetter(5), _itemsetter(5))
        checkbox_visible = property(_itemgetter(6), _itemsetter(6))
        display_string_nh = property(_itemgetter(7), _itemsetter(7))
        visible = property(_itemgetter(8), _itemsetter(8))
        
    def __init__(self):
        Gtk.TreeStore.__init__(self,
                str,    # 0: display string
                str,    # 1: absolute path for file rows, '' else
                bool,   # 2: checked (for commit)
                str,    # 3: group (nobranch,all,added,removed,renamed,kind_changed,
                        #           modified,unchanged,ignored,unknown,message)
                str,    # 4: relative path, or ''
                str,    # 5: file id
                bool,   # 6: checkbox visible
                str,    # 7: display path / message
                bool,   # 8: row visible
            )
        self.modelfilter = self.filter_new()
        self.modelfilter.set_visible_column(8)
        
    def __getitem__(self, tpath):
        return self.FilesRow(Gtk.TreeStore.__getitem__(self, tpath))
        
    def filterpath_to_path(self, tpath):
        #FIXME: Find out why tpath is sometimes a Gtk.TreePath and sometimes a str
        if type(tpath) is not Gtk.TreePath and tpath is not None:
            tpath = Gtk.TreePath(tpath)
        return tpath and self.modelfilter.convert_path_to_child_path(tpath)
        
    def path_to_filterpath(self, tpath):
        return tpath and self.modelfilter.convert_child_path_to_path(tpath)
        
    def iter_to_filterpath(self, titer):
        return self.modelfilter.convert_child_path_to_path(self.get_path(titer))
        
    @staticmethod
    def format_current_path(path):
        return '<span foreground="#7f7f00">%s</span>' % escape(path)
        
    def row_highlight(self, tpath, highlight):
        if highlight:
            self[tpath].display_string = self.format_current_path(self[tpath].display_string_nh)
        else:
            self[tpath].display_string = self[tpath].display_string_nh
            
            
