# queryrow.py
# Copyright 2015 Roger Marsh
# Licence: See LICENCE (BSD licence)

"""Create widgets that display details of game selection rules on database.
"""

import tkinter

from solentware_grid.gui.datarow import DataRow
from solentware_grid.gui.datarow import GRID_COLUMNCONFIGURE, GRID_CONFIGURE
from solentware_grid.gui.datarow import WIDGET_CONFIGURE, WIDGET, ROW

from ..core.chessrecord import ChessDBrecordQuery
from .querydbedit import ChessDBeditQuery
from .querydbdelete import ChessDBdeleteQuery
from .querydbshow import ChessDBshowQuery
from . import constants

ON_DISPLAY_COLOUR = '#eba610' # a pale orange


class ChessDBrowQuery(ChessDBrecordQuery, DataRow):
    
    """Define row in list of game selection rules.

    Add row methods to the game selection rule record definition.
    
    """

    header_specification = [
        {WIDGET: tkinter.Label,
         WIDGET_CONFIGURE: dict(text='Description'),
         GRID_CONFIGURE: dict(column=0, sticky=tkinter.EW),
         GRID_COLUMNCONFIGURE: dict(weight=1, uniform='pp'),
         ROW: 0,
         },
        #{WIDGET: tkinter.Label,
        # WIDGET_CONFIGURE: dict(text='Rule'),
        # GRID_CONFIGURE: dict(column=1, sticky=tkinter.EW),
        # GRID_COLUMNCONFIGURE: dict(weight=1, uniform='pp'),
        # ROW: 0,
        # },
        ]

    def __init__(self, database=None, ui=None):
        """Extend and associate record definition with database.

        database - the open database that is source of row data
        ui - the ChessUI instamce

        """
        super().__init__()
        self.ui = ui
        self.set_database(database)
        self.row_specification = [
            {WIDGET: tkinter.Label,
             WIDGET_CONFIGURE: dict(font=constants.LISTS_OF_GAMES_FONT),
             GRID_CONFIGURE: dict(column=0, sticky=tkinter.EW),
             ROW: 0,
             },
            #{WIDGET: tkinter.Label,
            # WIDGET_CONFIGURE: dict(font=constants.LISTS_OF_GAMES_FONT),
            # GRID_CONFIGURE: dict(column=1, sticky=tkinter.EW),
            # ROW: 0,
            # },
            ]
        
    def show_row(self, dialog, oldobject):
        """Return a ChessDBshowQuery dialog for instance.

        dialog - a Toplevel
        oldobject - a ChessDBrecordQuery containing original data

        """
        return ChessDBshowQuery(dialog, oldobject, ui=self.ui)
        
    def delete_row(self, dialog, oldobject):
        """Return a ChessDBdeleteQuery dialog for instance.

        dialog - a Toplevel
        oldobject - a ChessDBrecordQuery containing original data

        """
        return ChessDBdeleteQuery(dialog, oldobject, ui=self.ui)

    def edit_row(self, dialog, newobject, oldobject, showinitial=True):
        """Return a ChessDBeditQuery dialog for instance.

        dialog - a Toplevel
        newobject - a ChessDBrecordQuery containing original data to be
                    edited
        oldobject - a ChessDBrecordQuery containing original data
        showintial == True - show both original and edited data

        """
        return ChessDBeditQuery(
            newobject,
            dialog,
            oldobject,
            showinitial=showinitial,
            ui=self.ui)

    def grid_row(self, **kargs):
        """Return super().grid_row(textitems=(...), **kargs).

        Create textitems argument for ChessDBrowQuery instance.

        """
        return super().grid_row(
            textitems=(
                self.value.get_name_text(),
                #self.value.get_selection_rule_text(),
                ),
            **kargs)

    def grid_row_on_display(self, **kargs):
        self._current_row_background = ON_DISPLAY_COLOUR
        return self.grid_row(background=ON_DISPLAY_COLOUR, **kargs)

    def set_background_on_display(self, widgets):
        self._current_row_background = ON_DISPLAY_COLOUR
        self.set_background(widgets, self._current_row_background)


def make_ChessDBrowQuery(chessui):
    """Make ChessDBrowQuery with reference to ChessUI instance"""
    def make_selection(database=None):
        return ChessDBrowQuery(database=database, ui=chessui)
    return make_selection
