#
#   gamelist.py
#
#   This file is part of jcchess
#
#   jcchess 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.
#
#   jcchess 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 jcchess.  If not, see <http://www.gnu.org/licenses/>.
#

from gi.repository import Gtk
import os
from . import gv


class Gamelist:

    gamelist_ref = None

    def __init__(self):

        glade_dir = gv.jcchess.get_glade_dir()
        self.glade_file = os.path.join(glade_dir, "gamelist.glade")

        # create gamelist window
        self.builder = Gtk.Builder()
        self.builder.set_translation_domain(gv.domain)
        self.builder.add_from_file(self.glade_file)
        self.builder.connect_signals(self)

        self.window = self.builder.get_object("gamelist_window")
        self.treeview = self.builder.get_object("gamelist_treeview")
        self.liststore = self.builder.get_object("liststore1")

        cell0 = Gtk.CellRendererText()
        # cell0.set_property("cell-background", Gdk.color_parse("#F8F8FF"))
        tvcolumn0 = Gtk.TreeViewColumn()
        self.treeview.append_column(tvcolumn0)
        tvcolumn0.pack_start(cell0, True)
        tvcolumn0.set_min_width(50)
        tvcolumn0.set_attributes(cell0, text=0)
        self.treeview.connect("row_activated", self.entry_clicked)
        self.tree_selection = self.treeview.get_selection()

        self.window.hide()

    # user has closed the window
    # just hide it
    def delete_event(self, widget, event):
        self.window.hide()
        return True  # do not propagate to other handlers

    def entry_clicked(self, widget, data=None, data2=None):
        button = None
        self.loadgame_button_clicked_cb(button)
        
    # called from gui.py when doing view gamelist
    def show_gamelist_window_cb(self, action):
        self.show_gamelist_window()

    # called from psn.py when opening multi-game file
    # and from show_gamelist_window_cb above
    def show_gamelist_window(self):
        # "present" will show the window if it is hidden
        # if not hidden it will raise it to the top
        self.window.present()
        return

    def clear(self):
        self.liststore.clear()
        
    def addgame(self, gameno, headers):
        h = str(gameno) + ". "
        hdrno = 1
        for header in headers:
            header_value = headers.get(header)
            # ignore unset headers
            if "?" in header_value:
                continue
            if hdrno > 1:
                h += ", "
            h += header + ": " + header_value
            hdrno += 1
        self.liststore.append([h])

    def loadgame_button_clicked_cb(self, button):
        (treemodel, treeiter) = self.tree_selection.get_selected()
        if treeiter is not None:
            game_str = treemodel.get_value(treeiter, 0)
            gameno = ""
            i = 0
            while game_str[i] != ".":
                gameno += game_str[i]
                i += 1
            try:
                gameno = int(gameno)
            except ValueError as ve:
                return
            gv.load_save.load_game_from_multigame_file(gameno)


def get_ref():
    if Gamelist.gamelist_ref is None:
        Gamelist.gamelist_ref = Gamelist()
    return Gamelist.gamelist_ref
