#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generated by wxGlade 0.4.1 on Sun Aug 19 13:06:42 2007

import wx
from jubatu import events
import logging
import gettext
import os
from jubatu import util

# Load the translations file for the preferred language
lang = gettext.translation('chess', os.path.join(os.path.dirname(__file__), 'i18n'), languages=util.get_preferred_languages(), fallback=True)
# A game module MUST not use the lang.install() function, as it would override the default language for the core
_ = lang.ugettext 


class NewChessGamePanel(wx.Panel):
    """Class implementing the 'new game' panel for the chess engine.
    
    As of now, the allowable selections are: adversary to play with and colour of the pieces to be ruled by us."""
    
    def __init__(self, engine, *args, **kwds):
        # begin wxGlade: NewChessGamePanel.__init__
        kwds["style"] = wx.STATIC_BORDER|wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.label_5 = wx.StaticText(self, -1, _("Adversary:"))
        self.list_box_1 = wx.ListBox(self, -1, choices=[], style=wx.LB_SINGLE)
        self.label_6 = wx.StaticText(self, -1, _("I want to play with:"))
        self.piecesRadioBox = wx.RadioBox(self, -1, "", choices=[_("White"), _("Black")], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
        self.button_2 = wx.Button(self, -1, _("Send invitation"))
        self.button_3 = wx.Button(self, -1, _("Cancel"))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_LISTBOX_DCLICK, self.delete_opponent, self.list_box_1)
        self.Bind(wx.EVT_BUTTON, self.send_proposal, self.button_2)
        self.Bind(wx.EVT_BUTTON, self.cancel, self.button_3)
        # end wxGlade

        self.engine = engine
        wx.GetApp().buddySelectionManager.buddy_selection_bind(self.list_box_1, self.opponent_selected)

    def __set_properties(self):
        # begin wxGlade: NewChessGamePanel.__set_properties
        self.list_box_1.SetFocus()
        self.piecesRadioBox.SetSelection(0)
        self.button_2.Enable(False)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: NewChessGamePanel.__do_layout
        sizer_2 = wx.FlexGridSizer(2, 1, 30, 0)
        sizer_3 = wx.FlexGridSizer(1, 2, 0, 0)
        grid_sizer_2 = wx.FlexGridSizer(2, 2, 10, 15)
        grid_sizer_2.Add(self.label_5, 0, wx.TOP|wx.BOTTOM, 12)
        grid_sizer_2.Add(self.list_box_1, 0, wx.EXPAND, 0)
        grid_sizer_2.Add(self.label_6, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_2.Add(self.piecesRadioBox, 0, wx.EXPAND, 0)
        grid_sizer_2.AddGrowableCol(1)
        sizer_2.Add(grid_sizer_2, 1, wx.EXPAND, 0)
        sizer_3.Add(self.button_2, 0, 0, 0)
        sizer_3.Add(self.button_3, 0, wx.ALIGN_RIGHT, 0)
        sizer_3.AddGrowableCol(0)
        sizer_3.AddGrowableCol(1)
        sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        sizer_2.SetSizeHints(self)
        sizer_2.AddGrowableRow(1)
        sizer_2.AddGrowableCol(0)
        # end wxGlade

    def cancel(self, event): # wxGlade: NewChessGamePanel.<event_handler>
        """Close the 'new game' panel without sending any proposal."""
        
        self.AddPendingEvent(events.JuCloseCurrentTab())
        event.Skip()

    def send_proposal(self, event): # wxGlade: NewChessGamePanel.<event_handler>
        """Send a 'new game' proposal to the chosen adversary."""
        
        color = {0:u'white', 1:u'black'}
        self.engine.send_proposal(self.list_box_1.GetClientData(0), color[self.piecesRadioBox.GetSelection()])  # (opponent, color)
        self.ProcessEvent(events.JuStatusMessageEvent(_(u"Invitation sent. Waiting for an answer..."), 10000))
        self.AddPendingEvent(events.JuCloseCurrentTab())
        event.Skip()

    def opponent_selected(self, event):
        """Select an adversary.
        
        Take a look at the __init__ function to see how this is linked to the 'BuddySelectionManager'
        user in Jubatu program-wide.
        """
        
        logging.getLogger("chessEngine").info(event.jid.as_unicode())
        if event.control.GetCount() is 0:
            event.control.Append(event.jid.as_unicode(), event.jid)
            self.button_2.Enable(True)

    def delete_opponent(self, event): # wxGlade: NewChessGamePanel.<event_handler>
        """Deselect a selected adversary.
        
        This is called when the user double-click in the adversary. Take a look at the __init__ function.
        """
        
        self.button_2.Enable(False)
        event.GetEventObject().Delete(event.GetInt())
        event.Skip()

# end of class NewChessGamePanel


